(TailwindCSS) How do I make a column divider in a grid - grid

I want a divider to go between each of the columns of my grid like this:
The divider should always be in the center of any two columns and should not be displayed on the outer edges of the grid. Note that the number of columns will change according to screen size. Any ideas how to do this?
Here is my code:
https://play.tailwindcss.com/ATXYuNgHg9

You can try using a pseudo-element for the first red blocks, we make height with very long with vh unit and hide it using the overflow property on the parent element.
<script src="https://cdn.tailwindcss.com"></script>
<div class="absolute inset-0 justify-center p-32">
<div class="grid gap-x-8 gap-y-3 overflow-hidden xs:grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
<div class="relative w-30 h-10 bg-red-600 sm:after:content-[''] sm:after:absolute sm:after:w-[3px] sm:after:min-h-[500vh] sm:after:bg-black sm:after:right-[-1rem]"></div>
<div class="relative w-30 h-10 bg-red-600 lg:after:content-[''] lg:after:absolute lg:after:w-[3px] lg:after:min-h-[500vh] lg:after:bg-black lg:after:right-[-1rem]"></div>
<div class="relative w-30 h-10 bg-red-600 xl:after:content-[''] xl:after:absolute xl:after:w-[3px] xl:after:min-h-[500vh] xl:after:bg-black xl:after:right-[-1rem]"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
<div class="w-30 h-10 bg-red-600"></div>
</div>
</div>

Related

Tailwindcss - columns in wrong direction

I have a simple flexbox with a column grid of 2.
<div class="flex relative">
<div class="columns-2 gap-4 space-y-4">
<div class="bg-red-500 p-10">1</div>
<div class="bg-blue-500 p-10 py-14">2</div>
<div class="bg-orange-500 p-10 py-20">3</div>
<div class="bg-green-500 p-10">4</div>
</div>
</div>
The problem is the arrangement of the columns is in the "wrong" direction. The first row contains div1 and div3 but I want it to contain div1 and div2. How can I change the direction?
I want the divs to align like this:
Actually you are not using the grid of tailwind-css !! to use change the columns:2 to grid grid-cols-2
<div class="flex relative">
<div class="grid grid-cols-2 gap-4 space-y-4"> 👈 grid grid-cols-2
<div class="bg-red-500 p-10">1</div>
<div class="bg-blue-500 p-10 py-14">2</div>
<div class="bg-orange-500 p-10 py-20">3</div>
<div class="bg-green-500 p-10">4</div>
</div>
</div>
Now you can see the div1 and div2 are placed in the same row.
Edit:
But this will position the div but the grid looks similar. How to overcome this ?
Use the following code:
<div class="grid grid-cols-2 grid-rows-5 gap-4">
<div class="row-span-2 flex h-64 items-center justify-center bg-red-500 text-5xl">1</div>
<div class="row-span-3 flex items-center justify-center bg-blue-500 text-5xl">2</div>
<div class="row-span-2 flex items-center justify-center bg-orange-500 text-5xl">3</div>
<div class="row-span-2 flex items-center justify-center bg-green-500 text-5xl">4</div>
</div>
Output:
Tailwind-play

Tailwind Grid Height

