How to fade the edge of a div with just CSS? [duplicate]

You could overlap a pseudoelement with a gradient applied on it

ol {
  border: 1px #d8d8d8 dashed;
  font: 2em/1.6em Arial;
  position: relative;
}

ol:after {
  content: "";
  position: absolute;
  z-index: 1;
  bottom: 0;
  left: 0;
  pointer-events: none;
  background-image: linear-gradient(to bottom, rgba(255,255,255,0), rgba(255,255,255, 1) 90%);
  width: 100%;
  height: 4em;
}
<ol>
  <li>item1</li>
  <li>item2</li>
  <li>item3</li>
  <li>item4</li>
  <li>item5</li>
</ol>

Since pointer-events is set to none the text under the gradient will be also selectable/clickable.


Update 03.2023

The same effect can be achieved with the mask property

ol {
  --mask: linear-gradient(to bottom, 
      rgba(0,0,0, 1) 0,   rgba(0,0,0, 1) 40%, 
      rgba(0,0,0, 0) 95%, rgba(0,0,0, 0) 0
  ) 100% 50% / 100% 100% repeat-x;
  
  border: 1px #d8d8d8 dashed;
  font: 2em/1.6em Arial;
  -webkit-mask: var(--mask); 
  mask: var(--mask);
  
}
<ol>
  <li>item1</li>
  <li>item2</li>
  <li>item3</li>
  <li>item4</li>
  <li>item5</li>
</ol>

Leave a Comment