Animation How to - Do CSS3 Transitions on CSS generated content








Question

We would like to know how to do CSS3 Transitions on CSS generated content.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
div {<!--   w  ww.  j  a  va  2s  . c  o m-->
    height: 40px;
    width: 40px;
    background-color: #ececec;
    position: relative;
}
div:after {
    content: "";
    display: block;
    position: absolute;
    height: 100%;
    width: 100%; 
    background-color: #000;
    opacity: 0;
    -moz-transition: opacity .3s;
    -o-transition: opacity .3s;
    -webkit-transition: opacity .3s;
    transition: opacity .3s;
}
div:hover:after {
    opacity: 1;
}
  </style>
</head>
<body>
  <div>
</div>
</body>
</html>

The code above is rendered as follows: