CSS Overlay with Pseudo Element

How to create CSS overlay with pseudo element?

.modal {
     position:fixed;
     top:100px;
     margin-left: auto;
     margin-right: auto;
     left:0;
     right:0;
     width:500px;
     display:none;
     border:2px solid #736D61;
     background:#fff;
     padding:10px;
}
.modal:after {
     position:fixed;
     top:0px;
     left:0px;
     width:100%;
     height:100%;
     background:rgba(0,0,0,0.5);
}

I tried this, but it does not work.

+4
source share
1 answer

It probably doesn't work because the pseudo-element is not generated if the value is contentomitted . The default contentvalue is equal none, which probably means you are not seeing the pseudo-element. Therefore, you need to specify a value other than nonefor the property content:

.modal:after {
  content: '';
  position: fixed;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}

, , , .modal, . , - :before .modal, :

.modal {
  position: fixed;
  top: 100px;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
  width: 500px;
  border: 2px solid #736D61;
  background: #fff;
  padding: 10px;
}

.modal-overlay:before {
  content: '';
  position: fixed;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}
<div class="modal-overlay">
  <div class="modal">MODAL</div>
</div>
Hide result
+4

Source: https://habr.com/ru/post/1622647/


All Articles