Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic CSS - how to overlay a DIV with semi-transparent DIV on top

Tags:

html

css

I'm struggling to make this render right in my browser (Chrome). I have a wrapper holding all the elements of the HTML, and I want to have a DIV (lets call it div-1) that hold a image, and has a overlay div on top of it to the left, like I sketched in this picture...any quick solutions?

div bg with overlay semi transparent div

like image 471
TM23 Avatar asked Jul 25 '13 13:07

TM23


People also ask

How do I overlay one div on top of another?

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

How do you make a transparent overlay in CSS?

First, we create a <div> element (class="background") with a background image, and a border. Then we create another <div> (class="transbox") inside the first <div>. The <div class="transbox"> have a background color, and a border - the div is transparent.

How do I make a div semi transparent?

To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.

How do you overlay a div with an image in CSS?

Use z-index and top . This will layer the div on bottom, the image and then the span (overlay) on top. To set the positioning from the top edge, use top , which can be used with negative numbers if you need it to be higher on the Y axis than it's parent.


2 Answers

Here's a pure CSS solution, similar to DarkBee's answer, but without the need for an extra .wrapper div:

.dimmed {   position: relative; }  .dimmed:after {   content: " ";   z-index: 10;   display: block;   position: absolute;   height: 100%;   top: 0;   left: 0;   right: 0;   background: rgba(0, 0, 0, 0.5); } 

I'm using rgba here, but of course you can use other transparency methods if you like.

like image 189
marcvangend Avatar answered Nov 11 '22 03:11

marcvangend


Using CSS3 you don't need to make your own image with the transparency.

Just have a div with the following

position:absolute;
left:0;
background: rgba(255,255,255,.5);

The last parameter in background (.5) is the level of transparency (a higher number is more opaque).

Example Fiddle

like image 33
General_Twyckenham Avatar answered Nov 11 '22 04:11

General_Twyckenham