【问题标题】:Image grid with autorescaling in HTML & CSS Only仅在 HTML 和 CSS 中具有自动缩放功能的图像网格
【发布时间】:2019-03-23 00:08:42
【问题描述】:

我一直在尝试制作响应式自动重新缩放图像网格以在我们的网站上显示广告。我以此为参考——W3Schools-ImageGridMaker

基于此,我尝试了 4 天时间想出了这段代码。

div.gallery {
  border: 1px solid #ccc;
}

div.gallery:hover {
  border: 1px solid #777;
}

div.gallery img {
  display: block;
  object-fit: contain;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
}

div.desc {
  padding: 15px;
  text-align: center;
}

* {
  box-sizing: border-box;
}

.responsive {
  padding: 0 6px;
  float: left;
  width: 24.99999%;
}

@media only screen and (max-width: 700px) {
  .responsive {
    width: 49.99999%;
    margin: 6px 0;
  }
}

@media only screen and (max-width: 500px) {
  .responsive {
    width: 100%;
  }
}

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

div.boxed {
  border: 5px solid red;
  width: 100%;
  height: auto;
  overflow: auto;
}
<div class="boxed">

  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/UgPbxk2.jpg" alt="Shiva1"></a>
    </div>
  </div>


  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/ColLeDr.png" alt="Shiva2"></a>
    </div>
  </div>

  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/gVjcLg2.jpg" alt="Shiva3"></a>
    </div>
  </div>

  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"> <img src="https://i.imgur.com/nxGxovl.png" alt="Shiva4"></a>
    </div>
  </div>

</div>

这段代码是响应式的,在手机上我没有问题,因为它通过填充整个区域看起来很整洁。但是,如果您在桌面上查看它,它会在一些不适合高度的图片下方注入空白空间。我正在使用一堆 URL,并且可以将它们作为列表提供,以便桌面上的这个 1280X200px 区域整齐地填充不同大小的图像。我尝试研究 freewall 和大量其他 jsfiddles 和笔,但无法实现如何删除空白并使特定框看起来不错。谢谢。

【问题讨论】:

  • 我会使用更新的教程——你应该停止使用浮点数,在 css3 时代,除了图像以外的元素应该没有必要滥用它(甚至可能不需要使用它)现在在图像上),但是您的 sn-p 没有显示您描述的行为-在桌面上,我的图像下方没有空间,并且红色框在它们周围都是齐平的(但是图像被阻止了,因此它们的高度都相同在我的浏览器上)?
  • 感谢您的建议。在桌面上,第一个图像下方有空白区域。红色边框响应框内图像的最大高度。
  • 听起来你只需要将所有图像裁剪为相同的高度,如果你想要砖石效果,那么你需要使用 js 插件
  • 我确实将图像的大小调整为 400px 高度,但是当我将它们放入红框时,它们并没有对齐到相同的高度。砌体效应?让我看看那是什么。谢谢。
  • 如果所有其他方法都失败了,您可以使用 object-fit - 只需为每个图像设置一个高度和宽度(例如高度 400px 宽度 100%),然后使用 object fit 覆盖 - 就像背景尺寸覆盖一样。唯一的缺点是您需要一个 polyfill 才能使其在 ie 中工作 - developer.mozilla.org/en-US/docs/Web/CSS/object-fit

标签: html css responsive-design responsive-images


【解决方案1】:

根据我的 cmets - 我会选择使用 object-fit 的 flexbox 解决方案(使用 polyfil 表示 ie):

.container {
  display: flex;
  flex-direction: row;   /* default value so optional - lines children in a row */
  flex-wrap: wrap;        /* allows children to wrap */
  justify-content: space-between; /* space children evenly over row */
}

.responsive {
  flex-basis: 25%;  /* makes the width 25% */
  
  /* if you don't want a fixed height image, I would use the padding top trick for aspect ratio divs */
  position: relative;
  padding-top: 30%;
}

.responsive img {
  position:absolute;
  display: block;
  width: 100%;
  height: 100%;
  top:0;
  left:0;
  object-fit:cover;
}


@media only screen and (max-width: 700px) {
  .responsive {
    flex-basis: 50%;
    margin: 6px 0;
  }
}

@media only screen and (max-width: 500px) {
  .responsive {
    flex-basis: 100%;
    padding-top: 50%;
  }
}
<div class="container">
  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/600" alt="Shiva3"></a>
    </div>
  </div>
  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/400" alt="Shiva3"></a>
    </div>
  </div>
  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/900" alt="Shiva3"></a>
    </div>
  </div>
  <div class="responsive">
    <div class="gallery">
      <a target="_blank" href="https://tnilive.com" onclick="window.open('https://www.google.com'); window.open('https://www.yahoo.com');"><img src="https://www.fillmurray.com/400/700" alt="Shiva3"></a>
    </div>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 2016-10-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多