【问题标题】:Text keeps being to the right of my image文字一直在我的图片右侧
【发布时间】:2018-10-23 09:01:40
【问题描述】:

我的问题

我正在尝试为每张图片制作一个带有标题的照片库。但我遇到的问题是标题一直漂浮在图像旁边。参考请看screenshot

CSS 片段

@media only screen and (max-width: 600px) {
  .boxGallery {
    margin-left: 50%;
    margin-right: 50;
  }
}

.GalleryBox {
  display: block;
  padding-left: 100px;
  padding-right: 100px;
  width: 100%;
}

.boxGallery {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 80%;
}

div.gallery {
  margin: 5px;
  float: left;
  width: 250px;
}

div.gallery img {
  width: 250px;
  height: 190px;
}

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

请提供有关如何解决此问题的任何帮助。

【问题讨论】:

    标签: html css photo-gallery


    【解决方案1】:

    因为您正在浮动.boxGallery 中的元素,所以父项会丢失其尺寸(因为它不再考虑浮动内容)。因为孩子是浮动的,所以标题被定位为浮动的“填充物”。

    您可以通过所谓的 clearfix 解决此问题。如今,clearfix 很简单:

    .clearfixme::after {
        content: '';
        clear: both;
        display: table;
    }
    

    @media only screen and (max-width: 600px) {
      .boxGallery {
        margin-left: auto;
        margin-right: auto;
      }
    }
    
    .GalleryBox {
      display: block;
      padding-left: 100px;
      padding-right: 100px;
      width: 100%;
    }
    
    .boxGallery {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 80%;
    }
    
    .boxGallery::after {
    content: '';
    clear: both;
    display: table;
    }
    
    div.gallery {
      margin: 5px;
      float: left;
      width: 250px;
    }
    
    div.gallery img {
      width: 250px;
      height: 190px;
    }
    
    div.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>

    或者,我会为此使用 flex。我会怎么做:

    .boxGallery {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-wrap: wrap;
    }
    
    .gallery {
      min-width: 120px;
      width: 25%;
      margin: 12px;
    }
    
    .gallery img {
      width: 100%;
      height: auto;
    }
    
    .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 class="gallery">
        <img src="https://placeimg.com/640/480/any">
      </div>
    </div>

    【讨论】:

    • 嘿,谢谢!我现在唯一遇到的问题是我希望根据页面宽度将多个图像彼此相邻并将这些图像居中。现在发生的事情是,在我的页面上,只有 2 张图片彼此相邻:imgur.com/a/MZ7Qeff。当页面较小时,图像似乎没有正确居中:imgur.com/a/81UQhPs。
    • @RaytjeKn 这是因为您的.boxGallery 中只有两张图片。只需添加更多。
    • 顺便说一句,我不认为你想要这个:` .boxGallery {margin-left: 50%;边距右:50; }` 50 无论如何都不是有效值。我想你想要margin: auto
    • 查看添加的第二个 sn-p
    • 我看了两个片段。它们似乎都可以工作,但我仍然存在它们未正确居中的问题:imgur.com/a/q5WZP4O.
    【解决方案2】:

    您有 .gallery 浮动,因此它从正常的 HTML 流中弹出,并且父级不知道子元素的实际高度。您的 .boxGallery 需要“clear-fix”来清除浮动元素的高度:

    .boxGallery:after {
        visibility: hidden;
        display: block;
        content: "";
        clear: both;
        height: 0;
    }
    

    更多信息here

    @media only screen and (max-width: 600px) {
      .boxGallery {
        margin-left: 50%;
        margin-right: 50;
      }
    }
    
    .boxGallery {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 80%;
    }
    
    .boxGallery:after {
      display: block;
      content: "";
      clear: both;
      position: relative;
    }
    
    div.gallery {
      margin: 5px;
      float: left;
      width: 250px;
    }
    
    div.gallery img {
      width: 250px;
      height: 190px;
    }
    
    div.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>

    【讨论】:

    • 嘿,谢谢!我现在唯一遇到的问题是我希望根据页面宽度将多个图像彼此相邻并将这些图像居中。现在发生的事情是我的页面上只有 2 张图片彼此相邻:imgur.com/a/MZ7Qeff。当页面较小时,图像似乎没有正确居中:imgur.com/a/81UQhPs.
    【解决方案3】:

    由于您已浮动图像,因此它们已脱离正常流程,.boxGallery 元素的行为就像它们不再存在并且仅确认 .desc 元素。要解决此问题,您需要添加一个明确的修复程序,如下所示:

    .boxGallery::after {
      content: "";
      display: table;
      clear: both;
    }
    

    你可以阅读更多关于'clearfixing' here

    为了使图像居中,在断点处添加一个媒体查询,其中视口不够大,无法让图像彼此相邻我发现当视口低于 666px 时就是这种情况,此时删除浮动并居中带边距的图像。

    @media screen and (max-width: 666px) {
      div.gallery {
        float: none;
        margin-left: auto;
        margin-right: auto;
      }
    }
    

    请看下面的演示:

    @media only screen and (max-width: 600px) {
      .boxGallery {
        margin-left: 50%;
        margin-right: 50%;
      }
    }
    
    .GalleryBox {
      display: block;
      padding-left: 100px;
      padding-right: 100px;
      width: 100%;
    }
    
    .boxGallery {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 80%;
    }
    
    .boxGallery::after {
      content: "";
      display: table;
      clear: both;
    }
    
    div.gallery {
      margin: 5px;
      float: left;
      width: 250px;
    }
    
    div.gallery img {
      width: 250px;
      height: 190px;
    }
    
    div.desc {
      padding: 15px;
      text-align: center;
    }
    
    @media screen and (max-width: 666px) {
      div.gallery {
        float: none;
        margin-left: auto;
        margin-right: auto;
      }
    }
    <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>

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2014-09-24
      • 2017-05-14
      相关资源
      最近更新 更多