I am trying to create a grid layout that has 4 items in it. I want the first item to be full width, while the second, third, and fourth items should have width divided equally. There should only be two rows. Which css properties will help me achieve this?
.grid-container {
display: grid;
grid-template-columns: 1fr repeat(3, auto);
grid-template-rows: auto;
grid-gap: 20px;
}
.item {
background-color: #ddd;
padding: 20px;
text-align: center;
}
.item-1 {
grid-column: 1 / span 3;
}
<div class="grid-container">
<div class="item-1">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
4
Answers
Depends how you want to lay them out, but the documentation here shows good examples of various layouts.
Should achieve what you intend to.
Basically,
grid-row: 2/3
means.item
is in the 2nd row, but if you want 3 items in the 2nd row, that means there are actually 4 columns (this is how the grid system works). Equal width for.item
is achieved automatically. So now.item-1
needs to widen togrid-column: 1/4
meaning it spans from column 1 – 4. I put width 100% for good measure and background colors for my own debugging purposes.first, if you want to specify a style on any class that contains a specific name in your case "item"
[class *="item"] it refers that any class the has word item.
basically using repeat 3 areas each on with the auto width or you can use 1fr too.
You want a grid that is 3 columns wide, with each column taking up what space it can (1fr).
Then you just want to set the first item to span all 3 columns, which your CSS already does.
This snippet gives the first item a background color so we can check it is spanning correctly.
The snippet sets 2 rows each with the height determined by its content.
Please try this and let me know it that worked?