Infinite animation of contents on top of each other using CSS keyframes - html

I have the following HTML and CSS code:
.parent{
height: 10%;
width: 100%;
float: left;
position: relative;
}
.content1{
height: 100%;
width: 20%;
background-color: blue;
float: left;
position: absolute;
}
.content2{
height: 100%;
width: 20%;
background-color: red;
float: left;
animation-delay: 1s;
position: absolute;
}
.content3{
height: 100%;
width: 20%;
background-color:yellow;
float: left;
animation-delay: 2s;
position: absolute;
}
.content4{
height: 100%;
width: 20%;
background-color: green;
float: left;
animation-delay: 3s;
position: absolute;
}
.content5{
height: 100%;
width: 20%;
background-color: orange;
float: left;
animation-delay: 4s;
}
.parent div {
animation-name: animation_01;
animation-duration:2s;
animation-iteration-count:infinite;
animation-fill-mode: forwards;
opacity:0;
}
#keyframes animation_01 {
0% {
opacity: 0
}
50% {
opacity: 1
}
100% {
opacity: 0
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
As you can see in the code I display 5 contents on top of each other by using keyframes animation. I want to run this animation infinite therefore I put animation-iteration-count:infinite;.
However, once the animation reaches content5 it does not go back to content1 and starts all over again. Instead, it only goes back to content4 and then shows/hides content4 and content5 in an infinite loop.
What do I have to change in my code so the animation goes back to content1 and starts the animation all over again?

Define a longer animation.
The animation duration in this example is 5 seconds and the visible time frame is 2 seconds. Each div has a different delay, so when one is being fade out the other starts to fade in.
.parent {
height: 10%;
width: 100%;
float: left;
position: relative;
}
.parent div {
animation-name: animation_01;
animation-duration: 5s;
animation-iteration-count: infinite;
opacity: 0;
}
.content1 {
height: 100%;
width: 20%;
background-color: blue;
position: absolute;
opacity: 1;
}
.content2 {
height: 100%;
width: 20%;
background-color: red;
animation-delay: 1s;
position: absolute;
}
.content3 {
height: 100%;
width: 20%;
background-color: yellow;
animation-delay: 2s;
position: absolute;
}
.content4 {
height: 100%;
width: 20%;
background-color: green;
animation-delay: 3s;
position: absolute;
}
.content5 {
height: 100%;
width: 20%;
background-color: orange;
animation-delay: 4s;
}
.parent {}
#keyframes animation_01 {
20% {
opacity: 1
}
0%, 40% , 100% {
opacity: 0
}
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>

Related

CSS animation stlye is not working on div element inside flexbox

Can someone please review the following block of code and tell me why the animation on the image div and info div are not working.
this is the html file for the about page. (i had enclosed the block content in a div and given it position relative)
{%extends 'layout.html' %}
{%block content %}
.about {
align-items: center;
top: 5em;
display: flex;
flex-direction: row;
justify-content: center;
box-sizing: border-box;
width: 100%;
max-width: 100%;
}
#image {
animation-name: image2;
animation-duration: 3s;
animation-delay: 3s;
animation-fill-mode: forwards;
}
.image-file {
border: none;
border-radius: 100px;
width: 200px;
height: 200px;
margin-right: 100px;
}
#info {
text-align: center;
max-width: 100%;
-webkit-animation-name: info2;
-webkit-animation-duration: 3s;
-webkit-animation-fill-mode: forwards;
animation-name: info2;
animation-duration: 3s;
animation-fill-mode: forwards;
}
#keyframes image2 {
0% {
left: -100px;
}
100% {
left: 0;
}
}
#-webkit-keyframes info2 {
0% {
right: -100px;
}
100% {
right: 0;
}
}
#keyframes info2 {
0% {
right: -100px;
}
100% {
right: 0;
}
}
<div class='head1'>
<h class='head'>Welcome to the Home page</h>
</div>
<div class='about'>
<div id='image'>
<image class='image-file' src='https://via.placeholder.com/150' alt='nelson'>
</div>
<div id='info'>
<h class='head'>About me</h>
<p class='text'>I am a web developer specialized in vanilla css,<br> html and flask. <br>But i am developing further.</p>
</div>
</div>
Try adding:
#image { position:absolute; }
If you are using Firefox, open the dev tools, locate the div image, and see the styles info.
You will see something like this
Notice that left is grayed. Now, hover on the "i" circle. You will see a message stating "left has no effect because the element is not positioned".
So, this is your problem
.about {
align-items: center;
top: 5em;
display: flex;
flex-direction: row;
justify-content: center;
box-sizing: border-box;
width: 100%;
max-width: 100%;
}
#image {
animation-name: image2;
animation-duration: 3s;
animation-delay: 3s;
animation-fill-mode: forwards;
position: relative; /* added */
}
.image-file {
border: none;
border-radius: 100px;
width: 200px;
height: 200px;
margin-right: 100px;
}
#info {
text-align: center;
max-width: 100%;
-webkit-animation-name: info2;
-webkit-animation-duration: 3s;
-webkit-animation-fill-mode: forwards;
animation-name: info2;
animation-duration: 3s;
animation-fill-mode: forwards;
position: relative; /* added */
}
#keyframes image2 {
0% {
left: -100px;
}
100% {
left: 0;
}
}
#-webkit-keyframes info2 {
0% {
right: -100px;
}
100% {
right: 0;
}
}
#keyframes info2 {
0% {
right: -100px;
}
100% {
right: 0;
}
}
<div class='head1'>
<h class='head'>Welcome to the Home page</h>
</div>
<div class='about'>
<div id='image'>
<image class='image-file' src='https://via.placeholder.com/150' alt='nelson'>
</div>
<div id='info'>
<h class='head'>About me</h>
<p class='text'>I am a web developer specialized in vanilla css,<br> html and flask. <br>But i am developing further.</p>
</div>
</div>

