简体   繁体   English

带有 CSS 网格中的图像的单元格占用太多空间

[英]Cell with an image in CSS grid takes up too much space

I'm trying to create a 3 column layout where:我正在尝试创建一个 3 列布局,其中:

  • 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).图像的高度应为 100%(垂直填充视口)。
  • no layout shifts happen when the image loads (I included width and height attributes on the image).加载图像时不会发生布局变化(我在图像上包含了widthheight属性)。
  • I intend to use it with 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM