最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to make a css grid of images that resizes to fill at least one dimension of it's container, but doesn't dist

programmeradmin2浏览0评论

See example below - the current setup shows most of the images being cut off, but I want them to select sizes for the grid such that all cells will be shown. If you swap the '1' and '6' in the snippet CSS, you will see the images all shrink (and maintain their aspect ratios) to fit within the 300x500 container. How can I get the same behavior when all the images are in a single column?

.container {
  min-height: 500px;
  min-width: 300px;
  max-height: 500px;
  max-width: 300px;
  background: lightBlue;
  //padding: 10px; 
  overflow: hidden;
}

.grid {
  display: grid;
  max-height: 100%;
  grid-template-columns: repeat(1, minmax(1px, 1fr));
  grid-template-rows: repeat(6, minmax(1px, 1fr));
  
  // The above expands the images to match the width of the container, and then sets their
  // height based on the original aspect ratio - most items are not visible (i.e., the images
  // are expanding out of the grid container)
  // if you swap the '6' and '1' above, the images will correctly shrink to fit in their container,
  // at the largest aspect ratio that is supported
  
  
  //padding: 10px;
  background: green;
}

img {
  max-height: 100%;
  max-width: 100%;
  object-fit: cover;
  //padding: 5px;
  background: red;
}
<div class="container">  
  <div class="grid">
    <img src=".jpg" />
    <img src=".jpg" />
    <img src=".jpg" />
    <img src=".jpg" />
    
    <img src=".jpg" />
    <img src=".jpg" />
  </div>
</div>

See example below - the current setup shows most of the images being cut off, but I want them to select sizes for the grid such that all cells will be shown. If you swap the '1' and '6' in the snippet CSS, you will see the images all shrink (and maintain their aspect ratios) to fit within the 300x500 container. How can I get the same behavior when all the images are in a single column?

.container {
  min-height: 500px;
  min-width: 300px;
  max-height: 500px;
  max-width: 300px;
  background: lightBlue;
  //padding: 10px; 
  overflow: hidden;
}

.grid {
  display: grid;
  max-height: 100%;
  grid-template-columns: repeat(1, minmax(1px, 1fr));
  grid-template-rows: repeat(6, minmax(1px, 1fr));
  
  // The above expands the images to match the width of the container, and then sets their
  // height based on the original aspect ratio - most items are not visible (i.e., the images
  // are expanding out of the grid container)
  // if you swap the '6' and '1' above, the images will correctly shrink to fit in their container,
  // at the largest aspect ratio that is supported
  
  
  //padding: 10px;
  background: green;
}

img {
  max-height: 100%;
  max-width: 100%;
  object-fit: cover;
  //padding: 5px;
  background: red;
}
<div class="container">  
  <div class="grid">
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
  </div>
</div>

Share Improve this question asked Mar 15 at 12:15 RollieRollie 4,7794 gold badges39 silver badges68 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Simply define width and height instead of using min/max on the container

.container {
  height: 500px;
  width: 300px;
  background: lightBlue;
  overflow: hidden;
}

.grid {
  display: grid;
  height: 100%;
  grid-template-rows: repeat(6, minmax(1px, 1fr));
  background: green;
}

img {
  max-height: 100%;
  max-width: 100%;
  object-fit: cover;
  background: red;
}
<div class="container">  
  <div class="grid">
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
    <img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />
  </div>
</div>

You need to specify a height for your .container.

const s = []
for (let i = 0; i < 36; i++) {
  s.push(`<img src="https://cdn.pixabay/photo/2017/02/07/16/47/kingfisher-2046453_960_720.jpg" />`)
}
document.querySelectorAll('.grid').forEach(g => {
  g.innerHTML = s.join('')
})
.container {
  width: 80vw;
  height: 80vh;
  background: lightBlue;
  overflow: hidden;
}

.grid {
  display: grid;
  height: 100%;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  grid-template-rows: repeat(6, minmax(0, 1fr));
  background: green;
  gap: 2px;
}

img {
  max-height: 100%;
  max-width: 100%;
  background: red;
}

.cropped img {
  height: 100%;
  width: 100%;
  object-fit: cover;
  object-position: 66% top;
}
<div class="container">  
  <div class="grid"></div>
</div>

<p>or if you want the images to crop automatically</p>
<div class="container cropped">  
  <div class="grid"></div>
</div>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论