Hi I'm designing a grid system in NextJS using Tailwind.
I'm having trouble auto sizing the grids to fit the size of the parent element.
Below are two images to help convey my meaning.
Mockup wireframe of the container and grids. (Red = Wrapper, Pink = Layout, Purple = Grids)
What I've managed to code, (I want to resize the right hand boxes to the height of the window.)
<div className="bg-white dark:bg-custom05 md:fixed md:inset-y-0 md:left-0 md:flex md:items-start md:overflow-y-auto md:w-full ">
<div className="min-h-full md:flex md:w-16 md:flex-none md:items-center md:whitespace-nowrap md:py-12 md:leading-7 md:[writing-mode:vertical-rl]">
<div className="flex justify-between text-sm gap-12">
<div className="sm:w-14 sm:h-14 md:w-14 md:h-14 rounded-lg flex items-center justify-center bg-custom03 order-1">Avatar</div>
<div className="sm:w-60 sm:h-14 md:w-14 md:h-60 rounded-lg flex items-center justify-center bg-custom03 order-2">Author Tag Line</div>
<div className="sm:w-14 sm:h-14 md:w-14 md:h-14 rounded-lg flex items-center justify-center bg-custom03 order-3">03</div>
</div>
</div>
<div className="bg-white dark:bg-custom06 relative z-10 mx-auto p-10 md:max-w-md md:min-h-full md:flex-auto md:border-x md:border-custom07">
<div className="grid grid-row-3 grid-flow-row gap-12 text-sm text-center rounded-lg ">
<div className="p-4 rounded-lg bg-custom03 grid place-content-center row-span-6">01</div>
<div className="p-4 rounded-lg bg-custom03 grid place-content-center row-span-6">02</div>
<div className="p-4 rounded-lg bg-custom03 grid place-content-center row-span-6">03</div>
</div>
</div>
<div className="bg-white dark:bg-custom05 relative z-9 p-10 mx-auto md:max-h-full md:flex-auto ">
<div className="grid grid-col-2 grid-flow-col gap-12 text-sm text-center rounded-lg ">
<div className="p-4 rounded-lg bg-custom03 grid place-content-center col-span-2">01</div>
<div className="p-4 rounded-lg bg-custom03 grid place-content-center row-span-2 col-span-2">02</div>
<div className="p-4 rounded-lg bg-custom03 grid place-content-center row-span-3">03</div>
</div>
</div>
</div>
Answer Result & Refactor
Thanks to MagnusEffect's help I've been able solve the problem.
Ended up changing some of his styling and made sure to set the padding, gaps and other minor changes, hover its worked fantasticly!
You can use the flex properties to create the following grid systems like this.
<script src="https://cdn.tailwindcss.com"></script>
<div class=" border-red-600 border-2 p-3 flex flex-row items-center space-x-2">
<div class=" p-4 border-pink-500 border-2 h-screen space-y-2 items-center w-1/12 flex flex-col">
<div class=" border-purple-500 border-2 h-20 ">Av</div>
<div class=" border-purple-500 border-2 h-72">ot</div>
</div>
<div class=" p-4 border-pink-500 border-2 h-screen w-4/12 flex flex-col space-y-2">
<div class="p-3 border-purple-500 border-2 h-48">Video</div>
<div class="p-3 border-purple-500 border-2 h-24">Description</div>
<div class="p-3 border-purple-500 border-2 h-48">Github, Socials</div>
</div>
<div class=" p-4 border-pink-500 border-2 h-screen w-7/12 space-x-4 flex flex-row items-center">
<div class="flex flex-col space-y-2 w-1/2 h-full">
<div class="border-purple-500 border-2 h-full p-4">Projects</div>
<div class="border-purple-500 border-2 h-full p-4">Empty </div>
</div>
<div class="p-6 border-purple-500 border-2 w-1/2 h-full ">
Study/Work
</div>
</div>
</div>
Note:
Replace class= with className= in Reactjs.
View the demo in fullPage
Try h-screen or h-fit or you can add custom sizing h-[] in the square brackets you can add any sizing (px, vh) etc.

Grid of unequal heights in TailWindCSS

Is there a way to achieve a grid of unequal column heights (based on content in each div) using TailWindCSS ?
One way is to use flex to create a grid of unequal heights within the same column.
Please use full page view to see below code.
<script src="https://cdn.tailwindcss.com"></script>
<div class="container m-auto p-4">
<div class="flex flex-row space-x-4">
<!-- First Col -->
<div class="flex w-full flex-col space-y-4 bg-red-200">
<div class="h-40 rounded-lg bg-red-500"></div>
<div class="h-32 rounded-lg bg-red-500"></div>
<div class="h-60 rounded-lg bg-red-500"></div>
</div>
<!-- Sec Col -->
<div class="flex w-full flex-col space-y-4 bg-blue-200">
<div class="h-12 rounded-lg bg-blue-500"></div>
<div class="h-72 rounded-lg bg-blue-500"></div>
<div class="h-48 rounded-lg bg-blue-500"></div>
</div>
<!-- Third Col -->
<div class="flex w-full flex-col space-y-4 bg-green-200">
<div class="h-28 rounded-lg bg-green-500"></div>
<div class="h-40 rounded-lg bg-green-500"></div>
<div class="h-64 rounded-lg bg-green-500"></div>
</div>
</div>
</div>

How can I achieve 3 boxes on top of each other using Nextjs Tailwind?

