Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay with cut out rectangle mask

Tags:

css

overlay

Scenario: I want to cover an element by a rectangular mask (background #000 with opacity 0.8, for example). Next, there's an area in the covered area for which I want to highlight.

Here is a sample screenshot:

Overlay with rectangular cut out mask

(As you notice, there' s a rectangle cut from the darken mask. In Photoshop, it's easy - just place a layer on top and cut the rectangle out).

How could I make it work in CSS?

Side note: This kind of trick seems to be used a lot for "educating users about new features".

like image 396
hiro Avatar asked Oct 06 '15 05:10

hiro


People also ask

What is CSS mask?

The mask CSS shorthand property hides an element (partially or fully) by masking or clipping the image at specific points. Note: As well as the properties listed below, the mask shorthand also resets mask-border to its initial value.


2 Answers

You could use border or box-shadows for this. And use them on a pseudo element to minimize markup.

Example with box-shadows :

div{
  height:100%;
  position:relative;
}
.overlay{
  display:inline-block;
  position:relative;
  z-index: 1;
}
.overlay:after{
  content:'';
  position:absolute;
  top:0; left:0;
  width:100%; height:100%;
  box-shadow: 0px 0px 0px 9999px rgba(0,0,0,.85);
}
<div>
  <p>Some content</p>
  <p class="overlay">Other content</p>
  <p>More content</p>
</div>
like image 191
web-tiki Avatar answered Oct 19 '22 01:10

web-tiki


You can use CSS z-index to display the highlighted element above the overlay like this:

.overlay{
  display:none;
  position:absolute;
  background:rgba(0,0,0,0.8);
  width:100%;
  height:100%;
  top:0;
  left:0;
  z-index:999;
}

.highlight{
  position:relative;
  z-index:9999;
}

Live Demo

like image 1
EdenSource Avatar answered Oct 19 '22 01:10

EdenSource