I am trying to achieve a grid layout using Tailwind, but I have troubles of implementing it.
I thought at first to use flex but I think that would be difficult to achieve.
So far I am using grid grid-cols-3 gap-4 and it looks like the picture below, but I want the chart status to be below the X-as and Lijen, bottom to be databak, algemeen and data/filters.
<div className="h-full grid grid-flow-col-dense col-span-3 gap-4">
<div className="row-span-3">
1
</div>
<div className="col-span-2">
2
</div>
<div className="row-span-2 h-full col-span-2">
3
</div>
</div>
I cannot make the 3 element get the remaining height..
Take a look at this: https://tailwindcss.com/docs/grid-column
Basically, you want to span your cols.
<div class="grid grid-cols-3 gap-4">
<div class="...">01</div>
<div class="...">02</div>
<div class="...">03</div>
<div class="col-span-2 ...">04</div>
<div class="...">05</div>
<div class="...">06</div>
<div class="col-span-2 ...">07</div>
</div>
And you can do the same for the rows, so you can get Chart status in the same col under leien.
<div class="grid grid-rows-3 grid-flow-col gap-4">
<div class="row-span-3 ...">01</div>
<div class="col-span-2 ...">02</div>
<div class="row-span-2 col-span-2 ...">03</div>
</div>
Have you tried to just change the order of the items in the HTML?
If you're using for example a 12x12 grid you could also specify for each items the exact row/column you want it to span:
.grid-item-1 {
grid-column:2/4;
}
You could also try flex - it does have the "order" property:
.flex-item-1 {
order: 1;
}
It's not such a complex layout so you could try flex too.
Related
I have a grid layout set-up with windicss with this code
<div class="grid grid-cols-2 w-auto">
<div class="w-15">
<img class="h-15" src="https://starnumber.tk/logo.png" alt="logo">
</div>
<div>
<p class="text-left font-mono"><span class="text-xl">StarNumber12046</span><br>Svelte lover</p>
</div>
but the div takes too much space
Apparently, setting the display to an inline-flex fixes the problem.
I am trying to have the same space (and configurable) before and after the divider line. However I cannot seem to get the spacing after the divider line.
<div class="grid grid-cols-1 divide-y divide-solid">
<div class="pb-5">Hello</div>
<div class="pb-5">World</div>
<div class="pb-5">hi</div>
</div>
It looks like this:
How can I get the same spacing before and after the divider line? Ideally without needing to touch the children styles (i.e. remove the pb-#)?
Try this code,
<div class="grid grid-cols-1 divide-y divide-solid bg-gray-500">
<div class="py-2 first:pt-0">Hello</div>
<div class="py-2">World</div>
<div class=" py-2 last:pb-0">hi</div>
</div>
credits: #brc-dd sample
I would like to create a grid with tailwind css where the first column is very narrow and the second one is very wide. Normally I find the tailwind docs very intuitive but I'm not understanding this one. Using grid-cols-{n} I can create equally sized columns but I don't understand how to make differently sized columns. How can I go about this?
If you want create columns with different widths then it will be an implicitly-created grid.
<div class="p-5 bg-slate-200">
<div class="grid grid-flow-col auto-cols-max gap-x-5">
<div class="bg-white w-20">Hello 1</div>
<div class="bg-white w-40">Hello 2</div>
<div class="bg-white">Hello 3</div>
</div>
</div>
https://play.tailwindcss.com/7XjBZDzwml
This is the relevant documentation here:
https://tailwindcss.com/docs/grid-auto-columns
And it explains how you can customize your theme if needed too.
Here's an example of a 2 column grid where the first column is narrow, and the second one is not:
<div class="grid grid-cols-[max-content_1fr] gap-x-4">
<div class="bg-red-200 p-4">Column 1</div>
<div class="bg-green-200 p-4">Column 2</div>
</div>
Tailwind play: https://play.tailwindcss.com/FTLi83L5zI
I'll assume that's not what you want as it's not 100% clear from your question. So here's a different example using a 12 column grid, you can span multiple columns to achieve something along the lines of what you are asking for:
<div class="grid grid-cols-12 gap-x-4 text-center">
<div class="bg-slate-400 p-4">Col</div>
<div class="bg-slate-400 p-4 col-span-2">Col</div>
<div class="bg-slate-400 p-4">Col</div>
<div class="bg-slate-400 p-4">Col</div>
<div class="bg-slate-400 p-4">Col</div>
<div class="bg-slate-400 p-4 col-span-4">Col</div>
<div class="bg-slate-400 p-4">Col</div>
<div class="bg-slate-400 p-4">Col</div>
</div>
Tailwind play: https://play.tailwindcss.com/XtUOu2cDD9
Finally, you could also do this:
<div class="grid grid-cols-12 gap-x-4 text-center">
<div class="bg-slate-400 p-4 col-span-4">Col</div>
<div class="bg-slate-400 p-4 col-span-8">Col</div>
</div>
Tailwind play: https://play.tailwindcss.com/bAnPhCdNfc
In the end the way I did it was:
grid grid-cols-[2rem,8fr]
Which creates a grid with 2 columns one, the first one 2rems wide the second one being 8fr wide
From the official documentation, I am only able to come up with something like this:
<div class="grid grid-cols-2 gap-3">
<div>1st col</div>
<div>2nd col</div>
</div>
But this gives me 2 columns with an equal width - how do I specify that the first column would be like 20% of the total width (I only need to place there a simple icon) and the rest of the width would be the second column (here would be a text)?
Thank you in advance.
Set grid-cols-5 to the wrapper and col-span-4 to second column. It will cover 4/5 (80%)
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.1.2/tailwind.min.css" />
<div class="grid grid-cols-5 gap-3">
<div class="bg-blue-100">1st col</div>
<div class="bg-red-100 col-span-4">2nd col</div>
</div>
Another way with grid-flow-col
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.1.2/tailwind.min.css" />
<div class="grid grid-flow-col gap-3">
<div class="bg-blue-100 col-span-1">1st col</div>
<div class="bg-red-100 col-span-4">2nd col</div>
</div>
You can define additional utilities by extending the theme in tailwind.config.js:
module.exports = {
theme: {
extend: {
gridTemplateColumns:
{
'20/80': '20% 80%',
'fixed': '40px 260px',
}
}
}
}
<div class="grid grid-cols-20/80 w-full h-64">
<div class="bg-blue-500"></div>
<div class="bg-red-500"></div>
</div>
<div class="grid grid-cols-fixed h-64">
<div class="bg-blue-500"></div>
<div class="bg-red-500"></div>
</div>
Updated for Tailwind CSS 3:
With the introduction of the JIT compiler and the ability to use arbitrary/dynamic values in some utilities, you can now do this without the config:
<div class="grid grid-cols-[20%_80%] w-full h-64">
<div class="grid grid-cols-[40px_260px] w-full h-64">
You can use col-span like below
<div class="grid grid-cols-5 gap-3"> // This will create 5 grids so 20% each
<div class="some-class"></div>
<div class="col-span-4"></div> // This will take 80% of space
</div>
Reference: https://tailwindcss.com/docs/grid-column#class-reference
Simple
<div class="flex flex-wrap">
<div class="w-1/5"> 20% </div>
<div class="w-4/5"> 80% </div>
</div>
https://v1.tailwindcss.com/components/flexbox-grids
further to #horuskol's solution for column templates, % doesn't respect the container dimensions so if you use gap then the grid will overflow it's container
if instead of % you use fr then the grid contents will be styled flexibly and so not overflow the container
eg.
grid-cols-[1fr_2fr] gives 2 cols at 33% / 66%
grid-cols-[1fr_2fr_1fr] gives 3 cols at 25% / 50% / 25%
I am using Tailwind CSS and am trying to build a grid that certain cell contents span multiple cells while other items stay in the single cell. In the code below, I try to show what I am trying to accomplish. I know my col-span-2 class is not doing anything, but I put it there to indicate what I wish I could do. I want all cells to be the same width (I just want some cell content to span over the dividers). Do I need some sort of overlay logic?
Any help is appreciated.
Thanks!
<div id=container class="m-auto p-2">
<div id=theGrid class="grid grid-cols-3 divide-x divide-gray-600">
<div id=cell1 class="px-4 h-24">
Cell 1<br />
<div id=thingThatSpansTwoCells class="h-4 bg-green-700 text-white col-span-2">Spans 2 Cells</div>
</div>
<div id=cell2 class="px-4 h-24">Cell 2</div>
<div id=cell3 class="px-4 h-24">Cell 3</div>
</div>
</div>
You will need to write your own CSS for this. To span 2 column, use width:200% and don't forget to account for the padding you are using:
.w-200 {
width:calc(200% + 3rem);
}
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet">
<div id=container class="m-auto p-2">
<div id=theGrid class="grid grid-cols-3 divide-x divide-gray-600">
<div id=cell1 class="px-4 h-24">
Cell 1<br />
<div id=thingThatSpansTwoCells class="bg-green-700 text-white w-200">Spans 2 Cells</div>
</div>
<div id=cell2 class="px-4 h-24">Cell 2</div>
<div id=cell3 class="px-4 h-24">Cell 3</div>
</div>
</div>