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:
(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".
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.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With