无法将img调整为div大小:CSS网格

时间:2018-07-06 07:07:38

标签: html css

嘿,我正在尝试将img放入其div容器中。我正在使用CSS Grid作为布局。

html,
body {
  margin: 0;
  padding: 0;
}

.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-row: auto;
}

.item {
  height: 500px;
  border: 1px solid black;
}

.banner {
  max-width: 100%;
  max-height: 100%;
}


/* laptops and desktops */

@media (min-width: 1025px) and (max-width: 1280px) {
  .container {
    grid-template-columns: repeat(2, 1fr);
    grid-row: 1fr;
    padding: 60px 130px 20px 130px;
    grid-gap: 50px;
  }
}
<div class="container">
  <div class="item">
    <img class="banner" src="https://images.unsplash.com/photo-1479936343636-73cdc5aae0c3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6672a25aa653266572aaf2be253fb56a&auto=format&fit=crop&w=1000&q=80" />
  </div>
  <div class="item">
    <img class="banner" src="https://images.unsplash.com/photo-1508296695146-257a814070b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=bfacf656c686ff7e39ac1386691557be&auto=format&fit=crop&w=1000&q=80" />
  </div>
</div>

item容器的底部始终有一个空格。该图像覆盖了整个容器。有人可以告诉我如何解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

也许这可以帮助您:

html,
body {
  margin: 0;
  padding: 0;
}

.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-row: auto;
}

.item {
    /*height: 500px;*/
    max-width: 500px; /*Remove this line if 500px limit is not important for you.  */
    border: 1px solid black;
}

.banner {
  /*max-width: 100%;
  max-height: 100%;*/
  width: 100%;
  display: block;

}


/* laptops and desktops */

@media (min-width: 1025px) and (max-width: 1280px) {
  .container {
    grid-template-columns: repeat(2, 1fr);
    grid-row: 1fr;
    padding: 60px 130px 20px 130px;
    grid-gap: 50px;

  }

  
}
    <body>

<div class="container">
  <div class="item">
    <img class="banner" src="https://images.unsplash.com/photo-1479936343636-73cdc5aae0c3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6672a25aa653266572aaf2be253fb56a&auto=format&fit=crop&w=1000&q=80" />
  </div>
  <div class="item">
    <img class="banner" src="https://images.unsplash.com/photo-1508296695146-257a814070b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=bfacf656c686ff7e39ac1386691557be&auto=format&fit=crop&w=1000&q=80" />
  </div>
</div>
                     
        
    </body>

答案 1 :(得分:0)

尽管您的媒体查询很奇怪,但我认为您想解决比率问题,所以我尝试这样:

html,
body {
  margin: 0;
  padding: 0;
}
*{
  box-sizing: border-box;
}
.container {
  display: inline-grid;
  grid-template-columns: 1fr;
  grid-row: auto;
}
.item{
  display: inline-block;
  width: 500px;
  max-width: 100%;
}
.ratio-1-1{
  padding-top: calc(100% - 2px);
  /* 2px = border * 2 */
  position: relative;
  border: 1px solid black;
  font-size: 0;
}
.ratio-1-1 img{
  position: absolute;
  width: 100%;
  height: auto;
  left: 0;
  top: 0;
}
@media (max-width: 1280px) and (min-width: 1025px){
  .container {
      grid-template-columns: repeat(2, 1fr);
      grid-row: 1fr;
      padding: 60px 130px 20px 130px;
      grid-gap: 50px;
  }
}
<div class="container">
  <div class="item">
    <div class="ratio-1-1">
      <img src="https://images.unsplash.com/photo-1479936343636-73cdc5aae0c3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6672a25aa653266572aaf2be253fb56a&auto=format&fit=crop&w=1000&q=80" alt="">
    </div>
  </div>
  <div class="item">
    <div class="ratio-1-1">
      <img src="https://images.unsplash.com/photo-1508296695146-257a814070b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=bfacf656c686ff7e39ac1386691557be&auto=format&fit=crop&w=1000&q=80" alt="">
    </div>
  </div>
</div>

您的图片为500x500,因此.ratio-1-1上的填充顶部为图片高度/图片宽度* 100%。

答案 2 :(得分:-1)

在您的.banner类中添加

width: 100%;
object-fit: cover;

这将拉伸您的图像,并且完全不会丢失比例。