CSS animation not being applied to div - css

Looking at Animista.net I would like to write some custom CSS animations. So I thought I would try by using one of their examples, and then tweaking it for my personal use.
.box {
width: 100px;
height: 100px;
background-color: red;
-webkit-animation: fade-in 1.2s steps(80, end) both;
-moz-animation: fade-in 1.2s steps(80, end) both;
animation: fade-in 1.2s steps(80, end) both;
}
<div class="box"></div>
But I cannot get the animations to work at all.
What am I doing wrong?

Here you are. You must add animation.
#keyframes example {
0% {background-color:red; left:0px; top:0px;}
25% {background-color:yellow; left:200px; top:0px;}
50% {background-color:blue; left:200px; top:200px;}
75% {background-color:green; left:0px; top:200px;}
100% {background-color:red; left:0px; top:0px;}
}
Codepen

You also need to define the #keyframes to make the animation work. See the attached snippet.
.box {
width: 100px;
height: 100px;
background-color: red;
-webkit-animation: fade-in 1.2s steps(80, end) both;
-moz-animation: fade-in 1.2s steps(80, end) both;
animation: fade-in 1.2s steps(80, end) both;
}
#-webkit-keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1.0;
}
}
#-moz-keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1.0;
}
}
#-o-keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1.0;
}
}
#keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1.0;
}
}
<div class="box"></div>

Related

CSS Animation, hide overflow for initial part of animation

I am trying to create a CSS animation where I have a frame with a background image, then I have a crane that needs to slide into the frame from the bottom, so for that I would need overflow:hidden; so that you can't see the crane sliding into the frame. But then after it slides up into the frame, I need the arm of the crane to rotate down and extend out of the frame. However, since I have overflow:hidden; for the first part of the animation, I'm not sure how to make the second part work. Here's what I have so far:
.frame {
width:600px;
height:300px;
background:url('http://placehold.it/600x300');
overflow:hidden;
}
.crane-container {
position:relative;
}
.crane {
position:absolute;
bottom:-500px;
right:100px;
height:200px;
width:50px;
animation:slideUp 3s ease-in-out;
animation-fill-mode: forwards;
}
.arm {
height:200px;
width:50px;
background:#000;
animation:rotateArm 4s ease-in-out;
animation-fill-mode: forwards;
animation-delay: 3s;
transform-origin:bottom left;
}
#keyframes slideUp {
0% {
bottom: -500px;
}
100% {
bottom: -300px;
}
}
#keyframes rotateArm {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(-120deg);
}
}
<div class="frame">
<div class="crane-container">
<div class="crane">
<div class="arm"></div>
</div>
</div>
</div>
Think differently and instead of animating position you can animate the height and you don't need the overflow.
Have a look:
.frame {
width: 600px;
height: 300px;
background: url('http://placehold.it/600x300');
overflow: visible;
}
.crane-container {
position: relative;
height:100%;
}
.crane {
position: absolute;
bottom: 0;
right: 100px;
height: 0;
width: 50px;
animation: slideUp 3s ease-in-out;
animation-fill-mode: forwards;
}
.arm {
height: 100%;
width: 100%;
background: #000;
animation: rotateArm 4s ease-in-out;
animation-fill-mode: forwards;
animation-delay: 3s;
transform-origin: bottom left;
}
#keyframes slideUp {
0% {
height: 0;
}
100% {
height: 200px;
}
}
#keyframes rotateArm {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(-120deg);
}
}
#keyframes over {
0%,100% {
overflow:visible;
}
}
<div class="frame">
<div class="crane-container">
<div class="crane">
<div class="arm"></div>
</div>
</div>
</div>

Animation doesnot work on Display: block to none

I am trying to show or hide the title of sidepanel while collapsing it to width:50%
I am using display: block to display:none;
Somehow I am not able to use the animation on it
Here is the code I am using
.fade-in {
animation: fade-in 0.5s ease-in-out both;
}
.fade-out {
animation: fade-out 0.6s ease-in-out both;
}
#keyframes fade-in {
0% {
opacity: 1;
display: block;
}
100% {
opacity: 0;
display: none;
}
}
#keyframes fade-out {
0% {
opacity: 0;
display: none;
}
100% {
opacity: 1;
display: block;
}
}
Any idea why display is not working
.fade-in {
animation: fade-in 0.5s ease-in-out both;
background:black;
height:200px;
width:200px;
margin-bottom:10px;
}
.fade-out {
animation: fade-out 0.6s ease-in-out both;
background:red;
height:200px;
width:200px;
}
#keyframes fade-in {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
#keyframes fade-out {
0% {
opacity: 1;
display: none;
}
100% {
opacity: 0;
}
}
<div class="fade-in"></div>
<div class="fade-out"></div>

Fade In Fade Out with Delay using CSS

