Animation Not Running - css

I am trying to make a loading screen using CSS Animations. The screen is 4 different bars shrinking and growing. I want to arrange them in this formation where they form a square. I use absolute positioning to position them, but I would like to know if there is a better way. I managed to do 3 bars with display and float but did not manage to do the last one.
Now, the animations are not running at all. Can somebody help me?
Code:
https://codepen.io/ngmh/pen/gxewJK
HTML:
<div id="top"></div>
<div id="right"></div>
<div id="bottom"></div>
<div id="left"></div>
CSS:
#top{
background-color: red;
width: 100px;
height: 25px;
border-radius: 12.5px;
position: absolute;
left: 37.5px;
animation-name: loading-1;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#bottom{
background-color: yellow;
width: 100px;
height: 25px;
border-radius: 12.5px;
position: absolute;
top: 112.5px;
animation-name: loading-1;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#left{
background-color: blue;
width: 25px;
height: 100px;
border-radius: 12.5px;
animation-name: loading-2;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#right{
background-color: green;
width: 25px;
height: 100px;
border-radius: 12.5px;
position: absolute;
left: 112.5px;
top: 37.5px;
animation-name: loading-2;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#keyframes loading-1{
0%:{width: 100px;}
50%:{width: 10px;}
100%:{width: 100px;}
}
#keyframes loading-2{
0%:{height: 100px;}
50%:{height: 10px;}
100%:{height: 100px;}
}

There shouldn't be any colon : after the percent sign % in your #keyframes rules
#top{
background-color: red;
width: 100px;
height: 25px;
border-radius: 12.5px;
position: absolute;
left: 37.5px;
animation-name: loading-1;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#bottom{
background-color: yellow;
width: 100px;
height: 25px;
border-radius: 12.5px;
position: absolute;
top: 112.5px;
animation-name: loading-1;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#left{
background-color: blue;
width: 25px;
height: 100px;
border-radius: 12.5px;
animation-name: loading-2;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#right{
background-color: green;
width: 25px;
height: 100px;
border-radius: 12.5px;
position: absolute;
left: 112.5px;
top: 37.5px;
animation-name: loading-2;
animation-duration: 4s;
animation-iteration-count: infinite;
}
#keyframes loading-1{
0% {width: 100px;}
50% {width: 10px;}
100% {width: 100px;}
}
#keyframes loading-2{
0% {height: 100px;}
50% {height: 10px;}
100% {height: 100px;}
}
<div id="top"></div>
<div id="right"></div>
<div id="bottom"></div>
<div id="left"></div>
Using pseudo elements would make the markup smaller and with that maybe more maintainable.
The outer does not use absolute positioning and will flow better with the rest of the content.
.outer {
position: relative;
}
.outer div,
.outer::before,
.outer::after,
.outer div::before,
.outer div::after {
content: '';
position: absolute;
border-radius: 12.5px;
animation-duration: 4s;
animation-iteration-count: infinite;
}
.outer::before {
background-color: red;
width: 100px;
height: 25px;
left: 37.5px;
animation-name: loading-1;
}
.outer::after{
background-color: yellow;
width: 100px;
height: 25px;
top: 112.5px;
animation-name: loading-1;
}
.outer div::before{
background-color: blue;
width: 25px;
height: 100px;
animation-name: loading-2;
}
.outer div::after{
background-color: green;
width: 25px;
height: 100px;
left: 112.5px;
top: 37.5px;
animation-name: loading-2;
}
#keyframes loading-1{
0% {width: 100px;}
50% {width: 10px;}
100% {width: 100px;}
}
#keyframes loading-2{
0% {height: 100px;}
50% {height: 10px;}
100% {height: 100px;}
}
<div class="outer"><div></div></div>

Related

How to have a sliding animation from the bottom to the top with CSS?

