大碗编程

Did You Know?

CSS 图片廊

图片廊

以下是使用 CSS 创建图片廊:

<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="http://static.dawancode.com/images/demo/demo1.jpg">
      <img src="http://static.dawancode.com/images/demo/demo1.jpg" alt="图片文本描述"  >
    </a>
    <div class="img-fluid">这里添加图片文本描述</div>
  </div>
</div>
 
<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="http://static.dawancode.com/images/demo/demo2.jpg">
      <img src="http://static.dawancode.com/images/demo/demo2.jpg" alt="图片文本描述"  >
    </a>
    <div class="img-fluid">这里添加图片文本描述</div>
  </div>
</div>
 
<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="http://static.dawancode.com/images/demo/demo3.jpg">
      <img src="http://static.dawancode.com/images/demo/demo3.jpg" alt="图片文本描述"  >
    </a>
    <div class="img-fluid">这里添加图片文本描述</div>
  </div>
</div>
 
<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="http://static.dawancode.com/images/demo/demo4.jpg">
      <img src="http://static.dawancode.com/images/demo/demo4.jpg" alt="图片文本描述"  >
    </a>
    <div class="img-fluid">这里添加图片文本描述</div>
  </div>
</div>

 


更多实例

<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>

<h4>Responsive Image Gallery</h4>
<h4>Resize the browser window to see the effect.</h4>

<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="img_5terre.jpg">
      <img src="img_5terre.jpg" alt="Cinque Terre"  >
    </a>
    <div class="img-fluid">Add a description of the image here</div>
  </div>
</div>


<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="img_forest.jpg">
      <img src="img_forest.jpg" alt="Forest"  >
    </a>
    <div class="img-fluid">Add a description of the image here</div>
  </div>
</div>

<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="img_lights.jpg">
      <img src="img_lights.jpg" alt="Northern Lights"  >
    </a>
    <div class="img-fluid">Add a description of the image here</div>
  </div>
</div>

<div class="img-fluid">
  <div class="img-fluid">
    <a target="_blank" href="img_mountains.jpg">
      <img src="img_mountains.jpg" alt="Mountains"  >
    </a>
    <div class="img-fluid">Add a description of the image here</div>
  </div>
</div>

<div class="img-fluid"></div>

<div >
  <p>This example use media queries to re-arrange the images on different screen sizes: for screens larger than 700px wide, it will show four images side by side, for screens smaller than 700px, it will show two images side by side. For screens smaller than 500px, the images will stack vertically (100%).</p>
  <p>You will learn more about media queries and responsive web design later in our CSS Tutorial.</p>
</div>

</body>