i want to make the image center

I am creating a loading logo for splash screen.animation looks exactly like this.so when I tried this code but my logo isn't positioned center as I wanted and also the logo is going down as the device length is increasingit is looking like this.supposed to be like this.i am using ionic4/angular8.
ion-content{
--background:yellow;
}
.tree {
left: 0;
right: 10%;
margin:0 auto;
top:50%;
transform:translateY(-50%);
position: relative;
}
.tree > div {
position: absolute;
height: 100%;
width: 100%;
background: yellow;
top: 0;
left: 0;
-webkit-animation-name: hello;
-webkit-animation-duration: 5s;
-webkit-animation-fill-mode: forwards;
animation-name: hello;
animation-duration: 5s;
animation-fill-mode: forwards;
}
.tree img {
max-width: 100%;
}
#keyframes hello {
0% {
height: 100%;
}
100% {
height: 0%;
}
}
#-webkit-keyframes hello {
0% {
height: 100%;
}
100% {
height: 0%;
}
}
<ion-content>
<div class="tree">
<div></div>
<img src="logo.svg" />
</div>
<ion-content>
You can use margin: 0 auto
.tree {
margin: 0 auto;
}
You also can use flexbox to solve this (https://css-tricks.com/snippets/css/a-guide-to-flexbox/)
Playing around with the height which is currently using 80vh (view height) you could determine the position vertically. which is different when using another type of device (screen height)
.tree {
display: flex;
justify-content: center;
align-items: center;
height: 80vh;
}
.tree > div {
position: absolute;
height: 100%;
width: 100%;
background: #fff;
top: 0;
left: 0;
-webkit-animation-name: hello;
-webkit-animation-duration: 5s;
-webkit-animation-fill-mode: forwards;
animation-name: hello;
animation-duration: 5s;
animation-fill-mode: forwards;
}
.tree img {
max-width: 300px;
}
#keyframes hello {
0% {
height: 100%;
}
100% {
height: 0%;
}
}
#-webkit-keyframes hello {
0% {
height: 100%;
}
100% {
height: 0%;
}
}
<div class="tree">
<div></div>
<img src="http://www.colourbox.com/preview/4221089-363386-green-vector-tree-nature-symbol.jpg" alt="" />
</div>
Simple center the div
<ion-content>
<center>
<div class="tree">
<div></div>
<img src="logo.svg" />
</div>
</center>
<ion-content>
ion-content{
--background:yellow;
}
.tree {
position: absolute;
left:50%;
top:50%;
margin-top:-50px;
margin-left:-50px;
}
.tree img {
max-width: 100px;
}
#keyframes hello {
0% {
height: 100%;
}
100% {
height: 0%;
}
}
#-webkit-keyframes hello {
0% {
height: 100%;
}
100% {
height: 0%;
}
}
<ion-content>
<div class="tree">
<div class="loader-img">
<img src="http://www.colourbox.com/preview/4221089-363386-green-vector-tree-nature-symbol.jpg" />
</div>
</div>
<ion-content>

