如何将图像彼此相邻

时间:2018-10-23 19:47:00

标签: html css image photo-gallery

.boxGallery {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  margin-left: auto;
  margin-right: auto;
  width: 80%;
}

.gallery {
  min-width: 320px;
  width: 50%;
  margin: 12px;
  float: left;
}

.gallery img {
  width: 100%;
  height: auto;
  padding: 5px;
  border-radius: 12px;
}

.desc {
  padding: 15px;
  text-align: center;
}
<div class="boxGallery">
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>

  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
</div>

<div class="desc">
  <p>Auvergne, Frankrijk 2018</p>
</div>

<div class="boxGallery">
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>

  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
</div>

我正试图做一个照相馆。问题是我无法使图像彼此相邻。因此,我想要的是:我想要在照片的每个部分上方放置一个标题,该部分必须居中,并且多个图像应能够根据页面的大小彼此相邻。最后一件事是我无法上班(截图)。我真的不知道该如何解决。任何想法吗?

SCREENSHOT

2 个答案:

答案 0 :(得分:0)

如果您具有Bootstrap的基本知识。您可以使用以下示例制作照片库。 https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_img_thumbnail2&stacked=h

另一个例子

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<div class="row">
    <div class="col-md-4">
  <h2>Rounded Corners</h2>
  <p>The .img-rounded class adds rounded corners to an image (not available in IE8):</p>            
  <img src="cinqueterre.jpg" class="img-rounded" alt="Cinque Terre" width="304" height="236">
  </div>
  <div class="col-md-4">
  <h2>Rounded Corners</h2>
  <p>The .img-rounded class adds rounded corners to an image (not available in IE8):</p>            
  <img src="cinqueterre.jpg" class="img-rounded" alt="Cinque Terre" width="304" height="236">
  </div>
  <div class="col-md-4">
  <h2>Rounded Corners</h2>
  <p>The .img-rounded class adds rounded corners to an image (not available in IE8):</p>            
  <img src="cinqueterre.jpg" class="img-rounded" alt="Cinque Terre" width="304" height="236">
  </div>
  <div class="col-md-4">
  <h2>Rounded Corners</h2>
  <p>The .img-rounded class adds rounded corners to an image (not available in IE8):</p>            
  <img src="cinqueterre.jpg" class="img-rounded" alt="Cinque Terre" width="304" height="236">
  </div>
  <div class="col-md-4">
  <h2>Rounded Corners</h2>
  <p>The .img-rounded class adds rounded corners to an image (not available in IE8):</p>            
  <img src="cinqueterre.jpg" class="img-rounded" alt="Cinque Terre" width="304" height="236">
  </div>
</div>
</div>

</body>
</html>

答案 1 :(得分:0)

我认为您需要将max-width用作弹性项目的宽度,而不是min-width。我在下面的代码段中添加了额外的图像,以更好地演示该示例。如果您运行它并在足够宽的屏幕上展开到整页,则所有图像都显示在同一行中,但是当您减小屏幕尺寸时,它们将在彼此之间折叠。

.boxGallery {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  margin-left: auto;
  margin-right: auto;
  width: 80%;
}

.gallery {
  max-width: 320px;
  width: 50%;
  margin: 12px;
  float: left;
}

.gallery img {
  width: 100%;
  height: auto;
  padding: 5px;
  border-radius: 12px;
}

.desc {
  padding: 15px;
  text-align: center;
}
<div class="desc">
  <p>First Title and Gallery</p>
</div>

<div class="boxGallery">
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
</div>

<div class="desc">
  <p>Auvergne, Frankrijk 2018</p>
</div>

<div class="boxGallery">
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>

  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>
  <div class="gallery">
    <img src="https://placeimg.com/640/480/any">
  </div>

</div>