【问题标题】:flexbox text overlay when hovering an image [duplicate]悬停图像时的弹性框文本覆盖[重复]
【发布时间】:2021-07-07 04:18:20
【问题描述】:

我目前正在尝试将 flexbox 用于我网站上的画廊...我遇到了一些问题,但这是唯一一个我无法弄清楚如何解决的问题。可能我可以对缺少的/可能做错的事情有不同的看法或方向。当我将鼠标悬停在图像上时,我的文本当前超出了它应该出现在图像上的位置。

我在这个 CodePen 中重现了我的问题: https://codepen.io/vinlune/pen/YzNeeyV

或者你可以在下面查看它——
html

<section class="galleryContainer">
  <div class="galleryCol">
  <div class="gallContent">
    <a href="websitepage.html"><img src="https://media2.giphy.com/media/lnCvspqqhwzGMxmf8R/giphy.gif?cid=ecf05e47o1cwbqk74f1e9am33o60ojpf26lmdebbsuz8h5tw&rid=giphy.gif">
      <div class="hoverText">
        title <br /> content #1</div>
      <p>cute corgi eating sushi</p></a>
  </div>
  
<div class="gallContent">
    <a href="websitepage.html"><img src="https://media3.giphy.com/media/SBiRK1eROiAHe0bg3A/giphy.gif">
      <div class="hoverText">
        title <br /> content #1</div>
      <p>cute corgi drinking boba</p></a>
  </div>
  
  <div class="gallContent">
    <a href="websitepage.html"><img src="https://media1.giphy.com/media/Kd5XdzdEhNqhYWe14S/giphy.gif">
      <div class="hoverText">
        title <br /> content #1</div>
      <p>cute corgi eating cake</p></a>
  </div>
  </div>
</section>

css

a { /*  */
  text-decoration:none;
}

section.galleryContainer {
  width: 92%;
  height: auto;
  padding: 0;
  margin: 0 auto;
}

section.galleryContainer .galleryCol {
  margin: 0 auto;
  width: 100%;
  display: flex;
  justify-content: center;
  flex-direction: row;
  flex-wrap: wrap;
  flex: 1 auto;
}
section.galleryContainer .galleryCol .gallContent {
  width: 32%;
  height: auto;
  margin: auto; 
  padding: 4px;
  background-color: #efefef;
}
section.galleryContainer .galleryCol .gallContent img{
  width: 100%;
  height: auto;
  postion: relative;
  display: block;
  opacity: 1;
  backface-visibility: hidden;
  transition: 0.5s;
}
section.galleryContainer .galleryCol .gallContent:hover img {
  background-color: #635ca8;
  opacity: 0.2;
}
.galleryCol .gallContent .hoverText {
  display: none;
  text-align: center;
  align-items: center;
  transition: 0.5s;
}

.galleryCol .gallContent:hover .hoverText {
  display: flex;
  position: absolute;
  flex-direction: row;
  justify-content: center;
  width: 100%;
  height: 100%;
  top: 25%;
  opacity: 1;
  z-index: 3;
}

感谢您的帮助!

