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
Related
I'm using Tailwind CSS to make a grid. In the parent div, I'm using the classes space-x-1 and space-y-1 to automatically align the child divs.
This works well except the first element is slightly misaligned. The first element has a computed margin of 0, while every other element has a computed margin of 4px. When I remove the space-x-1 and space-y-1 from the parent div, the child divs have the same margins.
<!-- Tailwind 3 -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Body -->
<div class="grid grid-cols-5 space-x-1 space-y-1">
<div class="text-white text-center p-2 bg-green-700">wood 1</div>
<div class="text-white text-center p-2 bg-gray-700">ore 2</div>
<div class="text-white text-center p-2 bg-green-700">wood 3</div>
<div class="text-white text-center p-2 bg-purple-800">sheep 4</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 5</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 6</div>
<div class="text-white text-center p-2 bg-pink-700">desert 7</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 8</div>
<div class="text-white text-center p-2 bg-gray-700">ore 9</div>
<div class="text-white text-center p-2 bg-green-700">wood 10</div>
<div class="text-white text-center p-2 bg-purple-800">sheep 11</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 12</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 13</div>
<div class="text-white text-center p-2 bg-green-700">wood 14</div>
<div class="text-white text-center p-2 bg-gray-700">ore 15</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 16</div>
<div class="text-white text-center p-2 bg-purple-800">sheep 17</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 18</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 19</div>
</div>
Instead of using space-x-1 and space-y-1, you should use gap-1, which is meant to be used with grid and flex layouts. It adds a gap between your items without the unwanted side margins. You can also use gap-x-1 and gap-y-1 individually.
The space- classes are meant for horizontal or vertical layouts only. It adds space to all items except the first one, to prevent an unwanted space at the start (which is why your first item is misaligned).
If you use grid, i will sugest to use gap-x-1 and gap-y-1.
And if you want margin top right left and bottom, you can use mt-1 ml-1 mr-1 mb-1
<!-- Tailwind 3 -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Body -->
<div class="grid grid-cols-5 gap-x-1 gap-y-1 mt-1 ml-1 mr-1 mb-1">
<div class="text-white text-center p-2 bg-green-700">wood 1</div>
<div class="text-white text-center p-2 bg-gray-700">ore 2</div>
<div class="text-white text-center p-2 bg-green-700">wood 3</div>
<div class="text-white text-center p-2 bg-purple-800">sheep 4</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 5</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 6</div>
<div class="text-white text-center p-2 bg-pink-700">desert 7</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 8</div>
<div class="text-white text-center p-2 bg-gray-700">ore 9</div>
<div class="text-white text-center p-2 bg-green-700">wood 10</div>
<div class="text-white text-center p-2 bg-purple-800">sheep 11</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 12</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 13</div>
<div class="text-white text-center p-2 bg-green-700">wood 14</div>
<div class="text-white text-center p-2 bg-gray-700">ore 15</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 16</div>
<div class="text-white text-center p-2 bg-purple-800">sheep 17</div>
<div class="text-white text-center p-2 bg-yellow-500">wheat 18</div>
<div class="text-white text-center p-2 bg-yellow-900">brick 19</div>
</div>
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>
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>
I've tried isolating an issue for a few hours to no avail. I think an image is worth more than a few words so let's start with that.
And the code.
<section id="about-me" class="flex bg-neutral-300 text-neutral-900">
<div class="container mx-auto flex flex-col space-y-5 md:flex-row md:space-x-5 p-5 md:p-0 md:pt-5 md:pb-5">
<div class="flex flex-1 justify-center h-32 rounded-lg shadow-sm shadow-neutral-900">
<div class="flex flex-col">
<p>test</p>
</div>
</div>
<div class="flex flex-1 justify-center h-32 rounded-lg shadow-sm shadow-neutral-900">
<div class="flex flex-col">
<p>test</p>
</div>
</div>
<div class="flex flex-1 justify-center h-32 rounded-lg shadow-sm shadow-neutral-900">
<div class="flex flex-col">
<p>test</p>
</div>
</div>
</div>
</section>
As we can see, the first div inside the container has a weird position. I've tried removing a lot of classes, including padding and margin, but the first element stays in this position no matter what.
I really don't see what is my issue, so I kind of need extra pairs of eyes right now. Thanks.
Remove in in the container div this class space-y-5. And it would work like expected.
<section id="about-me" class="flex bg-neutral-300 text-neutral-900">
<div class="container mx-auto flex flex-col md:flex-row md:space-x-5 p-5 md:p-0 md:pt-5 md:pb-5">
<div class="flex flex-1 justify-center h-32 rounded-lg shadow-sm shadow-neutral-900">
<div class="flex flex-col">
<p>test</p>
</div>
</div>
<div class="flex flex-1 justify-center h-32 rounded-lg shadow-sm shadow-neutral-900">
<div class="flex flex-col">
<p>test</p>
</div>
</div>
<div class="flex flex-1 justify-center h-32 rounded-lg shadow-sm shadow-neutral-900">
<div class="flex flex-col">
<p>test</p>
</div>
</div>
</div>
</section>
I want the 'title' to start right above 'first yeah', the image should also start there
I've tried several ways (and with some classes) but I haven't succeeded
The card code:
<div class="card border-dark">
<div class="card-body" style="background-color: #706747;">
<h1 class="card-title">
<p class="text-body">
<img src="/assets/logo.gif" alt="" width="60" height="48" class="d-inline-block">
{{cosos}} title
</p>
</h1>
<br>
<div class="d-flex flex-nowrap bd-highlight justify-content-center">
<div class="order-1 p-2 bd-highlight">First flex item</div>
<div class="order-2 p-2 bd-highlight">Second</div>
<div class="order-3 p-2 bd-highlight">Third</div>
<div class="order-4 p-2 bd-highlight">Final yup</div>
</div>
</div>
</div>
I'm using Bootstrap 5 for everything, I have no classes or styles of my own
Step 1: remove class justify-content-center and add p-2 after class card-title. Now you will see your logo.gif and title will align with first nav bar item.
Step 2: Add place-self: center; to the div with class card-body.
Step 3: Assign background-color: #706747; to the class card, remove from card-body div.
The updated HTML will be like this:
<div class="card border-dark" style="background-color: #706747;">
<div class="card-body" style="place-self: center;">
<h1 class="card-title p-2">
<p class="text-body">
<img src="/assets/logo.gif" alt="" width="60" height="48" class="d-inline-block"> {{cosos}} title
</p>
</h1>
<div class="d-flex flex-nowrap bd-highlight ">
<div class="order-1 p-2 bd-highlight">First flex item</div>
<div class="order-2 p-2 bd-highlight">Second</div>
<div class="order-3 p-2 bd-highlight">Third</div>
<div class="order-4 p-2 bd-highlight">Final yup</div>
</div>
</div>
</div>
You can make use of the grid-layout of bootstrap to align the elements.
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="card border-dark">
<div class="card-body" style="background-color: #706747;">
<h1 class="card-title">
<p class="text-body">
<div class="row">
<div class="col-3">
</div>
<div class="col-2">
<img src="/assets/logo.gif" alt="" width="60" height="48" >
</div>
<div class="col-7">
<span id="title1"> fdasdstitle</span>
</div>
</div>
</p>
</h1>
<br>
<div class="d-flex flex-nowrap bd-highlight justify-content-center">
<div class="order-1 p-2 bd-highlight">First flex item</div>
<div class="order-2 p-2 bd-highlight">Second</div>
<div class="order-3 p-2 bd-highlight">Third</div>
<div class="order-4 p-2 bd-highlight">Final yup</div>
</div>
</div>
</div>
</body>
</html>