I'm trying to add / style 3 boxes stacked on top of each other and then attach an image to it as such
https://i.stack.imgur.com/jVdIo.png
I was able to achieve this but it's leaning side ways
https://i.stack.imgur.com/fl0Zz.png
<div className='flex flex-col box-border rounded strok h-60 w-48 p-4 border-2 ...'>
<div className=' box-border rounded strok h-32 w-32 p-4 border-8 bg-black ...'>
<div className=' box-border rounded strok h-32 w-32 p-4 border-8 bg-black ...'>
<div className=' box-border rounded strok h-32 w-32 p-4 border-8 bg-black ...'></div>
</div>
</div>
</div>
You have all the boxes nested with padding. Since they're nested, they're considered children. The parents have padding. Consider having a parent with a position of relative and the 3 boxes having a position of absolute.
<div class="h-screen w-screen bg-gray-200 py-20 flex justify-center items-center sm:py-12">
<div class="h-40 w-40 relative">
<div class="rounded h-[150px] w-[150px] bg-red-700 absolute z-30 left-1">
</div>
<div class="rounded h-[150px] w-[134px] bg-slate-500 absolute z-20 top-3 left-3"></div>
<div class="rounded h-[142px] w-[118px] bg-black absolute z-10 top-8 left-5"></div>
I made an example for you on tailwind playground https://play.tailwindcss.com/MJnaFMVio6
This is how to implement that:
<!-- Background -->
<div class="mx-auto h-[500px] w-[500px] flex justify-center items-center bg-blue-500 relative p-[10px]">
<!-- Boxes container -->
<div class="relative">
<div class="relative mt-4 mx-4 h-full">
<!-- Boxes stacked behind -->
<div class="absolute -bottom-4 scale-[0.85] origin-bottom inset-x-0 h-full w-full bg-white/40 backdrop-blur-md rounded-2xl"></div>
<div class="absolute -bottom-2 scale-95 origin-bottom inset-x-0 h-full w-full bg-white/40 backdrop-blur-md rounded-3xl shadow-sm"></div>
<!-- Box on top -->
<div class="p-4 bg-white/60 backdrop-blur-md rounded-3xl h-full">
<img class="rounded-[20px]" src="https://variety.com/wp-content/uploads/2021/07/Rick-Astley-Never-Gonna-Give-You-Up.png">
</div>
</div>
</div>
</div>
On Tailwind play: https://play.tailwindcss.com/dmU2c4ai36

Vertical align with Tailwind CSS across full screen div

