CSS 图片画廊
CSS 可用于创建图片画廊。
图片库
下面这个图片库是使用 CSS 创建的:
<!DOCTYPE html>
<html>
<head>
<style>
div.gallery {
margin: 5px;
border: 1px solid #ccc;
float: left;
width: 180px;
}
div.gallery:hover {
border: 1px solid #777;
}
div.gallery img {
width: 100%;
height: auto;
}
div.desc {
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<div class="gallery">
<a target="_blank" href="/images/img_5terre.jpg">
<img src="/images/img_5terre.jpg" alt="Cinque Terre" width="600" height="400">
</a>
<div class="desc">在这里添加图片描述</div>
</div>
<div class="gallery">
<a target="_blank" href="/images/img_forest.jpg">
<img src="/images/img_forest.jpg" alt="Forest" width="600" height="400">
</a>
<div class="desc">在这里添加图片描述</div>
</div>
<div class="gallery">
<a target="_blank" href="/images/img_lights.jpg">
<img src="/images/img_lights.jpg" alt="Northern Lights" width="600" height="400">
</a>
<div class="desc">在这里添加图片描述</div>
</div>
<div class="gallery">
<a target="_blank" href="/images/img_mountains.jpg">
<img src="/images/img_mountains.jpg" alt="Mountains" width="600" height="400">
</a>
<div class="desc">在这里添加图片描述</div>
</div>
</body>
</html>
响应式图片画廊如何使用 CSS media 创建响应式图库,在台式机、平板电脑和智能手机上看起来都不错。
<!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="/images/img_5terre.jpg">
<img src="/znadmin/md/1253/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="/images/img_forest.jpg">
<img src="/znadmin/md/1253/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="/images/img_lights.jpg">
<img src="/znadmin/md/1253/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="/images/img_mountains.jpg">
<img src="/znadmin/md/1253/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教程 中,您将进一步了解 media queries 和 响应式web设计 。</p>
</div>
</body>
</html>