Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to use CSS to fade in an overlay block

Tags:

css

http://jsfiddle.net/AndyMP/T5pX2/

.block {
    position: relative;
    margin: 25px;
    width: 300px;
    height: 300px;
    border: 1px solid black;
}
.overlay {
    width: 300px;
    height: 300px;
    background: yellow;
    opacity: 0;
}

<div class="block">
    <div class="overlay">
    </div>
</div>
like image 725
Andy Avatar asked Jul 28 '14 11:07

Andy


People also ask

How do you make a fade effect in CSS?

In the CSS, use the @keyframes rule paired with fadeIn. At 0%, set the opacity to 0. At 100%, set the opacity to 1. This creates the fade-in effect.

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 you overlay elements in CSS?

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 I fade text in CSS?

Use animation and transition property to create a fade-in effect on page load using CSS. Method 1: Using CSS animation property: A CSS animation is defined with 2 keyframes. One with the opacity set to 0, the other with the opacity set to 1.


1 Answers

What you have at the moment is not an overlay it's just one div inside another.

To make it an overlay you need to use position:absolute as follows:

JSfiddle Demo

CSS

.block {
    position: relative;
    margin: 25px;
    width: 300px;
    height: 300px;
    border: 1px solid black;
    padding:1em;
}
.overlay {
    background: rgba(0,0,0,0.5);
    opacity: 0;
    position: absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
    transition:opacity 0.5s ease;
}

.block:hover .overlay {
    opacity:1;
}
like image 200
Paulie_D Avatar answered Sep 26 '22 03:09

Paulie_D