I'm trying to create a 3 column layout where:
- left and right colums are equal in width
- left colum is empty
- right column contains the image caption (a text of arbitrary length)
- middle column contains an image. Image should have the height of 100% (fill the viewport vertically).
- no layout shifts happen when the image loads (I included
width
andheight
attributes on the image). - I intend to use it with
srcset
and<picture>
(too serve correct image sizes and jpeg/webp). I did not include this in the demo.
Here's my attempt:
body, figure {
padding: 0;
margin: 0;
}
figure {
max-width: 100vw;
max-height: 100vh;
display: grid;
grid-template-columns: 1fr min-content 1fr;
grid-template-rows: 1fr;
grid-gap: 1rem;
border: 2px solid red;
}
figure::before {
content: 'abc';
}
img {
height: 100%;
width: auto;
}
<figure>
<img src="http://placekitten.com/g/400/600" width="400" height="600" alt="cat">
<figcaption>This is a cat</figcaption>
</figure>
This is how it looks like (screenshot shows grid tracks from Firefox dev tools):
The issue is this space I painted with red pen over. It makes the grid cell with the image too wide. I want this to look like this:
Here the grid cell is only as wide as it needs to in order to fit the image (preserving aspect ratio). As a result the image is centered horizontally.
How do I do that?