CSS:让文本显示在div中的渐变上方

时间:2015-04-06 01:55:47

标签: html css background z-index gradient

我正在新闻聚合网站的主页上工作,我们应该有几个区域可以在背景中提取内容,文本在顶部,两者之间有渐变。在大多数情况下,我有它,除非z-index较低,渐变仍然出现在文本框上方。我已经在坚实的背景上进行了实验,以确保这一点。代码和示例如下:http://jsfiddle.net/cx0uvshd/

<style type="text/css">
    .feature {
        position: relative;
        float: left; 
        width: 465px; 
        height: 170px;  
        margin-top: 24px; 
        margin-right: 30px; 
    }
    .feature.last { 
        margin-right: 0; 
    }
    .feature-bottom {
        background: none;
        position: absolute;
        bottom: 0;
        left: 0;
        padding: 0 30px 6px;
        width: 100%;
        z-index: 200;
        line-height: 1;
    }
    .feature-bottom::after {
        content: "";
        position: absolute;
        bottom: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
        z-index: 100;
        background: rgba(0,0,0,0);
        background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,0.5)), color-stop(100%, rgba(0,0,0,0.5)));
        background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: -o-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#000000', GradientType=0 );
    }
    .feature-bottom h3 { 
        color: #FFF; 
        font-size: 15px; 
        font-weight: 400; 
        margin: 0; 
    }
    .feature-bottom h2 { 
        color: #FFF; 
        font-size: 24px; 
        font-weight: 400; 
        margin: 0; 
    }
</style>

3 个答案:

答案 0 :(得分:0)

将(位置:相对)和(z-index:201)添加到功能底部h3和h2。查找以下修订代码:

.feature { position: relative; float: left; width: 465px; height: 170px;  margin-top: 24px; margin-right: 30px; }
.feature.last { margin-right: 0; }
.feature-bottom {
    background: none;
    position: absolute;
    bottom: 0;
    left: 0;
    padding: 0 30px 6px;
    width: 100%;
    z-index: 200;
    line-height: 1;
}
.feature-bottom::after {
    content: "";
    position: absolute;
    bottom: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 100;
    background: rgba(0,0,0,0);
    background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,0.5)), color-stop(100%, rgba(0,0,0,0.5)));
    background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: -o-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#000000', GradientType=0 );
}
.feature-bottom h3 { position:relative; z-index:201; color: #FFF; font-size: 15px; font-weight: 400; margin: 0; }
.feature-bottom h2 { position:relative; z-index:201; color: #FFF; font-size: 24px; font-weight: 400; margin: 0; }

答案 1 :(得分:0)

为什么不将渐变应用于.feature-bottom?像这样: http://jsfiddle.net/cx0uvshd/2/

在当前版本中,:before元素覆盖尽管z-index更大。您需要new stacking context

Here's a similar question

答案 2 :(得分:0)

一个不那么干净的解决方案是添加另一个div,其内容低于渐变效果。然后给该div一个类,它是feature-bottom的副本。然后将feature-bottom的颜色设置为透明以隐藏文本。还要更改最后两个选择器以使用副本类。同时复制用于新类的最后两个选择器。 JsFiddle

CSS:

    .feature { position: relative; float: left; width: 465px; height: 170px; margin-top: 24px; margin-right: 30px; }
    .feature.last { margin-right: 0; }

/*Copy of feature bottom*/
.feature-bottom2 {
background: none;
position: absolute;
bottom: 0;
left: 0;
padding: 0 30px 6px;
width: 100%;
z-index: 200;
line-height: 1;
}
.feature-bottom {
    color: transparent;
background: none;
position: absolute;
bottom: 0;
left: 0;
padding: 0 30px 6px;
width: 100%;
z-index: 200;
line-height: 1;
}
.feature-bottom::after {
content: "";
position: absolute;
bottom: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 100;
background: rgba(0,0,0,0);
background: -moz-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: -webkit-gradient(left top, left bottom, color-stop(0%, rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,0.5)), color-stop(100%, rgba(0,0,0,0.5)));
background: -webkit-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: -o-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: -ms-linear-gradient(top, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.5) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#000000',  GradientType=0 );
}
/* Copy of below*/
.feature-bottom2 h3 { color: #FFF; font-size: 15px; font-    weight: 400; margin: 0; }
.feature-bottom2 h2 { color: #FFF; font-size: 24px; font-weight: 400; margin: 0; }

.feature-bottom h3 { font-size: 15px; font-weight: 400; margin: 0; }
.feature-bottom h2 { font-size: 24px; font-weight: 400; margin: 0; }