I've spent the past hour trying to find a way to get a sliding animation from bottom to top using only CSS. It's supposed to be a test tube with fluid filling it. (This is what I want it to look like), however during the animation it overflows around the edges at the bottom and doesn't sit flush (like this).
HTML & CSS:
.testTube #border {
width: 40px;
height: 150px;
margin-top: 15px;
background-color: white;
border: 2px solid black;
border-radius: 5px 5px 25px 25px;
position: absolute;
}
.testTube #fluidLeft {
width: 20px;
height: 100px;
margin-left: 2px;
background-color: #ff007f;
border-radius: 0% 0% 0% 25px;
position: absolute;
animation-name: fluid;
animation-direction: normal;
animation-duration: 3s;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
.testTube #fluidRight {
width: 20px;
height: 0px;
margin-left: 22px;
background-color: #ff1a8c;
border-radius: 0% 0% 25px 0%;
position: absolute;
animation-name: fluid;
animation-direction: normal;
animation-duration: 3s;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
#keyframes fluid {
from {
height: 0px;
margin-top: 167px;
}
to {
height: 100px;
margin-top: 67px;
}
}
<div class="testTube">
<div id="border"></div>
<div id="fluidLeft"></div>
<div id="fluidRight"></div>
</div>
You can move the #fluidLeft and #fluidRight divs into the #border div. That way, you can add "overflow: hidden" on to the border div (now the parent) which will make it so the fluid divs (now children of the border div) don't overlap outside of the border. I also tweaked the "margin-left" on the fluids by 2px so it was centered in the tube.
.testTube #border {
width: 40px;
height: 150px;
margin-top: 15px;
background-color: white;
border: 2px solid black;
border-radius: 5px 5px 25px 25px;
position: absolute;
overflow: hidden;
}
.testTube #fluidLeft {
width: 20px;
height: 100px;
background-color: #ff007f;
border-radius: 0% 0% 0% 25px;
position: absolute;
animation-name: fluid;
animation-direction: normal;
animation-duration: 3s;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
.testTube #fluidRight {
width: 20px;
height: 0px;
margin-left: 20px;
background-color: #ff1a8c;
border-radius: 0% 0% 25px 0%;
position: absolute;
animation-name: fluid;
animation-direction: normal;
animation-duration: 3s;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
#keyframes fluid {
from {
height: 0px;
margin-top: 167px;
}
to {
height: 100px;
margin-top: 67px;
}
}
<div class="testTube">
<div id="border">
<div id="fluidLeft"></div>
<div id="fluidRight"></div>
</div>
</div>

keyframes animations not returning to first point or repeating

I'm trying to use a keyframe that will not return to first position, I mean if I have a transition from left to right, to stay right not return to the left side.
Code
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
-webkit-animation-name: example;
-webkit-animation-duration: 4s;
animation-name: example;
animation-duration: 4s;
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
100% {
background-color: yellow;
left: 200px;
top: 0px;
}
}
/* Standard syntax */
#keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
100% {
background-color: yellow;
left: 200px;
top: 0px;
}
}
<div></div>
You just need to add animation-fill-mode: forwards; to the div.
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
-webkit-animation-name: example;
-webkit-animation-duration: 4s;
-webkit-animation-fill-mode: forwards;
animation-name: example;
animation-duration: 4s;
animation-fill-mode: forwards;
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes example {
0% {background-color:red; left:0px; top:0px;}
100% {background-color:yellow; left:200px; top:0px;}
}
/* Standard syntax */
#keyframes example {
0% {background-color:red; left:0px; top:0px;}
100% {background-color:yellow; left:200px; top:0px;}
}
<div></div>
You just need to add 'animation-iteration-count: 1' it will work single time or if you want to take it in the loop then can try 'animation-iteration-count: infinite' into the div.
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
-webkit-animation-name: example;
-webkit-animation-duration: 4s;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 1; /*value can be infinite if you want to it in loop */
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
50%{
background-color: yellow;
left: 200px;
top: 0px;
}
100% {
background-color: red;
left: 0px;
top: 0px;
}
}
/* Standard syntax */
#keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
50%{
background-color: yellow;
left: 200px;
top: 0px;
}
100% {
background-color: red;
left: 0px;
top: 0px;
}
}
<div></div>

Why my animation does not work?

