Considering the requirements:
No JS or anything but CSS
"max-height" either on the father (white box) or the blue box won't work because the yellow one will change its height accordingly with its contents
both boxes will be changing their contents but I want the yellow box to be the height anchor
I need to make the blue box the same height as the yellow one, the yellow one will be changing its height frequently, and the blue one needs to follow the height and add scrollbar when needed
<div class="flex min-h-screen items-center justify-center bg-slate-200">
<div class="flex w-full max-w-xl items-start gap-4 bg-white p-4">
<div class="flex flex-grow flex-col gap-2 overflow-auto bg-sky-400 p-4">
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
</div>
<div class="flex flex-col gap-4 bg-yellow-500 p-4">
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
</div>
</div>
</div>
result image
Obs:
I'm using https://play.tailwindcss.com/ to keep it simple
If there's no solution I will manage to use the "max-height" property, that's why you don't need to waste your time doing any logic to fix this, I just wanna know if it's possible with css only
Assuming we're allowed to add an extra div, you can wrap the first flex child (the blue one), and add basis-0, and grow to it, like this:
<div class="flex min-h-screen items-center justify-center bg-slate-200">
<div class="flex w-full max-w-xl gap-4 bg-white p-4">
<div class="flex flex-grow flex-col">
<div class="flex grow basis-0 flex-col gap-2 overflow-auto bg-sky-400 p-4">
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
</div>
</div>
<div class="flex flex-col gap-4 bg-yellow-500 p-4">
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
<div class="bg-black/40 p-4"></div>
</div>
</div>
</div>
<script src="https://cdn.tailwindcss.com"></script>
You can try to change the height of the yellow box to see the effects.
Related
I want to have a flexbox layout where three columns have the same width.
I have to use Tailwind in this case, but it doesn't change the CSS theory... which, as far as I know, says I need to give the three columns the same basis, and then let them grow freely (all of them will grow the same proportion because they have the same basis).
But it's not working. One of the column gets bigger and I don't know why.
Note: I added a border just to make it visually clear, but I don't need that.
.just-a-border {
border: 2px dotted purple;
}
<link href="https://unpkg.com/tailwindcss#2.2.19/dist/tailwind.min.css" rel="stylesheet" />
<div class="grow flex flex-nowrap items-center gap-5 justify-center py-12 flex-row">
<div class="just-a-border h-full p-6 text-center basis-1 grow">
<div class="mb-2 text-sm font-medium uppercase">I want to have more words</div>
<div class="font-medium">6 hours</div>
</div>
<div class="just-a-border h-full p-6 text-center basis-1 grow">
<div class="mb-2 text-sm font-medium uppercase">Yes</div>
<div class="font-medium">1 hour</div>
</div>
<div class="just-a-border h-full p-6 text-center basis-1 grow">
<div class="mb-2 text-sm font-medium uppercase">No</div>
<div class="font-medium">3 hours</div>
</div>
</div>
Any idea to make it work?
You used the classes from Tailwind CSS v3. So, if you upgrade your Tailwind CSS version to v3, your codes will be just working good.
If you want to keep the current version which is v2, you just need to update a few of class names in your current codes like below. basis-1 to flex-1, grow to flex-grow
<style>
.just-a-border {
border: 2px dotted purple;
}
</style>
<div class="flex flex-row flex-nowrap items-center gap-5 justify-center py-12">
<div class="just-a-border h-full p-6 text-center flex-1 flex-grow">
<div class="mb-2 text-sm font-medium uppercase">I want to have more words</div>
<div class="font-medium">6 hours</div>
</div>
<div class="just-a-border h-full p-6 text-center flex-1 flex-grow">
<div class="mb-2 text-sm font-medium uppercase">Yes</div>
<div class="font-medium">1 hour</div>
</div>
<div class="just-a-border h-full p-6 text-center flex-1 flex-grow">
<div class="mb-2 text-sm font-medium uppercase">No</div>
<div class="font-medium">3 hours</div>
</div>
</div>
It appears you are using Tailwind classes from version 3 while you are using version 2.2.19.
I added the following classes to your css and it now gives even columns.
.grow {
flex-grow: 1;
}
.basis-1 {
flex-basis: 0.25rem;
}
Also see the Play demo which uses your code, unchanged, and works as you would have expected.
https://play.tailwindcss.com/VjbHZcTiVH
.just-a-border {
border: 2px dotted purple;
}
.grow {
flex-grow: 1;
}
.basis-1 {
flex-basis: 0.25rem;
}
<link href="https://unpkg.com/tailwindcss#2.2.19/dist/tailwind.min.css" rel="stylesheet" />
<div class="grow flex flex-nowrap items-center gap-5 justify-center py-12 flex-row">
<div class="just-a-border h-full p-6 text-center basis-1 grow">
<div class="mb-2 text-sm font-medium uppercase">I want to have more words</div>
<div class="font-medium">6 hours</div>
</div>
<div class="just-a-border h-full p-6 text-center basis-1 grow">
<div class="mb-2 text-sm font-medium uppercase">Yes</div>
<div class="font-medium">1 hour</div>
</div>
<div class="just-a-border h-full p-6 text-center basis-1 grow">
<div class="mb-2 text-sm font-medium uppercase">No</div>
<div class="font-medium">3 hours</div>
</div>
</div>
You can use grid for that, instead of flex. Defining the number of columns with grid-cols-3 will create three columns of equal width and height.
See playground example and tailwind docs.
<div class="grid w-full grid-cols-3 flex-row justify-center gap-5 py-12">
<div class="rounded-lg border border-slate-300 p-6 text-center">
<div class="mb-2 text-sm font-medium uppercase">I want to have more words</div>
<div class="font-medium">6 hours</div>
</div>
<div class="rounded-lg border border-slate-300 p-6 text-center">
<div class="mb-2 text-sm font-medium uppercase">Yes</div>
<div class="font-medium">1 hour</div>
</div>
<div class="rounded-lg border border-slate-300 p-6 text-center">
<div class="mb-2 text-sm font-medium uppercase">No</div>
<div class="font-medium">3 hours</div>
</div>
</div>
How can I make the second column use the minimum amount of space needed?
https://play.tailwindcss.com/tYdbqCSfoh?size=540x720
I have this:
But I want this instead:
<div class="min-h-screen bg-gray-50 py-6 flex flex-col justify-center relative overflow-hidden sm:py-12">
<div class="relative bg-gray-600">
<div class="max-w-full m-2">
<div class="grid gap-2 grid-flow-col bg-blue-300">
<div class="bg-green-200">sdf</div>
<div class="bg-red-200">sdf</div>
</div>
</div>
</div>
</div>
Just add grid-cols-1
<script src="https://cdn.tailwindcss.com"></script>
<div class="min-h-screen bg-gray-50 py-6 flex flex-col justify-center relative overflow-hidden sm:py-12">
<div class="relative bg-gray-600">
<div class="max-w-full m-2">
<div class="grid grid-cols-1 gap-2 grid-flow-col bg-blue-300">
<div class="bg-green-200">sdf</div>
<div class="bg-red-200">sdf</div>
</div>
</div>
</div>
</div>
I am making div based tables following this:
https://tailwindcss.com/docs/display#table
Here is my code:
<div class="container px-4 mx-auto my-8">
<div class="table w-full bg-white rounded shadow table-responsive text-center">
<div class="table-header-group bg-gray-50">
<div class="border-b border-gray-900 table-row">
<div class="table-cell p-3 text-sm font-bold uppercase">Title</div>
<div class="table-cell p-3 text-sm font-bold uppercase">Artist</div>
<div class="table-cell p-3 text-sm font-bold uppercase">Year</div>
</div>
</div>
<div class="table-row-group divide-y divide-gray-700">
<div class="table-row border-t border-gray-400 group hover:bg-gray-100">
<div class="table-cell p-3">Chocolate Starfish And The Hot Dog Flavored Water</div>
<div class="table-cell p-3">Limp Bizkit</div>
<div class="table-cell p-3">2000</div>
</div>
<div class="table-row border-t border-gray-400 group hover:bg-gray-100">
<div class="table-cell p-3">Significant Other</div>
<div class="table-cell p-3">Limp Bizkit</div>
<div class="table-cell p-3">1999</div>
</div>
<div class="table-row border-t border-gray-400 group hover:bg-gray-100">
<div class="table-cell p-3">Three Dollar Bill, Y’all $</div>
<div class="table-cell p-3">Limp Bizkit</div>
<div class="table-cell p-3">1997</div>
</div>
</div>
</div>
</div>
If you apply the border-t border-gray-400 group hover:bg-gray-100 to a standard table tr this works fine. Trying to dig through the Tailwind CSS but nothing obvious to me what is overriding this and hiding the top border from displaying. I even tried a divide-y divide-gray-700 trick on the sudo-tbody div and that didn't work either.
Just add .border-collapse to your .table.
How do we do gutters in Tailwind CSS?
My attempt with gap-4 below:
<div class="flex flex-col justify-center items-center h-64 bg-green-100 gap-4">
<div class="flex flex-wrap justify-center items-center gap-4 w-full">
<div class="bg-green-500 w-1/2 text-center">1</div>
<div class="bg-green-500 w-1/2 text-center">1</div>
<div class="bg-green-500 w-1/2 text-center">1</div>
<div class="bg-green-500 w-1/2 text-center">1</div>
<div class="bg-green-500 w-1/2 text-center">1</div>
</div>
</div>
But the result is not what I am trying to achieve as the gutters in Foundation:
<div class="grid-x grid-margin-x">
<div class="cell medium-6 large-4">12/6/4 cells</div>
<div class="cell medium-6 large-8">12/6/8 cells</div>
</div>
<div class="grid-x grid-padding-x">
<div class="cell medium-6 large-4">12/6/4 cells</div>
<div class="cell medium-6 large-8">12/6/8 cells</div>
</div>
Is it gaps that I should look for in TW? Or something else?
Use space-x-<n> and/or space-y-<n> (Space Between) for flexbox.
Use gap-<n> (Gap) for grids.
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet">
<div class="flex flex-row space-x-4 bg-red-200">
<div class="bg-red-400 p-4 flex-grow">Flex</div>
<div class="bg-red-400 p-4 flex-grow">Flex</div>
<div class="bg-red-400 p-4 flex-grow">Flex</div>
<div class="bg-red-400 p-4 flex-grow">Flex</div>
</div>
<br>
<div class="grid grid-flow-row grid-cols-4 gap-4 bg-blue-200">
<div class="bg-blue-400 p-4">Grid</div>
<div class="bg-blue-400 p-4">Grid</div>
<div class="bg-blue-400 p-4">Grid</div>
<div class="bg-blue-400 p-4">Grid</div>
</div>
What i want is the whole page to be static and division with paragraph to be scrollable. At the moment main page is also scrollable because of masthead. How to prevent this?
Code snippet:
<main class="flex flex-col h-screen">
<div class="flex bg-blue-300 h-32 ">Masthead</div>
<div class="flex flex-row h-full">
<div class="bg-gray-100 w-32 p-4">Sidebar</div>
<div class="flex flex-col bg-white flex-grow">
<div class="bg-gray-300 h-16 p-4">
<p>Header</p>
</div>
<div class="paragraph overflow-y-auto px-4">
#for($i=1; $i < 200; $i++)
<p>paragraph</p>
#endfor
</div>
</div>
</div>
</main>
I have always preferred to use flex flex-col flex-1 combination to achieve such layout. Checkout Tailwind Layout I created for better understanding.
<main class="flex flex-col h-screen">
<div class="flex h-32 bg-blue-300">Masthead</div>
<div class="flex flex-1 overflow-hidden">
<div class="flex bg-gray-100 w-32 p-4">Sidebar</div>
<div class="flex flex-1 flex-col">
<div class="flex bg-gray-300 h-16 p-4">Header</div>
<div class="flex flex-1 bg-blue-300 overflow-y-auto paragraph px-4">
#for($i=1; $i < 200; $i++)
<p>paragraph</p>
#endfor
</div>
</div>
</div>
<div class="flex">Footer</div>
</main>