0

I have this layout

X X X X
X X X X
A B

With grid-template-columns: repeat(4, 1fr);

But I actually want the two last items to take the full width of the remaining space, like :

X X X X
X X X X 
A A B B

Sorry for the dumb question, gonna watch a complete course but right now I really need to get this done

2 Answers2

0

you can also use :nth-last-child() and grid-column:span2; if your markup is always the same.

body>div {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

div {
  border: solid 1px;
  margin: 3px;
}

div div:nth-last-child(1),
div div:nth-last-child(2) {
  grid-column: span 2;
  color: green;
}
<div>
  <div>x</div>
  <div>x</div>
  <div>x</div>
  <div>x</div>
  <div>x</div>
  <div>x</div>
  <div>x</div>
  <div>x</div>
  <div>a</div>
  <div>b</div>
</div>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
-1

You can generate a CSS grid using this website.

Here is a complete guide about CSS Grid.

Added a red border, margin and height, so the desired grid is visible.

div{
border: 5px solid red;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 0px 0px;
  grid-template-areas:
    "X X X X"
    "X X X X"
    "A A B B";
    height:100px;
    width:100px;
}
.X { grid-area: X; }
.A { grid-area: A; }
.B { grid-area: B; }
<div class="grid-container">
  <div class="X">XXXX</div>
  <div class="A">AA</div>
  <div class="B">BB</div>
</div>
Sam
  • 723
  • 5
  • 18