How can I vertically align a div with Tailwind?
What I want:
-----------------------------------
| |
| |
| |
| item1 |
| item2 |
| |
| |
| |
-----------------------------------
What I currently have:
-----------------------------------
| item1 |
| item2 |
| |
| |
| |
| |
| |
| |
-----------------------------------
HTML
<div class="flex flex-col h-screen my-auto items-center bgimg bg-cover">
<h3 class="text-white">heading</h3>
<button class="mt-2 bg-white text-black font-bold py-1 px-8 rounded m-2">
call to action
</button>
</div>
CSS
.bgimg {
background-image: url('https://d1ia71hq4oe7pn.cloudfront.net/photo/60021841-480px.jpg');
}
I have successfully centered on the secondary axis (left-right) with class items-center. Reading the documentation, I tried align-middle but it does not work. I have confirmed the divs have full height and my-auto.
I'm using this version of Tailwind: https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css
Here is a JSFiddle: https://jsfiddle.net/7xnghf1m/2/
You can also do
<div class="flex h-screen">
<div class="m-auto">
<h3>title</h3>
<button>button</button>
</div>
</div>
Partly referencing #mythicalcoder 's solution but using only the necessary classes provided by TailwindCss (Version 1.8.+):
flex : To use a flex-div as container
h-screen : To size the container-height to the viewport height.
justify-center : To justify center (horizontal center) - main axis - Doc
items-center : To align the items to center (horizontal center) - cross axis - Doc
My Solution to center two text lines:
<link href="https://unpkg.com/tailwindcss#^1.0/dist/tailwind.min.css" rel="stylesheet"/>
<div class="flex h-screen justify-center items-center">
<div class="text-center bg-blue-400"> <!-- ⬅️ THIS DIV WILL BE CENTERED -->
<h1 class="text-3xl">HEADING</h1>
<p class="text-xl">Sub text</p>
</div>
</div>
Justify-Center and Items-Center
While Anders' answer solves the problem for this particular case, I think it's important to note that using justify-center and items-center is circumstantial.
Let's have a look at one of the examples from the tailwind documentation.
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"/>
<div class="flex justify-center bg-gray-100">
<div class="text-gray-800 text-center bg-gray-300 px-4 py-2 m-2">1</div>
<div class="text-gray-800 text-center bg-gray-300 px-4 py-2 m-2">2</div>
<div class="text-gray-800 text-center bg-gray-300 px-4 py-2 m-2">3</div>
</div>
As we can see the above code centers the elements horizontally. The reason for this is because the justify-center class centers the element on the flex container's main axis.
This means that if we were to change the main axis to 'column' then we would get a different result.
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"/>
<div class="flex flex-col justify-center bg-gray-100">
<div class="text-gray-800 text-center bg-gray-300 px-4 py-2 m-2">1</div>
<div class="text-gray-800 text-center bg-gray-300 px-4 py-2 m-2">2</div>
<div class="text-gray-800 text-center bg-gray-300 px-4 py-2 m-2">3</div>
</div>
Justify-Center and Items-Center centers the elements on the main axis and the cross axis, and they can be used in place of each other. They are the opposites of each other and will produce different results depending on what the current main axis is.
According to the question, the "Items1", "Items2" should be both horizontally and vertically aligned.
Horizontal => text-center/justify-center
Vertical => items-center
Here is a sample code for producing a view similar to the ASCII image in the question.
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"/>
<div class="relative h-32 bg-blue-400">
<div class="absolute inset-0 flex items-center justify-center">
Item 1
<br>
Item 2
</div>
</div>
I have tried to summarize different occurances of centering the divs
Center divs in Column
For fit width
<div class="bg-yellow-400 flex flex-col h-screen justify-center items-center">
<div className="bg-green-500 p-2">item 1</div>
<div className="bg-pink-500 p-2">item 2</div>
</div>
For full width
<div class="bg-yellow-400 flex flex-col h-screen justify-center items-center">
<div className="bg-green-500 p-2 w-full flex justify-center">
item 1
</div>
<div className="bg-pink-500 p-2 w-full text-center">item 2</div>
</div>
Extra:
Center vertically split divs
<div class="flex h-screen flex-col">
<div class="flex flex-1 items-center justify-center bg-green-500 p-2 text-4xl">
<div class="bg-yellow-400 p-6">Item 1</div>
</div>
<div class="flex flex-1 items-center justify-center bg-pink-500 p-2 text-4xl"><div class="bg-amber-400 p-6">Item 2</div></div>
</div>
Center horizontally split divs
<div class="flex h-screen">
<div class="flex flex-1 items-center justify-center bg-green-500 p-2 text-4xl">
<div class="bg-yellow-400 p-6">Item 1</div>
</div>
<div class="flex flex-1 items-center justify-center bg-pink-500 p-2 text-4xl"><div class="bg-amber-400 p-6">Item 2</div></div>
</div>
Center divs in Row
For fit height
<div>
<div class="flex h-screen items-center justify-center bg-yellow-400">
<div class="flex justify-center bg-green-500 p-2">item 1</div>
<div class="bg-pink-500 p-2 text-center">item 2</div>
</div>
</div>
For full height
<div>
<div class="flex h-screen items-center justify-center bg-yellow-400">
<div class="flex h-full items-center bg-green-500 p-2">item 1</div>
<div class="flex h-full items-center bg-pink-500 p-2">item 2</div>
</div>
</div>
#bastiotutuama's answer is already good, however if there are other surrounding items then use align self utilities instead of items-center. source
<link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"/>
<div class="bg-blue-500 flex justify-center h-screen">
<div class="bg-red-300 self-start">
<h1>
Let us get you off the board <br>
<span>Pursue what you wanted</span>
</h1>
<div class="mt-2 flex items-center">
Get started
Learn more
</div>
</div>
<div class="bg-yellow-300 self-center">
<h1>
Let us get you off the board <br>
<span>Pursue what you wanted</span>
</h1>
<div class="mt-2 flex items-center">
Get started
Learn more
</div>
</div>
<div class="bg-red-300 self-end">
<h1>
Let us get you off the board <br>
<span>Pursue what you wanted</span>
</h1>
<div class="mt-2 flex items-center">
Get started
Learn more
</div>
</div>
</div>
You have another choice which is grid and can do
<div class="grid justify-items-center items-center h-screen">
<div>
<h3>title</h3>
<button>button</button>
</div>
</div>
Navigation using React
<nav className="flex gap-8 items-center justify-center">
<Link className=''>SHOP</Link>
<Link className=''>BACKPACK</Link>
</nav>
For vertical center in Tailwind Grid
use class name:
self-center
<div class="self-center">
you can do
<div class="flex justify-center items-center flex-col">
<p>Item 1</p>
<p>Item 2</p>
</div>
However, this approach requires flexbox.
I did it this way and it works.
<div class="grid grid-cols-3 h-screen">
<div class="bg-red-400 col-span-3 sm:col-span-1 flex">
<div class="bg-blue-300 m-auto">
<h1>hello</h1>
</div>
</div>
<div class="col-span-3 bg-red-50 sm:col-span-2"></div>
See image
Use class justify-center to align on the main axis. align-middle operates on the cross axis.

Resources