let me start by saying that I am a novice developer (maybe even below that), so, I apologize if I do not explain myself well.
I am trying to get several customer reviews about our product to splash onto the screen (FadeIn1, delay, FadeOut1), (FadeIn2, delay, FadeOut2) etc. I can get the fade-in and fade-out to work individually but I can't seem to get them to work together. The below code only fades it out. Can some one please let me know what I am doing wrong? Thanks in advance...
/* keyframes that tell the start state and the end state of our object */
#-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; }}
#-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; }}
#keyframes fadeIn { from { opacity:0; } to { opacity:1; }}
#-webkit-keyframes fadeOut { from { opacity:1; } to { opacity:0; }}
#-moz-keyframes fadeOut { from { opacity:1; } to { opacity:0; }}
#keyframes fadeOut { from { opacity:1; } to { opacity:0; }}
.fade-in {
opacity:0; /* make things invisible upon start */
-webkit-animation:fadeIn ease-in 1; /* call keyframe named fadeIn, use animattion ease-in and repeat it only 1 time */
-moz-animation:fadeIn ease-in 1;
animation:fadeIn ease-in 1;
-webkit-animation-fill-mode:forwards; /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 1)*/
-moz-animation-fill-mode:forwards;
animation-fill-mode:forwards;
-webkit-animation-duration:1s;
-moz-animation-duration:1s;
animation-duration:1s;
}
.fade-out {
opacity:1; /* make things visible upon start */
-webkit-animation:fadeOut ease-out 1; /* call keyframe named fadeOut, use animattion ease-out and repeat it only 1 time */
-moz-animation:fadeOut ease-out 1;
animation:fadeOut ease-out 1;
-webkit-animation-fill-mode:forwards; /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 0)*/
-moz-animation-fill-mode:forwards;
animation-fill-mode:forwards;
-webkit-animation-duration:1s;
-moz-animation-duration:1s;
animation-duration:1s;
-webkit-animation-delay: 3s;
-moz-animation-delay: 3s;
animation-delay: 3s;
}
.fade-in.fade-out.one {
-webkit-animation-delay: 0.7s;
-moz-animation-delay: 0.7s;
animation-delay: 0.7s;
}
.fade-in.fade-out.two {
-webkit-animation-delay: 1.2s;
-moz-animation-delay:1.2s;
animation-delay: 1.2s;
}
.fade-in.fade-out.three {
-webkit-animation-delay: 1.6s;
-moz-animation-delay: 1.6s;
animation-delay: 1.6s;
}
/*---basic box ---*/
.box{
width: 200px;
height: 200px;
position: relative;
margin: 10px;
float: left;
border: 1px solid #333;
background: #999;
}
<body>
<div class="box fade-in fade-out one">
look at me fade in and out
</div>
<div class="box fade-in fade-out two">
i can fade too!
</div>
<div class="box fade-in fade-out three">
i can fade three!
</div>
</body>
Your original code is very close. Building upon the #ILoveCSS answer and your use of animation delay, I think this is the effect you are looking for:
#keyframes fade {
0% { opacity: 0 }
20% { opacity: 1 } /* 20% of 5 seconds = 1 second */
80% { opacity: 1 }
100% { opacity: 0 }
}
.fade {
opacity:0;
animation: fade ease-in-out 5s;
animation-fill-mode: forwards;
}
.fade:nth-child(2) { animation-delay: 0.25s; }
.fade:nth-child(3) { animation-delay: 0.5s; }
.box{
width: 100px;
height: 100px;
position: relative;
margin: 10px;
float: left;
border: 1px solid #333;
background: #999;
}
<div class="box fade">Box 1</div>
<div class="box fade">Box 2</div>
<div class="box fade">Box 3</div>
You can simplify the animation by using % frames instead.
You can control the speed of the animation under the .box animation selector.
#-webkit-keyframes myfade {
0% {
opacity: 0
}
20% {
opacity: 0
}
30% {
opacity: 1
}
40% {
opacity: 1
}
50% {
opacity: 1
}
60% {
opacity: 1
}
70% {
opacity: 0
}
80% {
opacity: 0
}
100% {
opacity: 0
}
}
#-moz-keyframes myfade {
0% {
opacity: 0
}
20% {
opacity: 0
}
30% {
opacity: 1
}
40% {
opacity: 1
}
50% {
opacity: 1
}
60% {
opacity: 1
}
70% {
opacity: 0
}
80% {
opacity: 0
}
100% {
opacity: 0
}
}
#keyframes myfade {
0% {
opacity: 0
}
20% {
opacity: 0
}
30% {
opacity: 1
}
40% {
opacity: 1
}
50% {
opacity: 1
}
60% {
opacity: 1
}
70% {
opacity: 0
}
80% {
opacity: 0
}
100% {
opacity: 0
}
}
.box {
width: 200px;
height: 200px;
position: relative;
margin: 10px;
float: left;
border: 1px solid #333;
background: #999;
animation: myfade 8s;
opacity: 0
}
<body>
<div class="box fade-in fade-out one">
look at me fade in and out
</div>
<div class="box fade-in fade-out two">
i can fade too!
</div>
<div class="box fade-in fade-out three">
i can fade three!
</div>
</body>

CSS how to make an element fade in and then fade out?

