Use css @keyframes
.elementToFadeInAndOut {
opacity: 1;
animation: fade 2s linear;
}
@keyframes fade {
0%,100% { opacity: 0 }
50% { opacity: 1 }
}
here is a DEMO
.elementToFadeInAndOut {_x000D_
width:200px;_x000D_
height: 200px;_x000D_
background: red;_x000D_
-webkit-animation: fadeinout 4s linear forwards;_x000D_
animation: fadeinout 4s linear forwards;_x000D_
}_x000D_
_x000D_
@-webkit-keyframes fadeinout {_x000D_
0%,100% { opacity: 0; }_x000D_
50% { opacity: 1; }_x000D_
}_x000D_
_x000D_
@keyframes fadeinout {_x000D_
0%,100% { opacity: 0; }_x000D_
50% { opacity: 1; }_x000D_
}
_x000D_
<div class=elementToFadeInAndOut></div>
_x000D_
Reading: Using CSS animations
You can clean the code by doing this:
.elementToFadeInAndOut {_x000D_
width:200px;_x000D_
height: 200px;_x000D_
background: red;_x000D_
-webkit-animation: fadeinout 4s linear forwards;_x000D_
animation: fadeinout 4s linear forwards;_x000D_
opacity: 0;_x000D_
}_x000D_
_x000D_
@-webkit-keyframes fadeinout {_x000D_
50% { opacity: 1; }_x000D_
}_x000D_
_x000D_
@keyframes fadeinout {_x000D_
50% { opacity: 1; }_x000D_
}
_x000D_
<div class=elementToFadeInAndOut></div>
_x000D_