【问题标题】:Colour Overlay On Hover - With Text [duplicate]悬停时的颜色叠加 - 带文本 [重复]
【发布时间】:2018-12-05 19:08:23
【问题描述】:

我有一系列带有背景图像的响应框,我正在尝试对其应用悬停覆盖。我希望它在悬停时具有文本/图像和彩色背景。悬停时,背景图像仍应可见,但带有带有文本和徽标的彩色覆盖层。

我不想使用 JS,而且我知道有一种 CSS 方法可以做到这一点,但我似乎无法让它与我所拥有的完全正确地工作。我已经以这种方式设置了这些框以确保它们能够响应 - 也许有更好的方法来做到这一点?

.matrix{
     display:block;
     overflow:hidden;
}
 .matrix .item{
     float:left;
     width:25%;
     position:relative;
}
 .matrix .item a, .matrix .item.team{
     display:block;
     position:relative;
     overflow:hidden;
}
 .matrix .item img{
     display:block;
     transition:all 0.3 ease;
     width:100%;
}
 .matrix .bleed-in{
     position:absolute;
     right:150%;
     top:-150%;
     width:150%;
     height:300%;
     transition:all 0.35s ease 0.1s;
     z-index:5;
     opacity:0.9;
     transform:rotate(30deg);
}
 .matrix .item .overlay{
     display:block;
     transition:all 0.25s ease 0.1s;
     position:absolute;
     leftL0;
     top:-10px;
     width:100%;
     height:100%;
     opacity:0;
     text-align:center;
     padding:0 10%;
     z-index:9;
}
 .vertical-align{
     position:relative;
     top:50%;
     transform:translateY(-50%);
}
 .matrix .item .overlay img{
     max-height:70px;
     width:auto;
     opacity:1;
     display:inline-block;
     margin: 0 auto 25px;
     transform:none;
}
 .matrix .item h3{
     color:#fff;
     margin:0 0 6px;
     font-size:20px;
     font-weight:500;
}
 .matrix .item p{
     color:#fff;
     margin:0;
     text-transform:uppercase;
     font-size:1.6rem;
}
 @media screen and (max-width:600px){
     .matrix .item{
         width:100%;
         float:none;
    }
}
 @media screen and (max-width:1024px){
     .matrix .item{
         width:50%;
    }
}
<section class="matrix bkg-grey">
   <article class="item" style="background-color: #0058cf">
      <a href="#">
         <img src="https://via.placeholder.com/300.png/09f/fff" alt="">
         <div class="bleed-in" style="background-color: #0058cf"></div>
         <div class="overlay">
            <div class="vertical-align">
               <img src="https://via.placeholder.com/300.png/09f/fff">
               <h3>1</h3>
               <p>Branding &amp; Strategy</p>
            </div>
         </div>
      </a>
   </article>
   <article class="item" style="background-color: #0058cf">
      <a href="#">
         <img src="https://via.placeholder.com/300.png/09f/fff" alt="">
         <div class="bleed-in" style="background-color: #0058cf"></div>
         <div class="overlay">
            <div class="vertical-align">
               <img src="https://via.placeholder.com/300.png/09f/fff">
               <h3>1</h3>
               <p>Branding &amp; Strategy</p>
            </div>
         </div>
      </a>
   </article>
   <article class="item" style="background-color: #0058cf">
      <a href="#">
         <img src="https://via.placeholder.com/300.png/09f/fff" alt="">
         <div class="bleed-in" style="background-color: #0058cf"></div>
         <div class="overlay">
            <div class="vertical-align">
               <img src="https://via.placeholder.com/300.png/09f/fff">
               <h3>1</h3>
               <p>Branding &amp; Strategy</p>
            </div>
         </div>
      </a>
   </article>
   <article class="item" style="background-color: #0058cf">
      <a href="#">
         <img src="https://via.placeholder.com/300.png/09f/fff" alt="">
         <div class="bleed-in" style="background-color: #0058cf"></div>
         <div class="overlay">
            <div class="vertical-align">
               <img src="https://via.placeholder.com/300.png/09f/fff">
               <h3>1</h3>
               <p>Branding &amp; Strategy</p>
            </div>
         </div>
      </a>
   </article>
</section>

我尝试添加一个带有叠加层的容器,但也没有用。

 .container {
     position: relative;
     height:483px;
     width:555px;
     background-image:url("https://via.placeholder.com/300.png/09f/fff");
     background-size:cover;
     background-repeat:no-repeat;
}
 .overlay {
     position: absolute;
     top: 0;
     bottom: 0;
     left: 0;
     right: 0;
     height: 100%;
     width: 100%;
     opacity: 0;
     transition: .5s ease;
     background-color: rgba(0,40,80,0.8);
}
 .container:hover .overlay {
     opacity: 1;
}

【问题讨论】:

    标签: html css hover overlay


    【解决方案1】:

    首先,您的代码很混乱。它没有正确缩进并且有很多不必要的代码。我想将此作为评论发布,但我的声誉还不够。

    它不起作用,因为 .overlay 应该是图像的兄弟,绝对定位于其父级 (.container) .因此,当您将鼠标悬停在其中时,容器会显示最初隐藏在其中的内容。

    <div>
        <!-- first item -->
        <div class="container">
            <img src="https://via.placeholder.com/300.png/09f/fff" alt="">
            <div class="overlay">
                <p>Content here</p>
            </div>  
        </div>
    
        <!-- second item -->
        <div class="container">
            <img src="https://via.placeholder.com/300.png/09f/fff" alt="">
            <div class="overlay">
                <p>Content here</p>
            </div>  
        </div>
    </div>
    

    这是 CSS 的样子。

    .container {
        width: 300px;
        height: 300px;
        position: relative;
        display: inline-block;
    }
    .overlay {
        position: absolute;
        opacity: 0;
        transition: .5s ease;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }
    .container:hover .overlay {
        opacity: 0.8;
        background: #fff;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 2021-04-19
      • 1970-01-01
      • 2011-03-03
      • 2021-04-18
      相关资源
      最近更新 更多