【问题标题】:Working with hover background Change on a DIV使用悬停背景更改 DIV
【发布时间】:2013-07-29 22:38:33
【问题描述】:

我一直在尝试实现将鼠标悬停在图片上的效果,它会使背景变暗并在顶部显示文本。

然后,当我将鼠标悬停时,我想要一个漂亮的集中文本。

这两件事都实现了,但后来我遇到了两个小问题。

  • 图片背景变暗时如何不让顶部的文字变暗
  • 如何在不指定图片宽度和高度的情况下集中文本(以启用响应式设计)

HTML

<div id="bottomWide">            
    <ul>
       <li class="first">
            <img src="http://127.0.0.1/www/media/wysiwyg/sub-head1.jpg" alt=""> 
            <div class="all-canvas">
                <div class="all-text">
                    <span class="title">A Heading</span><br>
                    <span class="text">Couples of Lines of Text will come here</span><br>
                    <span class="shop">See Details.</span>
                </div>
            </div>
        </li>
        <li class="second">
            <img src="http://127.0.0.1/www/media/wysiwyg/sub-head1.jpg" alt="">
                 <div class="all-canvas">
                <div class="all-text">
                    Some text here, style as needed
                </div>
            </div>
        </li>

    </ul> 
</div>

CSS

#bottomWide{
    width:100%;
    margin:0 auto;


}

#bottomWide ul{
    margin:0;
    padding:0;

}
#bottomWide li{
  list-style-type: none;
  display: inline-block;
    width:50%;
    text-align:justify;
    float: left;


     /* For Mouse Over*/
    position: relative;
    display: inline-block;
}
#bottomWide li:last-child img { 
   border-left: 4px solid #fff;
}
#bottomWide img{
    width:100%;
}



#bottomWide li div.all-canvas{
    position: absolute;
    top: 0;
    left: 0;
    /* 100% will equal the dimensions of the image, as nothing else will be inside the .container */
    width: 100%;
    height: 100%;
    color: #fff;
    background-color: #000;
    opacity: 0;
    /* This will create the fade effect */
    transition: opacity 0.3s;
    -webkit-transition: opacity 0.3s;
    /* Include all required vendor prefixes for opacity and transition */

    margin: 0 auto;
}
#bottomWide li:last-child div.all-canvas {
    margin-left: 4px;
}



#bottomWide li div.all-canvas:hover {
    opacity: 0.7;
}

#bottomWide li:last-child div.all-canvas:hover {
    opacity: 0.7;
    margin-left: 4px;
}

div.all-text {
    height:358px; /* This is what I want to change to %*/
    width: 623px; /* This is what I want to change to %*/ 
    text-align:center;
    border:1px solid silver;
    display: table-cell; 
    vertical-align:middle;
    margin: 0 auto; 
}

div.all-text span.title { padding: 3px; font: 18px/1.35 "Open Sans", Helvetica, sans-serif; text-transform: uppercase;}

输出看起来像这样:-

(注意图片顶部的暗淡文字;希望使它变得坚实) 谢谢。

【问题讨论】:

    标签: css background opacity


    【解决方案1】:

    如果您为元素设置不透明度,则所有子元素(包括文本)也将具有不透明度。您可以尝试使用 rgba 更改背景颜色:

    #bottomWide li div.all-canvas:hover {
        background-color: rgba(0,0,0, 0.7);
    }
    

    您仍然需要将不透明度从 0 切换到 1,这样它才能变得可见,但背景颜色应该是透明的。

    将文本居中会更加棘手,因为您正在尝试使其具有响应性。由于文本没有固定的高度,您可能必须使用 Javascript 来计算它。你可以从这个 CSS 开始:

    .theText {
        position: absolute;
        top: 50%;
        margin-top: -20px; // Should be half of the element height
    }
    

    您必须使用 javascript 来测量 .theText 的高度并相应地设置负边距顶部...

    【讨论】:

    • 谢谢。这行得通。知道如何在不指定图片宽度和高度的情况下集中文本(以启用响应式设计)
    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 2013-11-25
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多