仅在 HTML 和 CSS 中具有自动缩放功能的图像网格
Image grid with autorescaling in HTML & CSS Only
我一直在尝试制作响应式自动缩放图像网格以在我们的网站上展示广告。我用这个作为参考 - 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>
这段代码是响应式的,在手机上我没有遇到任何问题,因为它通过填充整个区域看起来很整洁。但是如果你在桌面上看它,它会在一些不适合高度的图片下面注入空 space。我正在使用一堆 URL 并将它们作为列表提供,以便桌面上的这个 1280X200px 区域整齐地填满不同大小的图像。我尝试查看 freewall 和大量其他 jsfiddles 和笔,但无法实现如何删除 whitespace 并使该特定框看起来不错。谢谢。
根据我的意见 - 我会选择使用 object-fit 的 flexbox 解决方案(使用 polyfil for 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>
我一直在尝试制作响应式自动缩放图像网格以在我们的网站上展示广告。我用这个作为参考 - 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>
这段代码是响应式的,在手机上我没有遇到任何问题,因为它通过填充整个区域看起来很整洁。但是如果你在桌面上看它,它会在一些不适合高度的图片下面注入空 space。我正在使用一堆 URL 并将它们作为列表提供,以便桌面上的这个 1280X200px 区域整齐地填满不同大小的图像。我尝试查看 freewall 和大量其他 jsfiddles 和笔,但无法实现如何删除 whitespace 并使该特定框看起来不错。谢谢。
根据我的意见 - 我会选择使用 object-fit 的 flexbox 解决方案(使用 polyfil for 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>