覆盖仅覆盖父div

时间:2014-08-20 16:12:44

标签: html css

我在div中有一组控件(列表框和下面的按钮),我想用半透明覆盖层覆盖它们(其中有一个居中的加载指示器)。

父div的大小和位置不固定。

以下是一个示例:http://jsfiddle.net/n4fbp8ex/   - 感兴趣的区域在红色边界内。我喜欢用覆盖物覆盖它。   - "正在加载......" div是我的叠加层。它应该覆盖整个父div(但没有别的),文本应该垂直和水平居中

如何修复我的" centeredOverlay"风格?

HTML:     

<div class="halfColumn">
    some content
    <br/>
    <input />
</div>
<div class="halfColumn">
    bla bla
    <br/>
    lorem ipsum
    <div style="border-style:solid; border-width: 1px; border-color: red;">
        <div class="centeredOverlay">Loading...</div>
        <select size=2 style="width:100%; height:50vh">
        </select>
        <button>Click me</button>
        <button>Click me too</button>

    </div>
</div>
<div style="clear:both"></div>
</div>

的CSS:

.centeredOverlay {
  position: absolute;
  background-color: rgba(0,0,0,0.3); /*dim the background*/
}

.halfColumn {
    float: left;
    width: 50%;
}

.halfColumn2 {
    float: right;
    width: 50%;
}

3 个答案:

答案 0 :(得分:10)

我认为这就是你要找的......尽管文字还没有定位。

JSfiddle

编辑 - JSfiddle with new span element to center text

<强> HTML

<div class="main">
    <div class="halfColumn">some content
        <br/>
        <input />
    </div>
    <div class="halfColumn">bla bla
        <br/>lorem ipsum
        <div class="wrapper">
            <div class="centeredOverlay">Loading...</div>
            <select size=2 style="width:100%; height:50vh"></select>
            <button>Click me</button>
            <button>Click me too</button>
        </div>
    </div>
    <div style="clear:both"></div>
</div>

<强> CSS

.centeredOverlay {
    position: absolute;
    background-color: rgba(255, 0, 0, 0.3);
    /*dim the background*/
    top:0;
    left:0;
    width:100%;
    height:100%;
    color:black;
    text-align: center;
}
.halfColumn {
    float: left;
    width: 50%;
}
.wrapper {
    position: relative;
    border-style:solid;
    border-width: 1px;
    border-color: red;
}
.halfColumn2 {
    float: right;
    width: 50%;

}

答案 1 :(得分:4)

这是你要找的吗?

.overlayContainer{
    position:relative;

}

.overlay {
  position: absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
    opacity:0.3;
    background:#b3b3b3;
    text-align:center;
}

http://jsfiddle.net/n4fbp8ex/4/

答案 2 :(得分:1)

commands 属性是 still not widely supported(Safari 和 Edge 落后,像往常一样)但看看它会多么酷:

inset
.parent-div {
  height: 200px;
  width: 200px;
  background: red;
  position: relative;
}

.child-div {
  background: rgba(0,0,255,0.5); 
  position: absolute;
  inset: 0;
}

子元素只需要 2 个 CSS 属性:

<div class="parent-div">
  <p>blablabla</p>
  <div class="child-div"></div>  
</div>

position: absolute; inset: 0; 实际上只是

的简写变体
inset: 0