How to size column height properly in Tailwind? - html

I'm learning Tailwind (migrating from bootstrap) and I'm struggling to figure out how to appropriately size column heights. Here I have a 2-column layout:
<div id="app" class="bg-slate-200 h-screen p-4" data-v-app="">
<div class="w-full h-full">
<div class="columns-2">
<div class="rounded p-4 bg-white">
<h1 class="text-3xl">Box 1</h1>
<div class="columns-1 space-y-2">
<div class="w-full"><button class="rounded border border-green-400 p-2 text-green-600 w-full">Button 1</button></div>
<div class="w-full"><button class="rounded border border-green-400 p-2 text-green-600 w-full">Button 2</button></div>
</div>
</div>
<div class="rounded p-4 bg-white">
<h1 class="text-3xl">Box 2</h1>
<div class="columns-1 space-y-2">
<div class="w-full"><button class="rounded border border-green-400 p-2 text-green-600 w-full">Button 1</button></div>
<div class="w-full"><button class="rounded border border-green-400 p-2 text-green-600 w-full">Button 2</button></div>
</div>
</div>
</div>
</div>
</div>
This renders just fine when the content is equal height. However, when I make one of the columns shorter, like so:
<div id="app" class="bg-slate-200 h-screen p-4" data-v-app="">
<div class="w-full h-full">
<div class="columns-2">
<div class="rounded p-4 bg-white">
<h1 class="text-3xl">Box 1</h1>
<div class="columns-1 space-y-2">
<div class="w-full"><button class="rounded border border-green-400 p-2 text-green-600 w-full">Button 1</button></div>
<div class="w-full"><button class="rounded border border-green-400 p-2 text-green-600 w-full">Button 2</button></div>
</div>
</div>
<div class="rounded p-4 bg-white">
<h1 class="text-3xl">Box 2</h1>
<div class="columns-1 space-y-2">
<div class="w-full"><button class="rounded border border-green-400 p-2 text-green-600 w-full">Button 1</button></div>
</div>
</div>
</div>
</div>
</div>
I get the following result:
You can see some of the bottom padding for the first column is cut off by its parent not being tall enough, and there is some extra padding added on the top of the second column.
Here's what the box layout looks like in the inspector:
The expected behavior would be the parent 2-column element sizing its height to be tall enough so that it does not clip the taller column. The shorter column should also be top-aligned without that weird extra top margin.
Here's a live example, any help would be greatly appreciated! https://jsfiddle.net/xm9g4jfr/1/

The columns property is designed for flowing text into multiple columns so I wouldn't recommend using it in this case.
If you take out columns-2 and replace it with a grid layout, grid grid-cols-2 gap-x-6, that should give you what you are after.
https://play.tailwindcss.com/EDzOYOTSnU

You can do this for a larger screen
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet" />
<div class="container mx-auto bg-gray-300 p-6">
<div class="flex flex-col">
<div class="-mx-4 flex flex-col items-stretch lg:flex-row">
<div class="flex-1 p-4">
<div class="block h-full overflow-hidden rounded-lg border bg-white shadow-md transition ease-in-out sm:flex-row">
<div class="p-4 sm:ml-16 md:ml-32 lg:ml-0">
<!-- heading -->
<div class="-mx-2 flex flex-wrap sm:mx-auto sm:mb-2">
<div class="p-4">
<h1 class="text-3xl">Box 1</h1>
<div class="space-y-2">
<div ><button class="w-96 rounded border border-green-400 p-2 text-green-600">Button 1</button></div>
<div ><button class="w-96 rounded border border-green-400 p-2 text-green-600">Button 2</button></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="flex-1 p-4">
<div class="block h-full overflow-hidden rounded-lg border bg-white shadow-md transition ease-in-out hover:cursor-pointer sm:flex-row">
<div class="p-4 sm:ml-16 md:ml-32 lg:ml-0">
<!-- heading -->
<div class="-mx-2 flex flex-wrap sm:mx-auto sm:mb-2">
<div class="p-4">
<h1 class="text-3xl">Box 1</h1>
<div class="space-y-2">
<div ><button class="w-96 rounded border border-green-400 p-2 text-green-600">Button 1</button></div>
<div ><button class="w-96 rounded border border-green-400 p-2 text-green-600">Button 2</button></div>
<div ><button class="w-96 rounded border border-green-400 p-2 text-green-600">Button 1</button></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

Related

How to center the text within the divisions?

