Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay that covers only parent div

Tags:

html

css

I have a group of controls inside a div (listbox and buttons below), and I'd like to cover them with a semi-transparent overlay (with a centered loading indicator in it).

Parent div size and position are not fixed.

Here is an example: http://jsfiddle.net/n4fbp8ex/ - Area of interest is inside red border. I'd like it to be covered with an overlay. - "Loading..." div is my overlay. It should cover whole parent div (but nothing else), and text should be centered vertically and horizontally

How do I fix my "centeredOverlay" style?

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%;
}
like image 549
Pavel Tupitsyn Avatar asked Aug 20 '14 16:08

Pavel Tupitsyn


People also ask

How do I overlay a div on top of another div?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

What is div Overlay?

Overlay means to cover the surface of something with a coating. In other words, it is used to set one thing on the top of another. The overlay makes a web-page attractive, and it is easy to design. Creating an overlay effect means to put two div together at the same place, but both will appear when required.

How can a child div fit parent div?

Method 1: First method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. Consider this HTML for demonstration: HTML.


3 Answers

I think this is what you are looking for..although the text has not yet been positioned.

JSfiddle

EDIT - JSfiddle with new span element to center text

.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%;
}
<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>
like image 99
Paulie_D Avatar answered Sep 29 '22 05:09

Paulie_D


Is this what you're looking for?

.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/

like image 22
Frisbetarian Avatar answered Sep 29 '22 07:09

Frisbetarian


The inset property is still not widely supported (Safari and Edge lagging behind, as usual) but look how cool it's going to be:

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

.child-div {
  background: rgba(0,0,255,0.5); 
  position: absolute;
  inset: 0;
}
<div class="parent-div">
  <p>blablabla</p>
  <div class="child-div"></div>  
</div>

The child element only needs 2 CSS properties:

position: absolute;
inset: 0;

inset: 0 really is just a shorthand variation of

top:0;
bottom:0;
right:0;
left:0;
like image 34
Félix Paradis Avatar answered Sep 29 '22 07:09

Félix Paradis