【问题标题】:HTML/CSS: Allow For Background Image Hover and Absolute Positioned TextHTML/CSS:允许背景图像悬停和绝对定位文本
【发布时间】:2021-09-09 03:22:48
【问题描述】:

我正在尝试完成以下工作:

  • 拥有灰度图像
  • 在灰度图像上放置文本和按钮
  • 允许对灰度图像进行悬停效果,使其在悬停时着色

我遇到的问题是,当我将鼠标悬停在图像顶部绝对定位的 div 的文本/按钮上时,悬停效果不再起作用。

这是一个例子:

这是我的 HTML 和 CSS:

<div id="homepage-solutions">
  <div class="image-box">
    <img src="https://azbigmedia.com/wp-content/uploads/2019/11/metalworking.jpg">
    <div class="text-box">
      <h3>Metalworking</h3>
      <a href="comingsoon" class="btn">Learn More</a>
    </div>
  </div>
</div>

#homepage-solutions .image-box {
    display: inline-block;
    position: relative;
    overflow: hidden;
    min-width: 250px;
    min-height: 250px;
    max-width: 100%;
    max-height: 100%;
    text-align: center;
    list-style: none;
    vertical-align: middle;
}
#homepage-solutions .image-box img {
    display: block;
    height: 200px;
    width: 100%;
    filter: gray;
    -webkit-filter: grayscale(1);
    filter: grayscale(1);
}
#homepage-solutions .image-box img:hover {
    -webkit-filter: grayscale(0);
    filter: none;
}
#homepage-solutions .text-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 2em;
    text-align: center;
    max-width: 500px;
}
#homepage-solutions h3 {
    color: #fff;
    font-size: 22px;
    font-weight: 700;
    margin-top: 0;
    margin-bottom: 10px;
    text-align: center;
}
#homepage-solutions .text-box p {
    color: #fff;
    font-size: 12px;
    font-weight: 400;
    margin: 10px 0;
}
#homepage-solutions .text-box a.btn {
    display: inline-block;
    width: auto;
    color: #fff;
    background: #fd5f11;
    font-size: 13px;
    font-weight: 700;
    text-decoration: none;
    text-transform: uppercase;
    padding: 6px 20px;
    margin: 5px 0 0;
    border-radius: 0;
}
#homepage-solutions .text-box a.btn:hover {
  opacity: 0.75;
}

小提琴链接: https://jsfiddle.net/mwilk/96p7nkw1/5/https://jsfiddle.net/mwilk/96p7nkw1/6/#&togetherjs=H2kAkloCwG

谢谢!

【问题讨论】:

    标签: html css


    【解决方案1】:

    只需编辑选择器:

    #homepage-solutions .image-box img:hover
    

    #homepage-solutions .image-box:hover img
    

    看看它的工作原理

    #homepage-solutions .image-box {
      display: inline-block;
      position: relative;
      overflow: hidden;
      min-width: 250px;
      min-height: 250px;
      max-width: 100%;
      max-height: 100%;
      text-align: center;
      list-style: none;
      vertical-align: middle;
    }
    #homepage-solutions .image-box img {
      display: block;
      height: 200px;
      width: 100%;
      filter: gray;
      -webkit-filter: grayscale(1);
      filter: grayscale(1);
    }
    #homepage-solutions .image-box:hover img {
      -webkit-filter: grayscale(0);
      filter: none;
    }
    #homepage-solutions .text-box {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 2em;
      text-align: center;
      max-width: 500px;
    }
    #homepage-solutions h3 {
      color: #fff;
      font-size: 22px;
      font-weight: 700;
      margin-top: 0;
      margin-bottom: 10px;
      text-align: center;
    }
    #homepage-solutions .text-box p {
      color: #fff;
      font-size: 12px;
      font-weight: 400;
      margin: 10px 0;
    }
    #homepage-solutions .text-box a.btn {
      display: inline-block;
      width: auto;
      color: #fff;
      background: #fd5f11;
      font-size: 13px;
      font-weight: 700;
      text-decoration: none;
      text-transform: uppercase;
      padding: 6px 20px;
      margin: 5px 0 0;
      border-radius: 0;
    }
    #homepage-solutions .text-box a.btn:hover {
      opacity: 0.75;
    }
        <div id="homepage-solutions">
          <div class="image-box">
            <img
              src="https://azbigmedia.com/wp-content/uploads/2019/11/metalworking.jpg"
            />
            <div class="text-box">
              <h3>Metalworking</h3>
              <a href="comingsoon" class="btn">Learn More</a>
            </div>
          </div>
        </div>

    【讨论】:

    • OH MY.... 是的,这样就可以了。哇。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多