I Want the motion to work, but the background image position is fixed and does not move, why ?
#-webkit-keyframes anima {
from {
background-position: left;
} to {
background-position: right;
}
}
#bando {
border-radius: 4px;
background-image: url(neon.jpg);
background-size: 120%;
width: 600px;
padding-top:40px;
padding-bottom:40px;
margin-bottom: 10px;
font-size: 30px;
height:200px;
animation-name: anima;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-direction: alternate;
}
Thanks for helping
For this to work, make sure the image is bigger (or smaller) than the element, like in below sample, where the element is 600px wide and the image 800px
#-webkit-keyframes anima {
from {
background-position: left;
}
to {
background-position: right;
}
}
div {
border-radius: 4px;
background-size: 120%;
background: url(http://lorempixel.com/800/280/nature/1/);
width: 600px;
padding-top: 40px;
padding-bottom: 40px;
margin-bottom: 10px;
font-size: 30px;
height: 200px;
animation-name: anima;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-direction: alternate;
}
<div></div>
Another option is to use a pseudo element, as with that you can use transform to move the image, which I recommend and is a far more smoother and efficient way to do it
div {
position: relative;
border-radius: 4px;
width: 600px;
padding-top: 40px;
padding-bottom: 40px;
margin-bottom: 10px;
font-size: 30px;
height: 200px;
overflow: hidden;
}
div::after {
content: '';
position: absolute;
border-radius: 4px;
background-size: 120%;
background: url(http://lorempixel.com/800/280/nature/1/);
width: 800px;
height: 100%;
animation-name: anima;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-direction: alternate;
}
#-webkit-keyframes anima {
from {
transform: translateX(0);
}
to {
transform: translateX(-200px);
}
}
<div></div>

CSS animation speed control

Demo on CodePen
<pre>
.parent
border: 1px solid tomato
height: 300px
margin: 0 auto
margin-top: 30px
width: 80%
.box
width: 50px
height: 50px
position: absolute
animation-name: falling
animation-iteration-count: infinite
.box-1
background-color: lightblue
right: 60vw
animation-duration: 6s
#keyframes falling
0%
top: -10vh
100%
top: 90vh
.box-2
background-color: lightgreen
right: 70vw
animation-duration: 8s
#keyframes falling
0%
top: -10vh
100%
top: 90vh
</pre>
As you can see in the demo, the animation speed of the cube is slowing down the closer it gets to the bottom.
I'd like to make animation the same speed during the fall.
Thank you.
The default animation-timing-function in CSS is ease - accelerate in the start, slow after the middle. You need a linear timing function, that has a constant speed.
Change the box timing function to linear (pen):
.box
width: 50px
height: 50px
position: absolute
animation-name: falling
animation-iteration-count: infinite
animation-timing-function: linear
You can use animation function linear. Have a look at the snippet below:
.parent {
border: 1px solid tomato;
height: 300px;
margin: 0 auto;
margin-top: 30px;
width: 80%;
}
.box {
width: 50px;
height: 50px;
position: absolute;
animation-name: falling;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
.box-1 {
background-color: lightblue;
right: 60vw;
animation-duration: 6s;
}
#keyframes falling {
0% {
top: -10vh;
}
100% {
top: 90vh;
}
}
.box-2 {
background-color: lightgreen;
right: 70vw;
animation-duration: 8s;
}
#keyframes falling {
0% {
top: -10vh;
}
100% {
top: 90vh;
}
}
<div class="parent">
<div class="box box-1"></div>
<div class="box box-2"></div>
</div>
Hope this helps!

CSS Animation - Height anchor point

New to coding here. I'm practicing with CSS, and I'm trying to create a simple animation. The twist: I want the height of a div to change, but the animation anchors the height change at the top of the div, and I need it to anchor at the bottom. I've been looking around and saw the transform-origin element, but that doesn't seem to help, as it is not a transform, but a change in height. I also tried rotating the div 180degrees but that didn't work either.
Here's my code:
<!DOCTYPE html>
<html>
<head>
<style>
.rowleteyeleft {
height: 100px;
width: 50px;
border-radius: 50%;
background-color: hsl(46, 6%, 21%);
position: relative;
right: -70px;
top: 70px;
overflow:hidden;
animation-name: blinkleft;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-direction:alternate;}
#keyframes blinkleft {
to {height: 10px}
</style>
</head>
<body>
<div class="rowleteyeleft"></div>
</body>
</html>
What do I need to add to it to get the height to anchor at the bottom?
Use Pseudo-elements
.rowleteyeleft {
height: 100px;
width: 50px;
position: relative;
right: -70px;
top: 70px;
overflow: hidden;
}
.rowleteyeleft:before {
content:"";
position:absolute;
bottom: 0;
border-radius: 50%;
width:100%;
height:100%;
background-color: hsl(46, 6%, 21%);
animation-name: blinkleft;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-direction: alternate;
}
#keyframes blinkleft {
to {
height: 10px
}
}
<div class="rowleteyeleft"></div>
This demo shows you what is doing on behind the animation
.wrapper {
height: 100px;
width: 50px;
left: 70px;
top: 70px;
position: relative;
/*this help us animate the child from the bottom*/
overflow: hidden;
border: 1px dashed hsl(46, 6%, 21%);
}
.rowleteyeleft {
height: 10px;
width: 100%;
left: 0;
bottom: 0px;
position: absolute;
border-radius: 50%;
background-color: hsl(46, 6%, 21%);
animation-name: blinkleft;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-direction: alternate;
}
#keyframes blinkleft {
to {
height: 100px
}
}
<div class=wrapper><!--give your animated element a parent-->
<div class="rowleteyeleft"></div>
</div>

Resources