html / css完整背景覆盖另一个div内的div

时间:2015-03-08 10:38:04

标签: css image background overlay

我有一个简单的< div> (没有固定的hegiht),里面有文字:

<div id="section">
<div class="container">
<h1>text</h1>
<p>More text</p>
</div>
<!-- <div id="overlay"></div> -->
</div>

CSS的用法如下:

#section {
    background: red;
    overflow: hidden;
}

是否可以添加具有透明图像背景的div?

叠加sholud悬停在主红色背景上,但在文本下方。

我认为是这样的,但不起作用:

#section #overlay {
    position: relative;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100px; /* ??? */
    background: green;
    opacity: 0.1;
}

3 个答案:

答案 0 :(得分:1)

如果我正确地理解你,那么这样的事情如何:

<强> HTML:

<div id="section">
    <div id="container">
        <h1>My background is transparent!</h1>
        <p>More text</p>
    </div>
</div>

<强> CSS:

#section {
    width: 300px;
    background-color: red;
}

#container {
    position: relative;
    left: 50px;
    width: 200px;
    text-align: center;
    background-color: rgba(0,0,0,0.3);
}

此处的结果:JSFiddle

如果那不是你想要的,你能更具体地说明定位吗?

答案 1 :(得分:1)

#section {
background: red;
display: block;
position: relative;
overflow: hidden;
}
#container {
position: relative;
width: 100%;
text-align: center;
z-index: 9999;
}
#overlay {
position: absolute;
left: 0;
width: 100%;
height: 100%;
top: 0;
background-color: rgba(0,0,0,0.3);
}

答案 2 :(得分:0)

尝试下面的例子,使用jquery ui将减少工作量

    <div id="dialog">Your non-modal dialog</div>
<a href="#" id="open">Open dialog</a>

    $('#open').click(function() {
    $('#dialog').dialog('open');

});



jQuery(document).ready(function() {
    jQuery("#dialog").dialog({
        autoOpen: false,
        modal: true,
        open: function(){
            jQuery('.ui-widget-overlay').bind('click',function(){
                jQuery('#dialog').dialog('close');
            })
        }
    });
});