CSS3 响应式图片样式
CSS 可以用来创建图片画廊。本例使用媒体查询(media queries)在不同的屏幕大小上重新排列图像。调整浏览器窗口的大小以查看效果:
代码如下:
<!DOCTYPE html>
<html>
<head>
<style>
div.gallery {
border: 1px solid #ccc;
}
div.gallery:hover {
border: 1px solid #777;
}
div.gallery img {
width: 100%;
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;
}
</style>
</head>
<body>
<h2>自适应图片画廊</h2>
<h4>调整浏览器窗口的大小以查看效果。</h4>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="img_5terre.jpg">
<img src="/znadmin/md/1298/0.jpg" alt="Cinque Terre" width="600" height="400">
</a>
<div class="desc">在此处添加图像描述</div>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="img_forest.jpg">
<img src="/znadmin/md/1298/1.jpg" alt="Forest" width="600" height="400">
</a>
<div class="desc">在此处添加图像描述</div>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="img_lights.jpg">
<img src="/znadmin/md/1298/2.jpg" alt="Northern Lights" width="600" height="400">
</a>
<div class="desc">在此处添加图像描述</div>
</div>
</div>
<div class="responsive">
<div class="gallery">
<a target="_blank" href="img_mountains.jpg">
<img src="/znadmin/md/1298/3.jpg" alt="Mountains" width="600" height="400">
</a>
<div class="desc">在此处添加图像描述</div>
</div>
</div>
<div class="clearfix"></div>
<div style="padding:6px;">
<p>这个例子使用媒体查询(media queries)来重新排列不同屏幕尺寸上的图像:对于宽度大于700px的屏幕,它将并排显示四个图像;对于小于700px的屏幕,它将并排显示两个图像。对于小于500px的屏幕,图像将垂直堆叠(100%)。</p>
<p>在我们的 CSS 教程中,您将进一步了解 媒体查询 和 响应式web设计 。</p>
</div>
</body>
</html>