如何使 CSS 画廊中的 div 保持一致,或所有尺寸相同或两者?

How to Keep divs in CSS Gallery in Line, or All The Same Size or Both?

我正在使用 w3schools 中的以下代码制作画廊。我遇到的问题是,如果一个项目的描述很长,它会导致下面的项目变得混乱并且排列不正确。

据我所知,这可以通过使项目框的高度相同或在从顶行最高的底部到所有三行的顶部的每一行之间设置间隙来解决在底行。我不知道该怎么做。

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;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="overflow: hidden;">
<div class="gallery">
  <a target="_blank" href="https://www.w3schools.com/css/img_5terre.jpg">
    <img src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="600" height="400">
  </a>
  <div class="desc">Add a description of the image here and make it really long to see what happens</div>
</div>

<div class="gallery">
  <a target="_blank" href="https://www.w3schools.com/css/img_forest.jpg">
    <img src="https://www.w3schools.com/css/img_forest.jpg" alt="Forest" width="600" height="400">
  </a>
  <div class="desc">Add a description of the image here</div>
</div>

<div class="gallery">
  <a target="_blank" href="https://www.w3schools.com/css/img_lights.jpg">
    <img src="https://www.w3schools.com/css/img_lights.jpg" alt="Northern Lights" width="600" height="400">
  </a>
  <div class="desc">Add a description of the image here</div>
</div>

<div class="gallery">
  <a target="_blank" href="https://www.w3schools.com/css/img_mountains.jpg">
    <img src="https://www.w3schools.com/css/img_mountains.jpg" alt="Mountains" width="600" height="400">
  </a>
  <div class="desc">Add a description of the image here</div>
</div>
</div>
</body>
</html>

有没有人能解决如何让它们整齐地保持内联?

display:flex 提供了一种非常巧妙的方式来组织这样的项目。您可以将所有内容拉伸到与其所在行中最高项目相同的高度,或者您可以以多种不同的方式对齐它们,只需向父容器添加几行代码。 更多信息:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

.gallery-container {
  display: flex;
  align-items: stretch;
  flex-wrap: wrap;
}

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;
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <div class="gallery-container">
    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_5terre.jpg">
        <img src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here and make it really long to see what happens</div>
    </div>

    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_forest.jpg">
        <img src="https://www.w3schools.com/css/img_forest.jpg" alt="Forest" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here</div>
    </div>

    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_lights.jpg">
        <img src="https://www.w3schools.com/css/img_lights.jpg" alt="Northern Lights" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here</div>
    </div>

    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_mountains.jpg">
        <img src="https://www.w3schools.com/css/img_mountains.jpg" alt="Mountains" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here</div>
    </div>

  </div>
</body>

</html>

上面的答案很有效,我想再补充一点。如果你想垂直对齐描述,你可以再次使用 Flexbox。我已经在此处的 CSS 代码中添加了它。

.gallery-container {
  display: flex;
  align-items: stretch;
  flex-wrap: wrap;
}

div.gallery {
  margin: 5px;
  border: 1px solid #ccc;
  width: 180px;
  /* To align the description both horizontally and vertically*/
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items:center;
}

div.gallery:hover {
  border: 1px solid #777;
}

div.gallery img {
  width: 100%;
  height: auto;
}

div.desc {
  padding: 15px;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <div class="gallery-container">
    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_5terre.jpg">
        <img src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here and make it really long to see what happens</div>
    </div>

    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_forest.jpg">
        <img src="https://www.w3schools.com/css/img_forest.jpg" alt="Forest" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here</div>
    </div>

    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_lights.jpg">
        <img src="https://www.w3schools.com/css/img_lights.jpg" alt="Northern Lights" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here</div>
    </div>

    <div class="gallery">
      <a target="_blank" href="https://www.w3schools.com/css/img_mountains.jpg">
        <img src="https://www.w3schools.com/css/img_mountains.jpg" alt="Mountains" width="600" height="400">
      </a>
      <div class="desc">Add a description of the image here</div>
    </div>

  </div>
</body>

</html>