how to place multiple items in a grid column cell using tailwind? - css

I have a design where I have 3 items. 2 items should be placed vertically and 1 item has to be in it's own cell. So, 2 items should be placed in 1 cell vertically and 1 item takes it's own whole cell. To demonstrate, below is the image
How can I achieve this design using tailwind?

You can make such layout using grid - separate your layout on three columns (sidebar + main content spans on two columns) and 2 rows like #ChenBr did
<div class="border-2 grid grid-cols-3 grid-rows-2">
<div class="border-2 col-span-1">1</div>
<div class="border-2 col-span-2 row-span-2">2</div>
<div class="border-2 col-span-1">3</div>
<div>
However grid-rows-2 will be compiled as
.grid-rows-2 {
grid-template-rows: repeat(2, minmax(0, 1fr));
}
1fr for both rows means 2 rows will be sized equally. So I would recommend to change this class into grid-rows-[min-content_1fr] - that mean first row will take place base on its minimum content
min-content - is a keyword representing the largest minimal content contribution of the grid items occupying the grid track.
and the second one will take the rest - but it's up to your application
Basic example
<div class="grid grid-cols-3 grid-rows-[min-content_1fr]">
<div>1</div>
<div class="col-span-2 row-span-2">2</div>
<div>3</div>
<div>
DEMO

You can achieve this setup using grid/flex. Using one or the other depends on your content.
Grid:
Create a three-by-two grid using grid-cols-3 and grid-rows-2 on the grid's container. Then, set each container's span to fit your structure (using col-span-n row-span-n).
Read about grid-cols here and about grid-row here.
<div class="border-2 grid grid-cols-3 grid-rows-2">
<div class="border-2 col-span-1">1</div>
<div class="border-2 col-span-2 row-span-2">2</div>
<div class="border-2 col-span-1">3</div>
<div>
Tailwind-play
Flex:
We are going to have two containers. The main container will wrap all the elements (including the second container), and the inner container will wrap your first two elements. Each of those containers will have a flex utility applied to it.
Then, we will apply flex-col on the second container. This way, the container will place its children on top of each other, just like the first column of your image.
The first container's default flex-direction is flex-row which is why the inner container and the third element will be positioned next to each other, just like a row.
To give the structure a proportion similar to your image, we can set the inner container's width to 30% (w-[30%]), and the third element to 70% (w-[70%]).
Read about flex-direction here.
<div class="flex border-2">
<div class="flex w-[30%] flex-col">
<div class="border-2">1</div>
<div class="border-2">2</div>
</div>
<div class="w-[70%] border-2">3</div>
<div></div>
</div>
Tailwind-play

Related

How to fill remaining empty space if element using translate-x using tailwind CSS

i have problem with filling empty space, if parent div using display flex, and children have translate-x class, here some example code
<div class="flex h-screen w-screen">
<div class="w-1/3 -translate-x-10 bg-red-200"></div>
<div class="w-full bg-red-400"></div>
</div>
it show like this
how do i fill the empty space with red if i change the translate-x value
First thing that comes to mind is you could try adding a background color to the parent div like assuming you want the darker red. There are probably other methods but if this one works for your use case it's pretty simple.
<div class="flex h-screen w-screen bg-red-400">
<div class="w-1/3 -translate-x-10 bg-red-200"></div>
<div class="w-full bg-red-400"></div>
</div>

How to make css grid height increase to accommodate grid gap?

i'm facing this problem with grid where when i add a row-gap property the grid items will overflow in the bottom and cover other elements..how can i prevent this from happening? is there a way to make the grid parent's height increase automatically to fit the changes after gap is added?
i'm using react and bootstrap in my project..but to style the grid i'm using mainly CSS only
CSS
.items-grid{
display: grid;
grid-template-columns: repeat(4,1fr);
row-gap: 3%;
}
REACT.JS JSX
<div>
<div>
<div className="items-grid w-80 mx-auto">
{itemsList.map((item) => {
return <Item key={item._id} title={item.title} image={item.image} />
})}
</div>
</div>
<div className="w-90 mx-auto text-center my-5">
<h4 className="d-inline">"Some text"</h4>
</div>
</div>
here is what i end up with, as you can see the last row is overflowing in the bottom covering the elements below..what can i do to fix this?

3 colum layout on bootstrap losing its shape

I have a 3 columns design where the first column has a fixed width of 630px, the middle column should take all available space and the third column has a fixed width of 200px.
I set it up using bootstrap Flex, with flex-fill in the middle column. It looks all dandy but when I start filling up the middle section, it grows eating the space of the last one.
The code, simplified, looks like this:
//body css: height: 100%, oveflow: hidden
<div class="mainContainer d-flex flex-row w-100"> //css: height 100vh,
<div class="leftColumn d-flex">
//inner content ar boxes with fixed width leaning to the 600px
//this part looks perfect
</div>
<div class="MainArea flex-fill d-flex flex-column">
<div class="MainAreaUpperRowOfCards flex-fill d-flex flex-row flex-wrap">
// here I add cards with fixed size, they are lied down
// in a row, and they wrap nicelly.
// But when they wrap this section starts to grow eating the
// third colum's space
</div>
</div class="MainAreaLowerRow d-flex flex-row justify-content-center">
//css: height: 250px, margin-bottom: -60px
//here are some cards that only show the upper part, hence the -60px
</div>
<div>
<div class="RightColumn d-flex flex-column"> //css: width 220px; background and magins..
//here some cards with players information
//this is the column being eaten.
</div>
</div>
This is what it looks like at the beginning and after adding some cards:
So I have two questions:
How do I fix the width of the right-most column? (green arrow in the picture)
How do I remove the space on the wrapping of the cards? (red arrow in the picture)
I added an animation for convenience:
Change width: 220px; to min-width: 220px;.
It's hard to help you without a live demo. Try to remove flex classes one by one and see if any of these classes are causing this space.

Can I put content inside a row in bootstrap 3's grid?

I'm building a layout where many rows will span the entire containing column. I'm wondering if I should nest a col-12 inside each of these rows and put content in there or if I should just put the content inside the row?
So, is this OK:
<div class="row">
<div class="my-lazy-content"></div>
</div>
Or, is it better to put a column in there too:
<div class="row">
<div class="col-xs-12">
<div class="my-verbose-content></div>
</div>
</div>
If you want the content to span the entire column, you should add in the col-xs-12 wrapping div around your content. This ensures that the content has that 15px of padding to the left and right and doesn't simply "hard-align" to the edges of the row element since the row class has negative -15px of margin to compensate for the container's 15px padding?
Is it absolutely necessary to have the rows? If the content simply spans the entire width of the container, why not just nest your content directly within the container as follows:
<div class="container">
<div class="content-wrapper">
</div>
</div>

How to place elements in columns using bootstrap3 grid

I am using bootstrap for making responsive website but when I am giving padding to elements to adjust them in particular column it is breaking in other devices.
I am giving padding in % still it's breaking.
What should I do?
What is the best way to adjust elements within a particular column?
You should keep the grid elements intact. Add children inside them to add padding.
<div class="row">
<div class="col-md-8">
<div class="element-with-padding"></div>
</div>
<div class="col-md-4">
<div class="element-with-padding"></div>
</div>
</div>

Resources