Instead of using margin, you can use position ...
For animation, use animation.
.container {
position: relative;
width: 100%;
height: 170px;
overflow: hidden;
}
.box {
position: absolute;
width: 150px;
height: 150px;
top: 0px;
margin: 10px;
left: 100%;
border-radius: 5px;
background-color: whitesmoke;
box-shadow: 0px 0px 5px black;
}
.boxA {
animation: boxA 1s linear 0s forwards;
-webkit-animation: boxA 1s linear 0s forwards;
}
.boxB {
animation: boxB 1s linear 1s forwards;
-webkit-animation: boxB 1s linear 1s forwards;
}
.boxC {
animation: boxC 1s linear 2s forwards;
-webkit-animation: boxC 1s linear 2s forwards;
}
@keyframes boxA {
0% {
left: 100%;
background-color: teal;
}
100% {
left: 0px;
background-color: crimson;
}
}
@keyframes boxB {
0% {
left: 100%;
background-color: steelblue;
}
100% {
left: 170px;
background-color: teal;
}
}
@keyframes boxC {
0% {
left: 100%;
background-color: crimson;
}
100% {
left: 340px;
background-color: steelblue;
}
}
@-webkit-keyframes boxA {
0% {
left: 100%;
background-color: white;
}
100% {
left: 0px;
background-color: crimson;
}
}
@-webkit-keyframes boxB {
0% {
left: 100%;
background-color: white;
}
100% {
left: 170px;
background-color: teal;
}
}
@-webkit-keyframes boxC {
0% {
left: 100%;
background-color: white;
}
100% {
left: 340px;
background-color: steelblue;
}
}
<div class="container">
<div class="box boxA">A</div>
<div class="box boxB">B</div>
<div class="box boxC">C</div>
</div>
The animation
property is just a short form for properties animation-name
, animation-duration
, animation-timing-function
, animation-delay
, animation-iteration-count
, animation-direction
, animation-fill-mode
and animation-play-state
. .. then when doing:
animation: boxC 1s linear forwards;
is the same as doing the following:
animation-name: boxC;
animation-duration: 1s;
animation-timing-function: linear;
animation-fill-mode: forwards;
that is, execute the animation boxC
, it must have a duration of 1s
, where each fraction of the animation must occupy the same time slice ( linear
), and after the animation ends, the animation element DOM should not be reset in style ( forwards
).
@keyframes
defines the rules for the animation, for example:
.box {
width: 300px;
height: 300px;
animation: background 4s linear forwards;
-webkit-animation: background 4s linear forwards;
}
@-webkit-keyframes background {
0% { background-color: red; }
75% { background-color: green; }
100% { background-color: blue; }
}
@keyframes background {
0% { background-color: red; }
75% { background-color: green; }
100% { background-color: blue; }
}
<div class="box">
</div>
background
is the name of the animation, 0%
, 75%
and 100%
inform what css is expected at that time.
In the example above,% w / w will begin with div.box
, for 3s (75% of 4s) it will gradually change until it has background-color: red;
, it will again gradually change by 1s (25% of 4s) until you have background-color: green;