简体   繁体   English

使图像适合 CSS Grid

[英]Make image fit CSS Grid

I have a grid in which I position images but they doen't fill the entire space they are given.我有一个网格,我在其中放置图像,但它们并没有填满给定的整个空间。

I hope the image below illustrates the problem good enough.我希望下图能很好地说明问题。 As we can see the top image as well as the right one don't fill the rows they are assigned to.正如我们所看到的,顶部图像和右侧图像没有填充它们分配的行。 What causes this and how can I fix it?是什么原因造成的,我该如何解决?

Code is like that:代码是这样的:

 <section class="wrapper">
   <div class="one">
     <img class="img" src="images/lorem/ipsum.jpg" alt="">
   </div>

   <div class="two">
     <img class="img" src="images/lorem/ipsum2.jpg" alt="">
   </div>

   <div class="three">
     <img class="img" src="images/lorem/ipsum3.jpg" alt="">
   </div>
 </section>

 .wrapper {
   display: grid;
   grid-template-columns: repeat(8, 1fr);
   grid-auto-rows: minmax(150px, auto);
   grid-gap: 20px;
   height: 100vh;
 }

.one {
  grid-column: 2 / 7;
  grid-row: 1 / 3;
}

.two {
  grid-column: 2 / 5;
  grid-row: 3 / 4;
} 

.three {
  grid-column: 5 / 7;
  grid-row: 3 / 4;
}

.img {
  width: 100%;
}

在此处输入图像描述

How can I fix this?我怎样才能解决这个问题?

add one div to wrap your image, then set image style to .img { max-width: 100%;添加一个 div 来包裹你的图像,然后将图像样式设置为 .img { max-width: 100%; height: auto }高度:自动 }

I had a similar problem to this before , the simple solution is to set the height or width to 100% for the img elements to fill their container.我之前遇到过类似的问题,简单的解决方案是将高度或宽度设置为 100%,以便 img 元素填充其容器。 Then to set the object fit property to cover for better cropping.然后将对象适合属性设置为覆盖以便更好地裁剪。 You don't even need div containers for your images.您甚至不需要 div 容器来存储图像。 Like this:像这样:

The grid container will take care of the positioning of the cells.网格容器将负责单元格的定位。

 img{ width: 100%; height: 100%; object-fit: cover; }

This is normal because images are preserving their aspect ratio.这是正常的,因为图像保留了它们的纵横比。

A good way to solve this common problem is to set the image as a CSS-background parameter in a div in your grid instead of an <img> tag in html, such as background: url(' http://link-to-your-image/image.jpg ') and use background-size: cover;解决这个常见问题的一个好方法是将图像设置为网格中 div 中的 CSS-background 参数,而不是 html 中的<img>标签,例如 background: url(' http://link-to- your-image/image.jpg ') 并使用background-size: cover; on the same element.在同一个元素上。

This way, the picture will fill out all the space that is attributed to it.这样,图片将填满归属于它的所有空间。

尝试 background-size: 100% 100% 和 background-size: cover or fit

This worked for me using React with inline styles:这对我使用带有内联样式的 React 很有用:

<div 
    style = {{
        gridArea: "image",
        backgroundImage: `url(${imageFromImports})`,
        backgroundSize: "contain",
        backgroundRepeat: "no-repeat",
        backgroundPosition: "center",
    }}
/>

I finally discovered the rules to dynamically resizing images within a css grid.我终于发现了在 CSS 网格中动态调整图像大小的规则。 They are:他们是:

  1. Every nested grid element of the parent grid must have min-height: 0 set, including the img tag itself父网格的每个嵌套网格元素都必须设置min-height: 0 ,包括img标签本身
  2. The img tag must also define max-width: 100% img标签还必须定义max-width: 100%
  3. The immediate grid parents of the img tags must also have min-width: 0 set for the align- and justify- properties to work correctly img标记的直接网格父级还必须设置min-width: 0以使align-justify-属性正常工作

Here is sample code that will work:这是可以工作的示例代码:

// parent
<div css={{
    display: "grid",
    gridTemplateRows: "max-content 1fr",
    height: "50vh",
    maxHeight: "50vh",
}}>
    <header></header>

    <div css={{
      display: "grid",
      gridTemplateRows: "3fr 2fr",
      minHeight: 0,
    }}>
        <div css={{
          display: "grid",
          minHeight: 0,
          minWidth: 0,
        }}>
            <img
                css={{
                  maxHeight: "100%",
                  minHeight: 0,
                  borderRadius: 10,
                }}
            />
        </div>

        <div css={{
          display: "grid",
          minHeight: 0,
        }}>
            {imgs.map(img => 
                <div css={{
                  display: "grid",
                  minHeight: 0,
                  minWidth: 0
                }}>
                    <img
                        css={{
                          maxHeight: "100%",
                          minHeight: 0,
                          borderRadius: 10,
                        }}
                    />
                    <p>...</p>
                </div>
            }
        </div>
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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