Tailwind (CSS Grid) Same sizes for every Grid Element - html

I want to write a Formular with some input fields. I use Tailwind 2 and the new Grid System.
I want to have a 3 Column Formular (grid-cols-3)
On the second row I have a col-span-2 to have a row over 2 Colums because I want place a bigger textarea there.
The labels an input sizes a dfined with w-1/2 every time. When I have a colspan 2 I thought I have simple to change the width from the label to w-1/4 and the textbox to w-3/4 so the label must have the same size than all other only the input must have a bigger size.
What am I doing wrong? The label from the w-1/4 is 2 pixel bigger than the other one.
Here I have a link to the code example. https://play.tailwindcss.com/IqYsmfxpwJ
<div class="">
<h1>InfoBox</h1>
<div class="grid grid-cols-3 gap-2">
<div class="">
<lable class="inline-block w-1/2">One</lable><input class="w-1/2 border">
</div>
<div class="">
<lable class="inline-block w-1/2">Two</lable><input class="w-1/2 border">
</div>
<div class="">
<lable class="inline-block w-1/2">Tree</lable><input class="w-1/2 border">
</div>
<div class="col-span-2">
<lable class="inline-block w-1/4">Four</lable><input class="w-3/4 border">
</div>
<div class="">
<lable class="inline-block w-1/2">Five</lable><input class="w-1/2 border">
</div>
</div>
</div>
`

Of course the width wouldn't remain the same. When you combine two grid, the gap between gets included in the new bigger section. therefor the width of this section would be twice of the smaller ones plus the gap between them. As the result, the width of label would become bigger than the original ones.
Maybe something like this would work better for you:
<div class="">
<h1>InfoBox</h1>
<div class="grid grid-cols-3 gap-2">
<div class="flex">
<lable class="inline-block w-20 flex-none">One</lable><input class="w-full border">
</div>
<div class="flex">
<lable class="inline-block w-20 flex-none">Two</lable><input class="w-full border">
</div>
<div class="flex">
<lable class="inline-block w-20 flex-none">Tree</lable><input class="w-full border">
</div>
<div class="flex col-span-2">
<lable class="inline-block w-20 flex-none">Four</lable><input class="w-full border">
</div>
<div class="flex">
<lable class="inline-block w-20 flex-none">Five</lable><input class="w-full border">
</div>
</div>
</div>

Related

How to set a separator bigger than its container with CSS

I have the following form with tailwind. I want the red separator to cover the full width of the outer container. So I would need it to start 24px before the left margin (that's the -ml-6) and to end 24px after (it should be 48px larger)
I know I could just let each section set its own horizontal padding (px-6), but I'd like to have a general padding for all the sections (that's the px-6) and the separators between sections to ignore that padding.
How can I achieve it?
// deal with the CDN warning
setTimeout(() => {console.clear()});
<div class="grid h-screen w-screen content-center justify-center">
<div class="grid w-80 gap-4 rounded-lg bg-slate-50 p-4 px-6 shadow-lg">
<div class="grid gap-2 bg-slate-200">
<div class="text-lg">Section 1</div>
<div class="text-sm">Item1 1</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 2</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 3</div>
</div>
<div class="-ml-6 mr-0 w-full border-t-2 border-red-400"></div>
<div class="grid gap-2 bg-slate-200">
<div class="text-lg">Section 2</div>
<div class="text-sm">Item1 1</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 2</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 3</div>
</div>
</div>
</div>
<script src="https://cdn.tailwindcss.com"></script>
Note: here you have the tailwind playground example
The problem is w-full (width:100%). You just have to remove that class (set width to the default "auto").
I think, when you specify the width:100% the parent padding is taken into account when calculating the width.
// deal with the CDN warning
setTimeout(() => {
console.clear()
});
<div class="grid h-screen w-screen content-center justify-center">
<div class="rounded-lg bg-slate-50 shadow-lg">
<div class="grid w-80 gap-4 rounded-lg bg-slate-50 p-4 px-6 shadow-lg">
<div class="grid gap-2 bg-slate-200">
<div class="text-lg">Section 1</div>
<div class="text-sm">Item1 1</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 2</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 3</div>
</div>
<div class="-mx-6 border-t-2 border-red-400"></div>
<div class="grid gap-2 bg-slate-200">
<div class="text-lg">Section 2</div>
<div class="text-sm">Item1 1</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 2</div>
<div class="w-full border-t"></div>
<div class="text-sm">Item1 3</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.tailwindcss.com"></script>
You've asked a bit of an XY question. Rather than hammering extra markup to size, just refactor so you can put your border on an existing element. You said you'd rather have just one padding class, but that requires you to have extra markup just for styling. That's not good practice.
Chances are you can do the same for your interior elements. You shouldn't need extra divs just for a line. Overall, your markup ends up much cleaner.
// deal with the CDN warning
setTimeout(() => {
console.clear()
});
border-t-2 border-red-400
<div class="grid h-screen w-screen content-center justify-center">
<div class="rounded-lg bg-slate-50 shadow-lg">
<div class="grid w-80 gap-4 ">
<div class="grid w-80 p-4 px-6">
<div class="grid gap-2 pb-2 bg-slate-200">
<div class="text-lg">Section 1</div>
<div class="text-sm">Item1 1</div>
<div class="text-sm pt-2 border-t border-gray-400">Item1 2</div>
<div class="text-sm pt-2 border-t border-gray-400">Item1 3</div>
</div>
</div>
</div>
<div class="grid w-80 border-t-2 border-red-400">
<div class="grid w-80 p-4 px-6">
<div class="grid gap-2 pb-2 bg-slate-200">
<div class="text-lg">Section 2</div>
<div class="text-sm">Item1 1</div>
<div class="text-sm pt-2 border-t border-gray-400">Item1 2</div>
<div class="text-sm pt-2 border-t border-gray-400">Item1 3</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.tailwindcss.com"></script>
The solution I found so far is to use -ml-6 to make the separator start 24 px before, and use calc to enlarge the width 48px, like this:
<div class="-ml-6 w-[calc(100%+48px)] border-t-2 border-red-400"></div>
see tailwind playground

Responsive Curve line between elements

I'm currently working on a project that has two columns with text, headings and buttons. I would like to keep the design responsive, but when our client adds more text to "Step 3" the SVG doesn't line up correctly.
My thoughts
I would like to remove the SVG and set "anchor" points to the paragraph text boxes and create a curved dotted line between those anchor points.
The question is:
How do I achieve this?
Click to view image
I tried to use an SVG and scale it with the content, but this didn't work as expected.
After watching different solutions on forums, I didn't succeed in finding a solution.
This is the structure of the bock seen on the screenshot.
I am using Tailwindcss for my styling.
<section class="container mx-auto background mb-52">
<div class="flex flex-row">
<div class="flex flex-col justify-center relative max-w-[534px]">
<img class=" max-w-[534px] max-h-[575px]" src="https://via.placeholder.com/534x575.png" alt="">
</div>
<div class="ml-28 flex flex-row">
<div class="max-w-[315px]">
<!-- Item 1 -->
<div class="text-left p-3">
<div class="">
<h1 class="text-xl py-2"><span class="font-bold">1. </span>TITLE</h1>
<p class="font-roboto font-light">
text
</p>
</div>
<div class="py-4 flex flex-wrap">
Online strategie
Vindbaarheid
Webdesign
</div>
</div>
<!-- Item 3 -->
<div class="text-left p-3 ">
<div class="">
<h1 class="text-xl py-2"><span class="font-bold">3. </span>TITLE</h1>
<p class="font-roboto font-light">
Text
</p>
</div>
<div class="py-4 flex flex-wrap">
Klantportalen
</div>
</div>
<!-- Item 5 -->
<div class="text-left p-3 ">
<div class="">
<h1 class="text-xl py-2"><span class="font-bold">5. </span>TITLE</h1>
<p class="font-roboto font-light">
Text
</p>
</div>
<div class="py-4 flex flex-wrap">
Conversie optimalisatie
</div>
</div>
</div>
<!-- Col IMG -->
<div class="my-auto max-h-[764px] max-w-[100px] flex flex-col justify-center">
<img class="w-full" src="{{ asset('storage/images/curvy-line.svg') }}" alt="">
</div>
<div class="my-auto max-w-[315px]">
<!-- Item 2 -->
<div class="text-left p-3 ">
<div class="">
<h1 class="text-xl py-2"><span class="font-bold">2. </span>TITLE</h1>
<p class="font-roboto font-light">
Text
</p>
</div>
<div class="py-4 flex flex-wrap">
Koppelingen
E-Commerce
Productconfiguratoren
</div>
</div>
<!-- Item 4 -->
<div class="text-left p-3 ">
<div class="">
<h1 class="text-xl py-2"><span class="font-bold">4. </span>TITLE</h1>
<p class="font-roboto font-light">
Text
</p>
</div>
<div class="py-4 flex flex-wrap">
Bedrijfapps
Dashboarding
</div>
</div>
</div>
</div>
</div>
</section>

How to size column height properly in Tailwind?

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>

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>

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