I want to center the text within the division so that the two text columns aren't right next to each other.
Original:
So that it's like this:
My code:
<div class="px-80 py-4 grid grid-cols-2 divide-x-4">
<div className="w/2 flex flex-col">
<div class="font-bold text-6xl text-center">+9.7%</div>
<p class="text-gray-700 text-4xl text-center">
Since Last Quarter
</p>
</div>
<div className="w/2 flex flex-col justify-center w-full h-full items-center">
<div class="font-bold text-6xl text-center">-11.3%</div>
<p class="text-gray-700 text-4xl text-center">YTD</p>
</div>
</div>
</div>```
Try removing the grid and remove the px-80 and replace it with flex and the following code:
<div class="flex p-10 divide-x-2">
<div class=" flex flex-col flex-1">
<div class="font-bold text-6xl text-center font-[roboto]">+9.7%</div>
<p class="text-gray-700 mt-2 text-2xl text-center font-serif">
Since Last Quarter
</p>
</div>
<div class=" flex flex-col justify-center flex-1 items-center">
<div class="font-bold text-6xl text-center font-[roboto]">-11.3%</div>
<p class="text-gray-700 mt-2 text-2xl text-center font-serif">YTD</p>
</div>
</div>
</div>
</div>
Output:
Tailwind-play

CSS (Tailwind) Grid height 100vh not working

currently im trying to create a Layout which fills the whole screen.
Thought its very easy but found out it doesn't work as expected.
Here is a Fiddle https://jsfiddle.net/s5ocg3v8/36/
<div class="h-screen grid grid-rows-[auto_1fr] p-5">
<div class="bg-slate-200 mb-1 p-2">Header</div>
<div class="grid grid-cols-[auto_1fr]">
<div class="bg-slate-200 mr-1 p-2">Navigation</div>
<div class="bg-slate-200 p-2 overflow-y-auto">
Container
<div class="border-2 border-black" style="height: 1000px;">
Content
</div>
</div>
</div>
</div>
if the content gets bigger than the container, the defined height is ignored.
i want the layout to always fill the whole screen but not more, if the content is bigger then it should show a scrollbar for the container.
i could set the header to a fixed height and then use calc(100%-headerHeight) but thats not really what i want
Just add min-h-0 to the second grid container.
<script src="https://cdn.tailwindcss.com"></script>
<div class="h-screen grid grid-rows-[auto_1fr] p-5">
<div class="bg-slate-200 mb-1 p-2">Header</div>
<div class="grid grid-cols-[auto_1fr] min-h-0">
<div class="bg-slate-200 mr-1 p-2">Navigation</div>
<div class="bg-slate-200 p-2 overflow-y-auto">
Container
<div class="border-2 border-black" style="height: 1000px;">
Content
</div>
</div>
</div>
</div>
Or always prefer minmax(0,1fr) instead of 1fr
<script src="https://cdn.tailwindcss.com"></script>
<div class="h-screen grid grid-rows-[auto_minmax(0,1fr)] p-5">
<div class="bg-slate-200 mb-1 p-2">Header</div>
<div class="grid grid-cols-[auto_1fr]">
<div class="bg-slate-200 mr-1 p-2">Navigation</div>
<div class="bg-slate-200 p-2 overflow-y-auto">
Container
<div class="border-2 border-black" style="height: 1000px;">
Content
</div>
</div>
</div>
</div>

CSS - Make a Sidebar Box Sticky

I'm using TailwindCSS in my project. My page looks like below:
<main class="max-w-7xl mx-auto mt-6 px-4">
<div class="flex flex-col md:space-x-12 justify-center md:flex-row">
<div class="w-full md:w-9/12 mb-8">
<h1 class="text-3xl font-bold mt-2 mb-4">Title</h1>
<p class="mb-4 uppercase text-gray-500">META DATA</p>
<div class="prose lg:prose-lg post">
<p class="bg-gray-200 p-5 h-36 m-3"></p>
<p class="bg-gray-200 p-5 h-36 m-3"></p>
<p class="bg-gray-200 p-5 h-36 m-3"></p>
<p class="bg-gray-200 p-5 h-36 m-3"></p>
<p class="bg-gray-200 p-5 h-36 m-3"></p>
<p class="bg-gray-200 p-5 h-36 m-3"></p>
<p class="bg-gray-200 p-5 h-36 m-3"></p>
<p class="bg-gray-200 p-5 h-36 m-3"></p>
</div>
<hr class="mt-5" />
</div>
<div class="w-full md:w-3/12">
<div class="mb-5 mx-auto" style="max-width:300px">
<div class="rounded rounded-lg border">
<p class="bg-blue-100 p-5 h-12 m-3"></p>
<p class="bg-blue-100 p-5 h-12 m-3"></p>
<p class="bg-blue-100 p-5 h-12 m-3"></p>
<p class="bg-blue-100 p-5 h-12 m-3"></p>
</div>
<div class="rounded rounded-lg border mt-10 sticky self-auto top-20">
<div class="p-20">Make this box sticky</div>
</div>
</div>
</div>
</div>
</main>
I want to make the bottom box in the right sidebar sticky but it's not happening. I've seen many threads and followed advice like make it flex-start, but it doesn't work in this layout. Can you please help?
You can see the playground here - https://play.tailwindcss.com/MaFc2vXQRe
Add display: unset; on the parent div of sticky div check below code.
<div class="mb-5 mx-auto" style="max-width:300px; display: unset;">
Let me know if you have to face any issue

Fixing left and right margin in bootstrap row inside card

I am not able to customize left and right margin for bootstrap row inside card. Here are my css code and some screenshots.
<div class="container" style="margin-bottom: -20px;">
<div class="row">
<div class="col-6">
<div class="card shadow bg-white pt-1 pb-1 rounded">
<div class="row" id="xyz">
<div class="col-4">
<p><b>1,933</b></p>
</div>
<div class="col-4">
<p><b>₹100</b></p>
</div>
<div class="col-4" style="white-space:nowrap">
<p><b>₹4,000</b></p>
</div>
</div>
<div class="row">
<div class="col-6">
<img class="mx-auto d-block card-img" src="../../assets/images/note_9_pro_max1.jpg" alt="note-pro-max" style="width: 50px; height: 100px;">
</div>
<div class="col-6" style="white-space:nowrap">
<p><b>₹1,000</b></p>
</div>
</div>
</div>
</div>
</div>
</div>
Add
pl-1 and pr-1 in card div and try...
<div class="card shadow bg-white pt-1 pb-1 pl-1 pr-1 rounded">
JSFIDDLE
A little bit late, but I found a working solution. You can set the "width" of the card to percentages.
<div class="card shadow bg-white pt-1 pb-1 rounded" style="width: 95% !important">

Tailwind css: align element bottom of parent div

<div class="grid grid-cols-5 gap-4">
<div class="overflow-hidden border hover:rounded-lg hover:shadow-lg justify-evenly">
<div class="flex px-3 pt-1 justify-end">
<a class="underline italic" href="desc.html" title="Description">More</a>
</div>
<a class="cursor-pointer" href="www.www.com">
<img src="img/img.JPG" />
</a>
<div class="flex w-full justify-evenly mt-1 mb-4">
<a href="www.www.com class="bg-green-700 hover:bg-green-800 text-white font-bold py-1 px-4 rounded">
Button
</a>
</div>
</div>
<div class="overflow-hidden border hover:rounded-lg hover:shadow-lg justify-evenly">
</div>
...
</div>
I have grid with multiple grid cells. Each cell contains image. Under image there is a button. Currently button is placed just under the image. However, heights of images are different. So those buttons are not aligned.
I wonder how to put these buttons at the bottom of each cell??
I tried to add align-bottom on button wrapper, but it doesn't help.
Just need to play around with h-# flex flex-col justify-*. See a working demo.
<div class="grid grid-cols-3">
<div class="col-span-1 h-96 flex flex-col justify-between">
<img src="https://homepages.cae.wisc.edu/~ece533/images/airplane.png" class="h-full bg-cover bg-center" />
<button class="w-24 px-4 py-2 bg-gray-200 rounded-lg">Button</button>
</div>
<div class="col-span-1 h-96 flex flex-col justify-between">
<img src="https://homepages.cae.wisc.edu/~ece533/images/arctichare.png" class="h-3/5 bg-cover bg-center" />
<button class="w-24 px-4 py-2 bg-gray-200 rounded-lg">Button</button>
</div>
<div class="col-span-1 h-96 flex flex-col justify-between">
<img src="https://homepages.cae.wisc.edu/~ece533/images/baboon.png" class="h-3/4 bg-cover bg-center" />
<button class="w-24 px-4 py-2 bg-gray-200 rounded-lg">Button</button>
</div>
</div>
I see the following changes:
The card div should be a flex column, with the classes flex and flex-col and justify-between. This one each child element will be vertically positioned so there is the same space between them
Wrap the Img and the more link in a div, this way the space between the img and the more won't be that large