I know that there are a few existing questions about aspect ratio CSS grid, but none explain my issue.
I have a grid that scales at some aspect ratio. Some cells will be filled with content in future, as an example I filled some cells with image:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.grid {
position: relative;
display: grid;
grid-template-columns: 1.167fr 0.45fr .225fr 0.3fr 0.8fr;
grid-template-rows: 0.44fr 0.1875fr 0.4fr .9fr;
grid-gap: 2vw;
}
.grid figure {
background: #eee;
}
.grid figure img {
width: 100%;
height: 100%;
}
.grid figure:nth-child(1) {
grid-column: 1 / 2;
grid-row: 1 / 3;
}
<div class="grid">
<figure>
<img src="https://picsum.photos/id/237/200/200" />
</figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
</div>
Which works great (Run Code Snippet -> open Full Page -> resize browser to test).
Now, if I replace the image in the first <figure>
the layout breaks. Even though image size did not affect grid size (as img has width:100%;height:100%
).
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.grid {
position: relative;
display: grid;
grid-template-columns: 1.167fr 0.45fr .225fr 0.3fr 0.8fr;
grid-template-rows: 0.44fr 0.1875fr 0.4fr .9fr;
grid-gap: 2vw;
}
.grid figure {
background: #eee;
}
.grid figure .test-element {
width: 100%;
height: 100%;
}
.grid figure:nth-child(1) {
grid-column: 1 / 2;
grid-row: 1 / 3;
}
<div class="grid">
<figure>
<div class="test-element">Test</div>
</figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
<figure></figure>
</div>
Why does it work with img
but doesn't work with other element?