小码哥的IT人生

CSS 图片库 详解

css3基础 2022-05-23 12:05:17小码哥的IT人生shichen

CSS 图片库

CSS 可用于创建图片库。

黄色郁金香
黄色郁金香
红色郁金香
红色郁金香
花花草草
花花草草
花花草草
花花草草

 

图片库

下面这个图片库是使用 CSS 创建的:

示例代码:

<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="/i/photo/tulip-yellow.jpg">
    <img src="/i/photo/tulip-yellow.jpg" alt="Cinque Terre" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
<div class="gallery">
  <a target="_blank" href="img_forest.jpg">
    <img src="img_forest.jpg" alt="Forest" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
<div class="gallery">
  <a target="_blank" href="img_lights.jpg">
    <img src="img_lights.jpg" alt="Northern Lights" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
<div class="gallery">
  <a target="_blank" href="img_mountains.jpg">
    <img src="img_mountains.jpg" alt="Mountains" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
</body>
</html>

 

完整实例【亲自试一试】:

<!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="/i/photo/tulip-yellow.jpg">
    <img src="/i/photo/tulip-yellow.jpg" alt="Cinque Terre" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
<div class="gallery">
  <a target="_blank" href="/i/photo/tulip-red.jpg">
    <img src="/i/photo/tulip-red.jpg" alt="Forest" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
<div class="gallery">
  <a target="_blank" href="/i/photo/flower-1.jpg">
    <img src="/i/photo/flower-1.jpg" alt="Northern Lights" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
<div class="gallery">
  <a target="_blank" href="/i/photo/flower-2.jpg">
    <img src="/i/photo/flower-2.jpg" alt="Mountains" width="600" height="400">
  </a>
  <div class="desc">在此处添加图像描述</div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

更多实例

完整实例【响应式图库】:

<!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>
<h1>响应式图片库</h1>
<h2>请调整窗口大小来查看效果。</h2>
<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="/i/photo/tulip-yellow.jpg">
      <img src="/i/photo/tulip-yellow.jpg" alt="黄色郁金香" width="600" height="400">
    </a>
    <div class="desc">在此处添加图像描述</div>
  </div>
</div>
<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="/i/photo/tulip-red.jpg">
      <img src="/i/photo/tulip-red.jpg" alt="红色郁金香" width="600" height="400">
    </a>
    <div class="desc">在此处添加图像描述</div>
  </div>
</div>
<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="/i/photo/flower-1.jpg">
      <img src="/i/photo/flower-1.jpg" alt="花花草草" width="600" height="400">
    </a>
    <div class="desc">在此处添加图像描述</div>
  </div>
</div>
<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="/i/photo/flower-2.jpg">
      <img src="/i/photo/flower-2.jpg" alt="花花草草" width="600" height="400">
    </a>
    <div class="desc">在此处添加图像描述</div>
  </div>
</div>
<div class="clearfix"></div>
<div style="padding:6px;">
  <p>本例使用媒体查询来重新排列不同屏幕尺寸的图像:对于宽于 700 像素的屏幕,它将并排显示四幅图像;对于小于 700 像素的屏幕,将并排显示两幅图像。对于小于 500 像素的屏幕,图像将垂直堆叠(100%)。</p>
  <p>您稍后将在我们的 CSS 教程中学到有关媒体查询和响应式 Web 设计的更多知识。</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

如何使用 CSS 媒体查询创建响应式图库,在台式机、平板电脑和智能手机上看起来都不错。

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024