How do I create a CSS grid layout box that spans 2 columns and 2 rows?
I have created a grid according to the newest CSS Grid specification but am not familiar with it yet. I am trying to create the following layout without defining grid areas for each child of the grid.
codepen
body {
max-width: 1024px;
margin: 10px auto;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a b" "a a c" "d e f";
}
.grid__thing {
background-color: rebeccapurple;
}
.a {
grid-area: a;
}
.b {
grid-area: b;
}
.c {
grid-area: c;
}
.d {
grid-area: d;
}
.e {
grid-area: e;
}
.f {
grid-area: f;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="grid">
<div class="grid__thing a">
<img src="https://placehold.it/1360x880" alt="" />
</div>
<div class="grid__thing b">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing c">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing d">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing e">
<img src="https://placehold.it/1327x817" alt="" />
</div>
<div class="grid__thing f">
<img src="https://placehold.it/1327x817" alt="" />
</div>
</div>
Ideally, I would like to be able to set all grid properties on the parent grid item and then ONLY define properties on grid item A to span two columns and rows.
Currently, each grid area is specified and a unique class is added:
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a b"
"a a c"
"d e f";
.a {
grid-area: a;
}
.b {
grid-area: b;
}
.c {
grid-area: c;
}
.d {
grid-area: d;
}
.e {
grid-area: e;
}
.f {
grid-area: f;
}
I would like to do something like this, so I don't need to create a unique CSS class for each grid item:
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a b"
"a a c"
"d e f";
}
.a {
// The only unique selector, so this is the only thing that
// should be given unique styling
}
If you don't want to define grid areas for every grid item, then don't use a property grid-template-areas
that requires you to define names for each grid item.
Instead, just work with grid-template-columns
and grid-template-rows
in the container.
Then apply a 2x2 dimension to the first mesh element using grid-column
and grid-row
.
grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
grid-gap: 10px;
}
grid_item:first-child {
grid-column: 1 / 3; /* span from grid column line 1 to 3 (i.e., span 2 columns) */
grid-row: 1 / 3; /* same concept, but for rows */
}
/* non-essential decorative styles */
grid_item {
background-color: aqua;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5em;
}
<grid-container>
<grid_item>A</grid_item>
<grid_item>B</grid_item>
<grid_item>C</grid_item>
<grid_item>D</grid_item>
<grid_item>E</grid_item>
<grid_item>F</grid_item>
</grid-container>
demo codepen
This seems like a silly answer ...
Just do what you plan. remove unnecessary classes.
or am I missing something?
body {
max-width: 1024px;
margin: 10px auto;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "a a" "a a";
}
.grid__thing {
background-color: rebeccapurple;
}
.a {
grid-area: a;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="grid">
<div class="grid__thing a">
<img src="https://placehold.it/1360x880" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/660x405" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/1327x817" alt="" />
</div>
<div class="grid__thing">
<img src="https://placehold.it/1327x817" alt="" />
</div>
</div>