【问题讨论】:

    标签: html css image flexbox overlay


    【解决方案1】:

    您需要将 position: relative; 添加到您的 hoverText 元素的父级。

    我假设您希望 hoverText 位于 gallContent 元素内,因此将 position: relative; 添加到 gallContent CSS 定义中

    section.galleryContainer .galleryCol .gallContent {
      width: 32%;
      height: auto;
      margin: auto; 
      padding: 4px;
      background-color: #efefef;
      position: relative;
    }
    

    所以现在hoverText 将与gallContent 元素父元素“相对”

    【讨论】:

      【解决方案2】:

      您需要在section.galleryContainer .galleryCol .gallContent 类上定义position:relative

      检查下面的 sn-p 会很高兴。

      a { /*  */
        text-decoration:none;
      }
      
      section.galleryContainer {
        width: 92%;
        height: auto;
        padding: 0;
        margin: 0 auto;
      }
      
      section.galleryContainer .galleryCol {
        margin: 0 auto;
        width: 100%;
        display: flex;
        justify-content: center;
        flex-direction: row;
        flex-wrap: wrap;
        flex: 1 auto;
      }
      section.galleryContainer .galleryCol .gallContent {
        width: 32%;
        height: auto;
        margin: auto; 
        padding: 4px;
        background-color: #efefef;
        position: relative;
      }
      section.galleryContainer .galleryCol .gallContent img{
        width: 100%;
        height: auto;
        postion: relative;
        display: block;
        opacity: 1;
        backface-visibility: hidden;
        transition: 0.5s;
      }
      section.galleryContainer .galleryCol .gallContent:hover img {
        background-color: #635ca8;
        opacity: 0.2;
      }
      .galleryCol .gallContent .hoverText {
        display: flex;
        position: absolute;
        text-align: center;
        flex-direction: row;
        justify-content: center;
        align-items: flex-end;
        width: 100%;
        height: 80%;
        top: 0;
        left: 0;
        z-index: 3;
        transition: 0.5s;
        transform: scale(0);
        transform-origin: bottom;
      }
      
      .galleryCol .gallContent:hover .hoverText {
        transform: scale(1);
        box-shadow: inset 0 0 0 1px red;
      }
      <section class="galleryContainer">
        <div class="galleryCol">
          <div class="gallContent">
            <a href="websitepage.html"><img src="https://media2.giphy.com/media/lnCvspqqhwzGMxmf8R/giphy.gif?cid=ecf05e47o1cwbqk74f1e9am33o60ojpf26lmdebbsuz8h5tw&rid=giphy.gif">
              <div class="hoverText">
                title <br /> Lorem ispsome dolloar ispsome dolloar dolloar ispsome dolloar and added more content #1
              </div>
              <p>cute corgi eating sushi</p>
            </a>
          </div>
          <div class="gallContent">
            <a href="websitepage.html"><img src="https://media3.giphy.com/media/SBiRK1eROiAHe0bg3A/giphy.gif">
              <div class="hoverText">
                title <br /> content #2
              </div>
              <p>cute corgi drinking boba</p>
            </a>
          </div>
          <div class="gallContent">
            <a href="websitepage.html"><img src="https://media1.giphy.com/media/Kd5XdzdEhNqhYWe14S/giphy.gif">
              <div class="hoverText">
                title <br /> content #3
              </div>
              <p>cute corgi eating cake</p>
            </a>
          </div>
        </div>
      </section>

      【讨论】:

        【解决方案3】:

        position 属性在此处执行此操作。 由于您已将position:absolute 设置为hoverText div 元素,因此您必须在其父元素上设置position:relative 属性,即gallContent div。

        试试这个代码:

        a {
            text-decoration:none;
          }
        
          section.galleryContainer {
            width: 92%;
            /* height: auto; */
            padding: 0;
            margin: 0 auto;
          }
        
          section.galleryContainer .galleryCol {
            margin: 0 auto;
            width: 100%;
            display: flex;
            justify-content: center;
            flex-direction: row;
            flex-wrap: wrap;
            flex: 1 auto;
          }
          section.galleryContainer .galleryCol .gallContent {
            width: 32%;
            /* height: auto; */
            margin: auto;
            padding: 4px;
            background-color: #efefef;
            position: relative;
          }
          section.galleryContainer .galleryCol .gallContent img{
            width: 100%;
            /* height: auto; */
            display: block;
            opacity: 1;
            /* backface-visibility: hidden; */
            transition: 0.5s;
          }
          section.galleryContainer .galleryCol .gallContent:hover img {
            background-color: #635ca8;
            opacity: 0.2;
          }
          .galleryCol .gallContent .hoverText {
            display: none;
            text-align: center;
            align-items: center;
            position: absolute;
            /* flex-direction: row; */
            justify-content: center;
            width: 100%;
            height: 100%;
            top: 0;
            opacity: 1;
            z-index: 3;
            transition: 0.5s;
          }
        
          .galleryCol .gallContent:hover .hoverText {
            display: flex;
          }
        <section class="galleryContainer">
            <div class="galleryCol">
              <div class="gallContent">
                <a href="websitepage.html">
                  <img src="https://media2.giphy.com/media/lnCvspqqhwzGMxmf8R/giphy.gif?cid=ecf05e47o1cwbqk74f1e9am33o60ojpf26lmdebbsuz8h5tw&rid=giphy.gif">
                  <div class="hoverText">
                    title <br /> content #1
                  </div>
                  <p>cute corgi eating sushi</p>
                </a>
              </div>
        
              <div class="gallContent">
                <a href="websitepage.html">
                  <img src="https://media3.giphy.com/media/SBiRK1eROiAHe0bg3A/giphy.gif">
                  <div class="hoverText">
                    title <br /> content #1
                  </div>
                  <p>cute corgi drinking boba</p>
                </a>
              </div>
        
              <div class="gallContent">
                <a href="websitepage.html">
                  <img src="https://media1.giphy.com/media/Kd5XdzdEhNqhYWe14S/giphy.gif">
                  <div class="hoverText">
                    title <br /> content #1
                  </div>
                  <p>cute corgi eating cake</p>
                </a>
              </div>
            </div>
          </section>

        我还注释掉了一些我认为不应该影响你想要达到的目标的属性。

        如果您想了解更多信息,可以查看职位教程。

        https://www.w3schools.com/css/css_positioning.asp

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-28
          • 2017-10-20
          • 1970-01-01
          • 1970-01-01
          • 2017-01-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多