Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

enter image description here

Is it possible to achieve this with just one div (no background images/foreground images/layers)?

like image 726
antonpug Avatar asked Mar 26 '14 15:03

antonpug


People also ask

How to fade the Edge of a div with just CSS?

I do this by defining a new tag . This makes it really easy to add the desired fade out effect to any element you want using <fade/> at the end. Giving the fade element an absolute position with a gradient background works just as expected. As long as you remember to set the parent's position to relative .

How do you fade edges in CSS?

So, you could leave your current design, and add a box-shadow like: box-shadow: 0px -2px 2px rgba(34,34,34,0.6); This should give you a 'blurry' top-edge.

How do I fade text in CSS?

To apply a fade-out effect on an element, you need to use either the animation or transition property in CSS. Using the transition property, CSS lets you specify the initial and final state, for instance, you can let the contents of a div transition from black to white.


2 Answers

Example on codepen: http://codepen.io/anon/pen/sbHAc/

Relevant CSS

ol {   border   : 1px #d8d8d8 dashed;   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; } 

Resulting effect

enter image description here

if the browser supports the pointer-events property (all major browsers except IE<=10) then the text under the gradient will be also selectable/clickable.

like image 198
Fabrizio Calderan Avatar answered Sep 23 '22 18:09

Fabrizio Calderan


I (personally) find that using a secondary element as an "overlap" works pretty well. I do this by defining a new tag. This makes it really easy to add the desired fade out effect to any element you want using <fade/> at the end.

div {      position: relative;  }    fade {      position: absolute;      bottom: 0px;        display: block;          width: 100%;      height: 50px;          background-image: linear-gradient(to bottom,           rgba(255, 255, 255, 0),           rgba(255, 255, 255, 0.9)      100%);  }
<div>      text      <br>      text      <br>      text      <fade/>  </div>

Giving the fade element an absolute position with a gradient background works just as expected. As long as you remember to set the parent's position to relative.

like image 34
Aleksander Azizi Avatar answered Sep 24 '22 18:09

Aleksander Azizi