【问题标题】:How can I add a caption beneath an image in a basic CSS lightbox?如何在基本 CSS 灯箱中的图像下方添加标题?
【发布时间】:2017-10-30 08:04:01
【问题描述】:

我正在使用以下代码来显示一个仅使用 CSS 的简单灯箱。 (我不想使用 Javascript)。

.lightbox {
  display: none;
  position: fixed;
  z-index: 999;
  width: 100%;
  height: 100%;
  text-align: center;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.8);
}

.lightbox img {
  position: fixed !important;
  top: 50% !important;
  transform: translateY(-50%) translateX(-50%);
  width: 70%;
}

.lightbox figcaption {
  display: inline;
  position: fixed!important;
  top: 70% !important;
  right: 5%;
  transform: translateX(0%);
  color: white;
}

@media (max-width: 500px) {
  .lightbox img {
    width: 90%;
  }
}

@media (min-width: 800px) {
  .lightbox img {
    width: 50%;
  }
}
<a href="#img1"><img class="img-thumbnail" src="img/sensei/kieflesson.jpg"></a>
<a href="#_" class="lightbox" id="img1">
  <figure><img src="img/sensei/kieflesson.jpg">
    <figcaption>Test22</figcaption>
  </figure>
</a>

如您所见,我一直在尝试使用 figcaption 在图像下方立即添加标题。我设法让它显示,但它没有响应。我希望它随着图像大小的变化而移动。

任何帮助将不胜感激。

【问题讨论】:

    标签: html css lightbox caption


    【解决方案1】:

    使用position: absolute 作为图片和标题。 position: fixed 可以使用包装器。现在你已经把 position: fixed 到所有的包装和图像上。

    【讨论】:

    • 我可以看看你用 position: absolute. 尝试过的代码吗?
    【解决方案2】:

    感谢您的帮助。看来我的代码有点过于复杂了。我使用以下方法达到了预期的效果:

    .lightbox {
      display: none;
      position: fixed;
      z-index: 999;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      background: rgba(0, 0, 0, 0.8);}
    
    .lightbox figure {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width:90%;
        max-width: 500px;}
    
    .lightbox img {
        width:100%;}
    
    .lightbox figcaption {
        bottom:0;
        position:absolute;
        color:white;
        width: 100%;
        max-width: 500px;
        text-align: right;
        padding-right: 10px;
        background: rgba(0, 0, 0, 0.5);}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多