skip to Main Content

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

3

Answers


  1. This Can be done only in webkit at the moment. using the property -webkit-mask-box-image. other than that we can’t achieve this only using css.

    Login or Signup to reply.
  2. 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>
    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search