I can make an element with an opacity of zero fade in by changing its class to .elementToFadeInAndOut with the following css:
.elementToFadeInAndOut {
opacity: 1;
transition: opacity 2s linear;
}
Is there a way I can make the element fade out after it fades in by editing css for this same class?
Use css #keyframes
.elementToFadeInAndOut {
opacity: 1;
animation: fade 2s linear;
}
#keyframes fade {
0%,100% { opacity: 0 }
50% { opacity: 1 }
}
here is a DEMO
.elementToFadeInAndOut {
width:200px;
height: 200px;
background: red;
-webkit-animation: fadeinout 4s linear forwards;
animation: fadeinout 4s linear forwards;
}
#-webkit-keyframes fadeinout {
0%,100% { opacity: 0; }
50% { opacity: 1; }
}
#keyframes fadeinout {
0%,100% { opacity: 0; }
50% { opacity: 1; }
}
<div class=elementToFadeInAndOut></div>
Reading: Using CSS animations
You can clean the code by doing this:
.elementToFadeInAndOut {
width:200px;
height: 200px;
background: red;
-webkit-animation: fadeinout 4s linear forwards;
animation: fadeinout 4s linear forwards;
opacity: 0;
}
#-webkit-keyframes fadeinout {
50% { opacity: 1; }
}
#keyframes fadeinout {
50% { opacity: 1; }
}
<div class=elementToFadeInAndOut></div>
If you need a single fadeIn/Out without an explicit user action (like a mouseover/mouseout) you may use a CSS3 animation: http://codepen.io/anon/pen/bdEpwW
.elementToFadeInAndOut {
animation: fadeInOut 4s linear 1 forwards;
}
#keyframes fadeInOut {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
By setting animation-fill-mode: forwards the animation will retain its last keyframe
By setting animation-iteration-count: 1 the animation will run just once (change this value if you need to repeat the effect more than once)
I found this link to be useful: css-tricks fade-in fade-out css.
Here's a summary of the csstricks post:
CSS classes:
.m-fadeOut {
visibility: hidden;
opacity: 0;
transition: visibility 0s linear 300ms, opacity 300ms;
}
.m-fadeIn {
visibility: visible;
opacity: 1;
transition: visibility 0s linear 0s, opacity 300ms;
}
In React:
toggle(){
if(true condition){
this.setState({toggleClass: "m-fadeIn"});
}else{
this.setState({toggleClass: "m-fadeOut"});
}
}
render(){
return (<div className={this.state.toggleClass}>Element to be toggled</div>)
}
Try creating a keyframes animation for the opacity attribute of your element:
<style>
p {
animation-name: example;
animation-duration: 2s;
}
#keyframes example {
from {opacity: 2;}
to {opacity: 0;}
}
</style>
<div>
<p>[Element to fade]</p>
</div>
(You can also set the exact percentages of animations to make it fade in/out. For example, set 0% to 2 opacity, 50% to 0 opacity, and 100% to 2 opacity. A good source for this method is W3Schools # https://www.w3schools.com/css/tryit.asp?filename=trycss3_animation2 .)
Try this:
#keyframes animationName {
0% { opacity:0; }
50% { opacity:1; }
100% { opacity:0; }
}
#-o-keyframes animationName{
0% { opacity:0; }
50% { opacity:1; }
100% { opacity:0; }
}
#-moz-keyframes animationName{
0% { opacity:0; }
50% { opacity:1; }
100% { opacity:0; }
}
#-webkit-keyframes animationName{
0% { opacity:0; }
50% { opacity:1; }
100% { opacity:0; }
}
.elementToFadeInAndOut {
-webkit-animation: animationName 5s infinite;
-moz-animation: animationName 5s infinite;
-o-animation: animationName 5s infinite;
animation: animationName 5s infinite;
}

CSS3 keyframes ease-in box then ease-out

I am having a look at CSS3 keyframes and want to have a box that eases in then eases out for the specified iteration-count, this is what I have so far it eases in then disappears then eases in again.
I want the box to ease in then ease out. See my fiddle. What do I need to do to achieve this?
<div id="content">
<span class="aniamte"></span>
</div>
#keyframes reset {
0% { opacity: 0; }
100% { opacity: 0; }
}
#keyframes fade-in {
0% { opacity: 0; }
60% { opacity: 0; }
100% { opacity: 1; }
}
.aniamte {
background: red;
display: inline-block;
height: 100px;
width: 100px;
animation-name: reset, fade-in;
animation-duration: 3s;
animation-timing-function: ease-in-out;
animation-iteration-count: 5;
animation-delay: 0, 1s;
}
I believe you're looking for animation-direction:alternate, but your question is not very clear. This will make your element use the keyframes from 0% to 100% for the specified duration then go from 100% to 0% after the first iteration is complete
#keyframes fade-in {
0% { opacity: 0; }
100% { opacity: 1; }
}
.animate {
background: red;
display: inline-block;
height: 100px;
width: 100px;
animation-name: fade-in;
animation-duration: 3s;
animation-timing-function: ease-in-out;
animation-direction:alternate;
animation-iteration-count: 5;
}
Demo

Resources