Display one content after another using CSS keyframes

I have the following HMTL and CSS:
.parent{
height: 10%;
width: 100%;
float: left;
}
.content1{
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2{
height: 100%;
width: 20%;
background-color: red;
float: left;
}
.content3{
height: 100%;
width: 20%;
background-color:yellow;
float: left;
}
.content4{
height: 100%;
width: 20%;
background-color: green;
float: left;
}
.content5{
height: 100%;
width: 20%;
background-color: orange;
float: left;
}
.parent {
animation-name: animation_01;
animation-duration:5s;
animation-iteration-count:1;
animation-fill-mode: forwards;
}
#keyframes animation_01 {
0% {
opacity: 0
}
20% {
opacity: 1
}
40% {
opacity: 0
}
60% {
opacity: 1
}
80% {
opacity: 0
}
100% {
opacity: 1
}
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
This code itself works fine.
You can also find it in the JSfiddle here.
My target now is to display content1 to content5 one after another. Therefore, I wanted to use the opacity function within the animation of the CSS. However, I could not yet figure out how I can use opacity for an individual content because right now the animation only runs for all five contents combined.
Do you know if its is possible to get this animation working within #keyframes or do I need javascript/jQuery like in the example here?
You need to animate each div. You can use the same animation for all. For each div set animation-delay. Here is an example with the animation you defined in the question:
.parent {
height: 10%;
width: 100%;
float: left;
}
.parent div {
animation-name: animation_01;
animation-duration: 5s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
opacity: 0;
}
.content1 {
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2 {
height: 100%;
width: 20%;
background-color: red;
float: left;
animation-delay: 1s;
}
.content3 {
height: 100%;
width: 20%;
background-color: yellow;
float: left;
animation-delay: 2s;
}
.content4 {
height: 100%;
width: 20%;
background-color: green;
float: left;
animation-delay: 3s;
}
.content5 {
height: 100%;
width: 20%;
background-color: orange;
float: left;
animation-delay: 4s;
}
.parent {}
#keyframes animation_01 {
0% {
opacity: 0
}
20% {
opacity: 1
}
40% {
opacity: 0
}
60% {
opacity: 1
}
80% {
opacity: 0
}
100% {
opacity: 1
}
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
or if you want each item to be shown for only 1 second for example:
.parent {
height: 10%;
width: 100%;
float: left;
}
.parent div {
animation-name: animation_01;
animation-duration: 2s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
opacity: 0;
}
.content1 {
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2 {
height: 100%;
width: 20%;
background-color: red;
float: left;
animation-delay: 1s;
}
.content3 {
height: 100%;
width: 20%;
background-color: yellow;
float: left;
animation-delay: 2s;
}
.content4 {
height: 100%;
width: 20%;
background-color: green;
float: left;
animation-delay: 3s;
}
.content5 {
height: 100%;
width: 20%;
background-color: orange;
float: left;
animation-delay: 4s;
}
.parent {}
#keyframes animation_01 {
0% {
opacity: 0
}
50% {
opacity: 1
}
100% {
opacity: 0
}
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
UPDATE
To show them one on top of another set the parent position:relative and the children with position: absolute
.parent {
height: 10%;
width: 100%;
float: left;
position: relative;
}
.parent div {
animation-name: animation_01;
animation-duration: 2s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
opacity: 0;
}
.content1 {
height: 100%;
width: 20%;
background-color: blue;
position: absolute;
}
.content2 {
height: 100%;
width: 20%;
background-color: red;
animation-delay: 1s;
position: absolute;
}
.content3 {
height: 100%;
width: 20%;
background-color: yellow;
animation-delay: 2s;
position: absolute;
}
.content4 {
height: 100%;
width: 20%;
background-color: green;
animation-delay: 3s;
position: absolute;
}
.content5 {
height: 100%;
width: 20%;
background-color: orange;
animation-delay: 4s;
}
.parent {}
#keyframes animation_01 {
0% {
opacity: 0
}
50% {
opacity: 1
}
100% {
opacity: 0
}
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
The idea is to add the animation onto the child element rather than the parent and add a animation-delay to delay the start time of each animation ( this is considerably easier when using sass )
.parent{
height: 10%;
width: 100%;
float: left;
}
.content1{
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2{
height: 100%;
width: 20%;
background-color: red;
float: left;
animation-delay:.4s;
}
.content3{
height: 100%;
width: 20%;
background-color:yellow;
float: left;
animation-delay:.8s;
}
.content4{
height: 100%;
width: 20%;
background-color: green;
float: left;
animation-delay:1.2s;
}
.content5{
height: 100%;
width: 20%;
background-color: orange;
float: left;
animation-delay:1.6s;
}
.parent div{
animation-name: animation_01;
animation-duration:5s;
animation-iteration-count:1;
animation-fill-mode: forwards;
opacity:0;
}
#keyframes animation_01 {
0% {
opacity: 0;
}
100%{
opacity:1;
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>

HTML & CSS progress bar does not stop at the end

I have created this progress bar and I just can't make it stop at the end. Currently its stopping at 70% and gets cleared. Any ideas? Is there any kind of animation setting to stop it at 100%?
.wrap {
background-color: #f2f2f2;
height: 10px;
width: 400px;
margin: 0 auto;
position: relative;
top: 20px;
margin-bottom: 20px;
}
.bar {
background: #ffcc00;
height: 10px;
width: 0%;
}
.animating {
-webkit-animation: progress 3s ;
}
#-webkit-keyframes progress {
0% {
width: 0%;
}
100% {
width: 70%;
}
}
<div class="wrap">
<div class="bar animating"></div>
</div>
animation-fill-mode: forwards; or -webkit-animation: progress 3s forwards;
Try to use 100% in:
#-webkit-keyframes progress {
0% {
width: 0%;
}
100% {
width: 70%; /* edit to 100% */
}
}
A Fancy version
.wrap {
background-color: #f2f2f2;
height: 10px;
width: 400px;
margin: 0 auto;
position: relative;
top: 20px;
margin-bottom: 20px;
}
.wrap div {
background-color: #ffcc00;
height: 10px;
width: 0%;
border-radius: 5px;
animation: loadbar 3s;
-webkit-animation: loadbar 3s;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
#-webkit-keyframes loadbar {
0% {
width: 0%;
}
100% {
width: 100%;
}
#keyframes loadbar {
0% {
width: 0%;
}
100% {
width: 100%;
}
}
<div class="wrap">
<div class="bar animating"></div>
</div>

how do I animate div's resizing when page is first open [duplicate]

This question already has answers here:
css3 transition animation on load?
(13 answers)
Closed 5 years ago.
so when the page first opens it is fully occupied by the first div, i then want it to shrink into four equal divs of different colours automatically.
i have been trying to crack this for a while, here's my code so far:
div {
float: left;
}
#div1 {
width: 100%;
height: 100%;
background: #f24c43;
animation-name: shrink;
animation-duration: 4s;
}
#div2 {
width: 0%;
height: 0%;
background: #ffe605;
animation-name: grow;
animation-duration: 4s;
}
#div3 {
width: 0%;
height: 0%;
background: #e89b30;
animation-name: grow;
animation-duration: 4s;
}
#div4 {
width: 0%;
height: 0%;
background: #870e40;
animation-name: grow;
animation-duration: 4s;
}
<div id="div1">
</div>
<div id="div2">
</div>
<div id="div3">
</div>
<div id="div4">
</div>
You can add a container around your div elements and use "display: flex;" on it in order to have the items align properly and shrink/grow to your "%" based width/heights.
html,
body {
height: 100%;
padding: 0;
margin: 0;
}
/*animations*/
#keyframes shrink {
from {
width: 100%;
height: 100%;
}
to {
width: 50%;
height: 50%;
}
}
#keyframes grow {
from {
width: 0%;
height: 0%;
}
to {
width: 50%;
height: 50%;
}
}
/*css*/
#flex {
display: flex;
height: 100%;
width: 100%;
}
#div1 {
width: 100%;
height: 100%;
background: #f24c43;
animation-name: shrink;
animation-duration: 4s;
}
#div2 {
width: 0%;
height: 0%;
background: #ffe605;
animation-name: grow;
animation-duration: 4s;
}
#div3 {
width: 0%;
height: 0%;
background: #e89b30;
animation-name: grow;
animation-duration: 4s;
}
#div4 {
width: 0%;
height: 0%;
background: #870e40;
animation-name: grow;
animation-duration: 4s;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/home.css">
<title>GymBro2</title>
</head>
<body>
<div id="flex">
<div id="div1">
</div>
<div id="div2">
</div>
<div id="div3">
</div>
<div id="div4">
</div>
</div>
</body>
</html>