[英]Cell with an image in CSS grid takes up too much space
I'm trying to create a 3 column layout where:我正在尝试创建一个 3 列布局,其中:
width
and height
attributes on the image).width
和height
属性)。srcset
and <picture>
(too serve correct image sizes and jpeg/webp).srcset
和<picture>
一起使用(也提供正确的图像大小和 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):这是它的样子(屏幕截图显示来自 Firefox 开发工具的网格轨道):
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?我怎么做?
As a general rule, try to avoid making images into grid or flex items.作为一般规则,尽量避免将图像制作成网格或弹性项目。 There are still too many bugs in modern browsers that prevent reliable rendering.
现代浏览器中仍然存在太多阻止可靠呈现的错误。
I just wrote about this here: How can I get an image to fit to the size of a grid cell?我刚刚在这里写到: How can I get an image to fit to the size of a grid cell?
That said, here's a solution you may find useful:也就是说,这里有一个您可能会觉得有用的解决方案:
body, figure { padding: 0; margin: 0; } figure { display: grid; grid-template-columns: 1fr min-content 1fr; grid-gap: 1rem; height: 100vh; border: 2px solid red; } figure::before { content: 'abc'; } div { min-height: 0; } img { object-fit: cover; height: 100%; vertical-align: bottom; } * { box-sizing: border-box; }
<figure> <div> <img src="http://placekitten.com/g/400/600" width="400" height="600" alt="cat"> </div> <figcaption>This is a cat</figcaption> </figure>
Considering the fact that the image will be the highest element you can move the max-height:100vh
to it and it will be easier:考虑到图像将是最高元素这一事实,您可以将
max-height:100vh
到它,这样会更容易:
body, figure { padding: 0; margin: 0; } figure { display: grid; grid-template-columns: 1fr auto 1fr; grid-template-rows: 1fr; grid-gap: 1rem; border: 2px solid red; } figure::before { content: 'abc'; } img { max-height:calc(100vh - 4px); /* considering the border */ width: auto; max-width:100%; }
<figure> <img src="http://placekitten.com/g/400/600" width="400" height="600" alt="cat"> <figcaption>This is a cat</figcaption> </figure>
try to put justify-content:center in the figure.尝试将 justify-content:center 放在图中。 and the max-width:100% in the img.
以及 img 中的 max-width:100%。 this is what will center the image horizontally in the figure and makes it to take the full with.
这就是使图像在图中水平居中并使其完整的原因。
A few changes I would make;)我会做一些改变;)
* { margin: 0; padding: 0; } html, body { height: 100%; padding: 0; margin: 0; } body { min-height: 100%; padding: 0; } figure { max-width: 100vw; max-height: 100vh; display: grid; height: 100%; grid-template-columns: 1fr minmax(50%, min-content) 1fr; grid-gap: 1rem; border: 2px solid red; } figure::before { content: 'abc'; } img { object-fit: contain; width: 100%; height: 100%; }
<figure> <img src="http://placekitten.com/g/400/600" width="400" height="600" alt="cat"> <figcaption>This is a cat</figcaption> </figure>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.