CSS - 将<divs>放在透明的<div>的</div> </divs>上

时间:2013-11-06 07:14:06

标签: html css css3

<div>内部有透明度的情况下,有什么方法可以让内容/图片不透明吗?

这是HTML:

    <div id="main-button-wrapper" class="left">

        <div id="button-bg-layer" class="box-bg-layer corners"></div>

        <div class="buttons-bg-overlay box-bg-overlay corners">

            <img alt="Test" src="http://www.schroff.co.uk/railway/src/symbol_test.gif" />

        </div>

    </div>

CSS:

#main-button-wrapper {
    height: 319px;
    margin-left: 22px;
    position: relative;
    width: 321px;
}

#button-bg-layer {
    position: absolute;
    right: 0;
    height: 319px;
    width: 321px;
}

.buttons-bg-overlay {
    position: relative;
    right: 0;
    margin: 11px;
    height: 66px;
    width: 299px;
    text-align: center;
    padding-top: 26px;
}

#buttons-wrapper {
    position: relative;
    width: 299px;
    height: 297px;
    z-index: 3;
    margin: 22px;
}

/* Background Layers */

.box-bg-layer {
    background-color: #010101;
    z-index: 1;
    zoom: 1;
    filter: alpha(opacity=40);
    opacity: 0.4;   
}

.box-bg-overlay {
    background-color: #010101;
    z-index: 2;
    zoom: 1;
    filter: alpha(opacity=40);
    opacity: 0.4;   
}

我尝试在图片上添加z-index: 4;。我能想到的唯一另一种方法是将div背景设置为绝对定位,然后将内容定位在div之外但是必须有更简单的方法吗?

非常感谢任何帮助!

参见JSFiddle:

http://jsfiddle.net/Sa8jw/

2 个答案:

答案 0 :(得分:5)

而不是使用opacity使用rgba a代表alpha。这将使子元素不透明......

background-color: rgba(0,0,0,.5); /* RGBA for #010101 will be rgba(1,1,1,.4) */

.4的{​​{1}}等同于a

Demo

答案 1 :(得分:1)

这是一个帮助你的小提琴: - )

FIDDLE

和更改的代码。

之后将不透明度添加到伪类的位置
    #main-button-wrapper {
    height: 319px;
    margin-left: 22px;
    position: relative;
    width: 321px;
}

#button-bg-layer {
    position: absolute;
    right: 0;
    height: 319px;
    width: 321px;
}

.buttons-bg-overlay {
    position: relative;
    right: 0;
    margin: 11px;
    height: 66px;
    width: 299px;
    text-align: center;
    padding-top: 26px;
}

#buttons-wrapper {
    position: relative;
    width: 299px;
    height: 297px;
    z-index: 3;
    margin: 22px;
}

/* Background Layers */
.box-bg-layer{
background-color: #010101;
    z-index: 1;
zoom: 1;}
.box-bg-layer : after{

    filter: alpha(opacity=40);
    opacity: 0.4;   
}
.box-bg-overlay {   background-color: red;
    z-index: 2;
    zoom: 1;
}
.box-bg-overlay :after{
    filter: alpha(opacity=40);
    opacity: 0.4;   
}