Why does adding opacity break 3D CSS animations? - html

This is my base animation. Everything here works fine unless 1 line of code is added to the div, div::before, div::after { } selector.
*, * ::before, * ::after {
transform-style: preserve-3d;
margin: 0; padding: 0;
box-sizing: border-box;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
html { overflow: hidden; background-color: #eee; }
html, body, main {
height: 100%;
color: #999;
font-family: Arial;
text-align: center;
text-transform: uppercase;
}
main, div, div::before, div::after {
display: flex;
justify-content: center;
align-items: center;
}
body {
perspective: 20rem;
}
div, div::before, div::after {
position: absolute;
transform-origin: center center -5rem;
width: 10rem; height: 10rem;
border-style: solid;
border-color: #ccc;
border-width: 1rem;
background-color: rgb( 221, 221, 221, 0.75 );
}
<style>
div::before {
transform: rotateX( 90deg ); content: 'top';
}
div::after {
transform: rotateX( -90deg ); content: 'bottom';
}
div > div {
transform: rotateY( 180deg );
transform-origin: center center center;
}
div > div::before {
transform: rotateY( 90deg ); content: 'right';
}
div > div::after {
transform: rotateY( -90deg ); content: 'left';
}
.rotate_y { animation-name: rotate_y; }
#keyframes rotate_y {
0% { transform: rotateY( 0deg ); } 100% { transform: rotateY( 360deg ); }
}
.rotate_x { animation-name: rotate_x; animation-duration: 200s; }
#keyframes rotate_x {
0% { transform: rotateX( 0deg ) scale( 1 ); }
100% { transform: rotateX( 360deg ) scale( -6 ); }
}
</style>
<main class='rotate_x'>
<div class='rotate_y'>
front <div>back</div>
</div>
</main>
When the following code is added all of the selected elements proceed to disappear except for the non-pseudo elements.
div, div::before, div::after {
opacity: 0.9;
}
Below is a demonstration with a comment in the CSS following the new line of code added:
*, * ::before, * ::after {
transform-style: preserve-3d;
margin: 0; padding: 0;
box-sizing: border-box;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
html { overflow: hidden; background-color: #eee; }
html, body, main {
height: 100%;
color: #999;
font-family: Arial;
text-align: center;
text-transform: uppercase;
}
main, div, div::before, div::after {
display: flex;
justify-content: center;
align-items: center;
}
body {
perspective: 20rem;
}
div, div::before, div::after {
position: absolute; transform-origin: center center -5rem;
width: 10rem; height: 10rem;
border-style: solid;
border-color: #ccc;
border-width: 1rem;
background-color: rgb( 221, 221, 221, 0.75 );
opacity: 0.9; /* this is the only line that has changed from the first snippet */
}
<style>
div::before {
transform: rotateX( 90deg ); content: 'top';
}
div::after {
transform: rotateX( -90deg ); content: 'bottom';
}
div > div {
transform: rotateY( 180deg );
transform-origin: center center center;
}
div > div::before {
transform: rotateY( 90deg ); content: 'right';
}
div > div::after {
transform: rotateY( -90deg ); content: 'left';
}
.rotate_y { animation-name: rotate_y; }
#keyframes rotate_y {
0% { transform: rotateY( 0deg ); } 100% { transform: rotateY( 360deg ); }
}
.rotate_x { animation-name: rotate_x; animation-duration: 200s; }
#keyframes rotate_x {
0% { transform: rotateX( 0deg ) scale( 1 ); }
100% { transform: rotateX( 360deg ) scale( -6 ); }
}
</style>
<main class='rotate_x'>
<div class='rotate_y'>
front <div>back</div>
</div>
</main>
All of the pseudo elements are gone. Also the 'front' and 'back' divs have been flattened. The whole animation breaks.
Why does this behavior occur and is it possible to implement the opacity property on a 3D CSS animation. If so; how?

Related

Is there a way that I can make the second animation not get overwritten?

So I've tried separating the animations with a comma and having them on the same transform but it still doesn't work.
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
background: silver;
display: flex;
justify-content: center;
align-items: center;
}
.box {
width: 20%;
height: 20%;
background-color: pink;
transform: rotate(0deg) translatey(0px);
animation: wavy 3s linear infinite alternate,
float 3s linear infinite alternate;
}
#keyframes wavy {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-10deg);
}
100% {
transform: rotate(10deg);
}
}
#keyframes float {
0% {
transform: translatey(0px);
}
50% {
transform: translatey(-20px);
}
100% {
transform: translatey(0px);
}
}
<div class="container">
<div class="box"></div>
</div>
And here's a link to the codepen:
https://codepen.io/FaroukHamadi/pen/OJOWWKW
Yes - add an id to the div and set that animation on the specified id. For your example, I called it #box
EDIT ~ the id solution I had previously worked flawlessly UNLESS there are two transforms being used in the keyframe which is your case. What I would suggest is just combining the two animations into one animation and using more % increments. So instead of 0, 50, and 100, you can use 0, 25, 50, 75, 100 - to combine the two and have it seem like they are "alternating"
* {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
background: silver;
display: flex;
justify-content: center;
align-items: center;
}
.box {
width: 20%;
height: 20%;
background-color: pink;
transform: rotate(0deg) translatey(0px);
animation: wavy-float 3s linear infinite alternate;
}
#keyframes wavy-float {
0% {
transform: rotate(0deg):
}
25% {
transform: rotate(-20deg);
}
50% {
transform: translateY(-20px);
}
75% {
transform: rotate(20deg);
}
100% {
transform: translateY(20px)
}
}
<div class="container">
<div class="box"></div>
</div>

TranslateZ() is ignored for span element

The span element; the child of the div element in the snippet below is behaving unexpectedly. The span element's background-color is set to #0dd and it's transform properties are set to scale( 0.5 ) translateZ( 5rem ). The scale value is respected and shrinks the element to half size; the translateZ value however is ignored and doesn't change the position of the element.
The desired result is for the span element to be pushed forwards away from it's parent div. Why is translateZ ignored here and how can we translate span in the Z direction?
* {
box-sizing: border-box;
transform-style: preserve-3d;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body, span {
display: flex;
justify-content: center;
align-items: center;
}
body {
perspective: 30rem;
}
section, div, span {
position: absolute;
width: 10rem;
height: 10rem;
}
div, span {
opacity: 0.5;
background-color: #000;
}
div span {
transform: scale( 0.5 ) translateZ( 5rem );
background-color: #0dd;
}
<style>
.rotate_y {
animation-name: rotateY;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
#keyframes rotateY {
0% {
transform: rotateY( 0deg );
}
100% {
transform: rotateY( 360deg );
}
}
</style>
<section class='rotate_y'>
<div>
<span></span>
</div>
</section>
UPDATE: Seems to behave properly on IOS. Issue is with desktop Chrome.
The opacity seems to be the culprit but don't know why. It's probably a bug so use transparent background instead:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
height: 100vh;
margin: 0;
perspective: 30rem;
display: flex;
justify-content: center;
align-items: center;
}
.rotate_y * {
display: block;
background-color: rgba(0,0,0,0.5);
width: 10rem;
height: 10rem;
transform-style: preserve-3d;
}
.rotate_y span {
transform: scale( 0.5) translateZ( 5rem);
background-color: rgba(0,221,221,0.5);
}
.rotate_y {
transform-style: preserve-3d;
animation-name: rotateY;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
#keyframes rotateY {
0% {
transform: rotateY( 0deg);
}
100% {
transform: rotateY( 360deg);
}
}
<section class='rotate_y'>
<div>
<span></span>
</div>
</section>

Rotate child div perpendicular to parent div in 3D space

html {
font-size: 0.7rem;
}
div div {
top: 5rem;
background-color: #444;
transform-origin: top center;
}
.rotate_x {
animation-name: rotate_x;
}
#keyframes rotate_x {
0% {
transform: rotateX( 0deg );
}
100% {
transform: rotateX( 360deg );
}
}
.rotate_y {
animation-name: rotate_y;
}
#keyframes rotate_y {
0% {
transform: rotateY( 0deg );
}
100% {
transform: rotateY( 180deg );
}
}
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
html, body {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
body {
perspective: 5rem;
}
div {
position: relative;
top: -1.5rem;
width: 5rem;
height: 5rem;
background-color: #222;
border-radius: 0.5rem;
}
</style>
<div class='rotate_y'>
<div class='rotate_x'>
</div>
</div>
The gray bottom div doesn't appear to be rotating out perpendicular to the black top div as intended. Instead the bottom gray div appears to shrink until it has a negative value and gets inverted. The desired result is for the bottom gray div to rotate up as if on a hinge; making a 'L' shape in 3D space
before it comes all the way up and flips around to do the same on the other side. As the parent div rotates 360 degrees.
How can I make the bottom div create an 'L' shape as it connects with it's parent div?
You need to set transform-style and make the perspective a bit bigger:
html {
font-size: 0.7rem;
}
div div {
top: 5rem;
background-color: #444;
transform-origin: top center;
}
.rotate_x {
animation-name: rotate_x;
}
#keyframes rotate_x {
0% {
transform: rotateX( 0deg );
}
100% {
transform: rotateX( 360deg );
}
}
.rotate_y {
animation-name: rotate_y;
transform-style:preserve-3d; /* HERE */
}
#keyframes rotate_y {
0% {
transform: rotateY( 0deg );
}
100% {
transform: rotateY( 180deg );
}
}
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
html, body {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
body {
perspective: 15rem;
}
div {
position: relative;
top: -1.5rem;
width: 5rem;
height: 5rem;
background-color: #222;
border-radius: 0.5rem;
}
</style>
<div class='rotate_y'>
<div class='rotate_x'>
</div>
</div>

How to make a 3d cube rotate in place

I have created a 3d cube using CSS, now i want to rotate that cube on my HTML page. My problem is the when the cube rotates, it also moves to the sides, i need it to stay in place and rotate.
i've tried changing the posistion of my div to relative, which scattered the cube sides and still made it rotate to the sides.
I believe the problem has something to do with the transform-origin, however no matter how i change the values it doesn't help.
.spinner div {
position: absolute;
display: inline-block;
width: 300px;
height: 300px;
border: 2px solid rgb(0, 0, 0);
box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.4);
text-align: center;
font-size: 100px;
}
.spinner .face1 {
transform: translateZ(150px);
background-color: blue;
}
.spinner .face2 {
transform: rotateY(90deg) translateZ(150px);
background-color: rgb(184, 187, 31);
}
.spinner .face3 {
transform: rotateY(180deg) translateZ(150px);
background-color: green;
}
.spinner .face4 {
transform: rotateY(-90deg) translateZ(150px);
background-color: red;
}
.spinner {
animation: spincube 6s infinite;
transform-style: preserve-3d;
transform-origin: 50% 0;
}
.center-screen {
display: flex;
flex-direction: column;
justify-content: top;
align-items: top;
text-align: center;
margin-top: 10%;
margin-left: 40%;
}
#keyframes spincube {
from {
transform: rotateY(0deg);
}
to {
transform: rotateY(-360deg)
}
}
<body>
<div class="center-screen">
<div class="spinner">
<div class="face1">1</div>
<div class="face2">2</div>
<div class="face3">3</div>
<div class="face4">4</div>
</div>
</div>
</body>
as described i expected the cube to stay in place but it slides out to the side.
I would re adjust the transformation like below to make sure the slides are around the center of the main element which is an empty element.
Note the use of translateX to achieve the needed effect.
.spinner div {
position: absolute;
width: 100px;
height: 100px;
border: 2px solid rgb(0, 0, 0);
box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.4);
text-align: center;
font-size: 80px;
}
.spinner .face1 {
transform: translateZ(50px) translateX(-50%);
background-color: blue;
}
.spinner .face2 {
transform: rotateY(90deg);
background-color: rgb(184, 187, 31);
}
.spinner .face3 {
transform: translateZ(-50px) translateX(-50%) rotateY(180deg) ;
background-color: green;
}
.spinner .face4 {
transform: translateX(-100%) rotateY(-90deg);
background-color: red;
}
.spinner {
animation: spincube 6s infinite;
transform-style: preserve-3d;
display: inline-block; /* This is important !!*/
outline: 5px solid red; /* to illustrate */
}
.center-screen {
text-align: center;
margin-top: 10%;
}
#keyframes spincube {
to {
transform: rotateY(-360deg)
}
}
<div class="center-screen">
<div class="spinner">
<div class="face1">1</div>
<div class="face2">2</div>
<div class="face3">3</div>
<div class="face4">4</div>
</div>
</div>
You can also rely on left to handle this:
.spinner div {
position: absolute;
width: 100px;
left:-50px;
height: 100px;
border: 2px solid rgb(0, 0, 0);
box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.4);
text-align: center;
font-size: 80px;
}
.spinner .face1 {
transform: translateZ(50px);
background-color: blue;
}
.spinner .face2 {
transform: rotateY(90deg);
background-color: rgb(184, 187, 31);
left:0;
}
.spinner .face3 {
transform: translateZ(-50px) rotateY(180deg) ;
background-color: green;
}
.spinner .face4 {
transform:rotateY(-90deg);
background-color: red;
left:-100px;
}
.spinner {
animation: spincube 6s infinite;
transform-style: preserve-3d;
display: inline-block; /* This is important !!*/
outline: 5px solid red; /* to illustrate */
position:relative;
}
.center-screen {
text-align: center;
margin-top: 10%;
}
#keyframes spincube {
to {
transform: rotateY(-360deg)
}
}
<div class="center-screen">
<div class="spinner">
<div class="face1">1</div>
<div class="face2">2</div>
<div class="face3">3</div>
<div class="face4">4</div>
</div>
</div>
You could do something like this:
Credits: https://codepen.io/bcgwebdesign/pen/gRXxxR?editors=0100
Tip: There are a lot of demos of this kind on Codepen
/* keyframes for rotating animation */
#-webkit-keyframes spinX {
from { transform: rotateY(0); }
to { transform: rotateY(360deg); }
}
#-webkit-keyframes spinBoth {
from { transform: rotateY(0) rotateX(0); }
to { transform: rotateY(360deg) rotateX(0deg) ; }
}
#-webkit-keyframes spinY {
from { transform: rotateX(0); }
to { transform: rotateX(360deg); }
}
#-webkit-keyframes recolor {
0% { background: rgba(0,255,0,0.1); }
33% { background: rgba(255,0,0,0.1); }
66% { background: rgba(0,0,255,0.1); }
}
/* scene wrapper */
.wrapper{
height: 300px;
margin-top:0;
position:relative;
perspective: 1000px;
perspective-origin: 50% -50px;
}
/* cube wrapper */
.cube {
position: relative;
margin: 200px auto;
width: 200px;
transform-style: preserve-3d;
animation: spinBoth 5s infinite ease-in-out;
transition: all 1s linear;
}
/* outer cube */
b {
position:absolute;
width:200px;
height:200px;
display:block;
background:rgba(255,0,0,0.1);
box-shadow:inset 0 0 30px rgba(0,0,0,0.2);
font-size:20px;
text-align:center;
line-height:200px;
color:rgba(0,0,0,0.5);
font-family:sans-serif;
text-transform:uppercase;
transition: all 1s linear;
}
b.back{
transform: translateY(-100px) translateZ(-100px) rotateY(180deg);
}
b.right{
transform:translateY(-100px) rotateY(-270deg) translateX(100px);
transform-origin: top right;
}
b.left{
transform:translateY(-100px)rotateY(270deg) translateX(-100px);
transform-origin: center left;
}
b.top{
transform:rotateX(-90deg) translateY(-100px) translateZ(-100px);
transform-origin: top center;
}
b.bottom{
transform:rotateX(90deg) translateY(100px) translateZ(100px);
transform-origin: bottom center;
}
b.front{
transform: translateZ(100px) translateY(-100px);
}
<div class="wrapper">
<div class="cube">
<b class="front">front</b>
<b class="back">back</b>
<b class="top">top</b>
<b class="bottom">bottom</b>
<b class="left">left</b>
<b class="right">right</b>
</div>
</div>

Pulsing Heart CSS animation

I`m working on an animated heart only with CSS.
I want it to pulse 2 times, take a small break, and then repeat it again.
What I have now:
small ==> big ==> small ==> repeat animation
What I'm going for:
small ==> big ==> small ==> big ==> small ==> pause ==> repeat animation
How can I do it?
My code :
#button{
width:450px;
height:450px;
position:relative;
top:48px;
margin:0 auto;
text-align:center;
}
#heart img{
position:absolute;
left:0;
right:0;
margin:0 auto;
-webkit-transition: opacity 7s ease-in-out;
-moz-transition: opacity 7s ease-in-out;
-o-transition: opacity 7s ease-in-out;
transition: opacity 7s ease-in-out;}
#keyframes heartFadeInOut {
0% {
opacity:1;
}
14% {
opacity:1;
}
28% {
opacity:0;
}
42% {
opacity:0;
}
70% {
opacity:0;
}
}
#heart img.top {
animation-name: heartFadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 1s;
animation-direction: alternate;
}
<div id="heart" >
<img class="bottom" src="https://goo.gl/nN8Haf" width="100px">
<img class="top" src="https://goo.gl/IIW1KE" width="100px">
</div>
See also this Fiddle.
You can incorporate the pause into the animation. Like so:
#keyframes heartbeat
{
0%
{
transform: scale( .75 );
}
20%
{
transform: scale( 1 );
}
40%
{
transform: scale( .75 );
}
60%
{
transform: scale( 1 );
}
80%
{
transform: scale( .75 );
}
100%
{
transform: scale( .75 );
}
}
Working example:
https://jsfiddle.net/t7f97kf4/
#keyframes heartbeat
{
0%
{
transform: scale( .75 );
}
20%
{
transform: scale( 1 );
}
40%
{
transform: scale( .75 );
}
60%
{
transform: scale( 1 );
}
80%
{
transform: scale( .75 );
}
100%
{
transform: scale( .75 );
}
}
div
{
background-color: red;
width: 50px;
height: 50px;
animation: heartbeat 1s infinite;
}
<div>
Heart
</div>
Edit:
Working example with pure CSS heart shape:
https://jsfiddle.net/qLfg2mrd/
#keyframes heartbeat
{
0%
{
transform: scale( .75);
}
20%
{
transform: scale( 1);
}
40%
{
transform: scale( .75);
}
60%
{
transform: scale( 1);
}
80% {
transform: scale( .75);
}
100%
{
transform: scale( .75);
}
}
#heart
{
position: relative;
width: 100px;
height: 90px;
animation: heartbeat 1s infinite;
}
#heart:before,
#heart:after
{
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
-moz-border-radius: 50px 50px 0 0;
border-radius: 50px 50px 0 0;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transform-origin: 0 100%;
-moz-transform-origin: 0 100%;
-ms-transform-origin: 0 100%;
-o-transform-origin: 0 100%;
transform-origin: 0 100%;
}
#heart:after
{
left: 0;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transform-origin: 100% 100%;
-moz-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
-o-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
<div id="heart"></div>
Pulse 2 times, take a small break, and then repeat it again
Try this. Going with animation opacity is a bad choice. transform: scale() will do the job.
.heart:before {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
font-family: 'icons';
font-size: 21px;
text-indent: 0;
font-variant: normal;
line-height: 21px;
}
.heart {
position: relative;
width: 500px;
overflow: inherit;
margin: 50px auto;
list-style: none;
-webkit-animation: animateHeart 2.5s infinite;
animation: animateHeart 2.5s infinite;
}
.heart:before,
.heart:after {
position: absolute;
content: '';
top: 0;
left: 50%;
width: 120px;
height: 200px;
background: red;
border-radius: 100px 100px 0 0;
-webkit-transform: rotate(-45deg) translateZ(0);
transform: rotate(-45deg) translateZ(0);
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
}
.heart:after {
left: 26%;
-webkit-transform: rotate(45deg) translateZ(0);
transform: rotate(45deg) translateZ(0);
-webkit-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
#-webkit-keyframes animateHeart {
0% {
-webkit-transform: scale(0.8);
}
5% {
-webkit-transform: scale(0.9);
}
10% {
-webkit-transform: scale(0.8);
}
15% {
-webkit-transform: scale(1);
}
50% {
-webkit-transform: scale(0.8);
}
100% {
-webkit-transform: scale(0.8);
}
}
#keyframes animateHeart {
0% {
transform: scale(0.8);
}
5% {
transform: scale(0.9);
}
10% {
transform: scale(0.8);
}
15% {
transform: scale(1);
}
50% {
transform: scale(0.8);
}
100% {
transform: scale(0.8);
}
}
span {
font-family: 'Cantora One', sans-serif;
font-size: 64px;
position: absolute;
top: 165px;
}
<div class="heart">
</div>
I like ketan's answer, but I wanted to improve the heart animation to make it more realistic.
A heart does not double in size when it beats. 10% change in size looks better to me.
I like it getting both larger and smaller
When it stops moving altogether it looks dead to me. Even when it isn't beating, it needs to expand or contract a little
I removed the "alternate directions" code so that it runs the same way through every time
I explicitly have the heart start end and at normal scale (1) and have the animation in the middle of the sequence. It seems clearer that way to me.
#heart img{
position:absolute;
left:0;
right:0;
margin:0 auto;
}
#keyframes heartFadeInOut {
0% {transform: scale(1);}
25% {transform: scale(.97);}
35% {transform: scale(.9);}
45% {transform: scale(1.1);}
55% {transform: scale(.9);}
65% {transform: scale(1.1);}
75% {transform: scale(1.03);}
100% {transform: scale(1);}
}
#heart img.bottom {
animation-name: heartFadeInOut;
animation-iteration-count: infinite;
animation-duration: 2s;
}
<div id="heart" >
<img class="bottom" src="https://i.stack.imgur.com/iBCpb.png" width="100px">
</div>
Based on various comments and making use of the ♥ we'll get this:
body {
font-size: 40pt;
color: red;
}
#keyframes heartbeat {
0% {
font-size: .75em;
}
20% {
font-size: 1em;
}
40% {
font-size: .75em;
}
60% {
font-size: 1em;
}
80% {
font-size: .75em;
}
100% {
font-size: .75em;
}
}
div {
animation: heartbeat 1s infinite;
}
<div>
♥
</div>
body{
margin: 0;
padding: 0;
background: #1f1f1f;
}
body:before
{
position: absolute;
content: '';
left: 50%;
width: 50%;
height: 100%;
background: rgba(0,0,0,.2);
}
.center
{
position: absolute;
top:50%;
left: 50%;
background: #1f1f1f;
transform: translate(-50%,-50%);
padding: 100px;
border: 5px solid white;
border-radius: 100%;
box-shadow:20px 20px 45px rgba(0,0,0,.4);
z-index: 1;
overflow: hidden;
}
.heart
{
position: relative;
width: 100px;
height: 100px;
background:#ff0036;
transform: rotate(45deg) translate(10px,10px);
animation: ani 1s linear infinite;
}
.heart:before
{
content: '';
width: 100%;
height: 100%;
background: #ff0036;
position: absolute;
top:-50%;
left:0;
border-radius: 50%;
}
.heart:after
{
content:'';
width: 100%;
height: 100%;
background: #ff0036;
position: absolute;
bottom:0;
right:50%;
border-radius: 50%;
}
.center:before
{
content: '';
position: absolute;
top:0;
left:-50%;
width: 100%;
height: 100%;
background: rgba(0,0,0,.3);
}
#keyframes ani{
0%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
25%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
30%{
transform: rotate(45deg) translate(10px,10px) scale(1.4);
}
50%{
transform: rotate(45deg) translate(10px,10px) scale(1.2);
}
70%{
transform: rotate(45deg) translate(10px,10px) scale(1.4);
}
90%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
100%{
transform: rotate(45deg) translate(10px,10px) scale(1);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HeartBeat Animation</title>
<link rel="stylesheet" href="Style.css" type="text/css">
</head>
<body>
<div class="center">
<div class="heart">
</div>
</div>
</body>
</html>
Output
for more: Heart Beating Animation
I think this is what you want for your image animation. There is no need of top image. Just use bottom.
#button{
width:450px;
height:450px;
position:relative;
top:48px;
margin:0 auto;
text-align:center;
}
#heart img{
position:absolute;
left:0;
right:0;
margin:0 auto;
}
#keyframes heartFadeInOut {
0%
{ transform: scale( .5 ); }
20%
{ transform: scale( 1 ); }
40%
{ transform: scale( .5 ); }
60%
{ transform: scale( 1 ); }
80%
{ transform: scale( .5 ); }
100%
{ transform: scale( .5 ); }
}
#heart img.bottom {
animation-name: heartFadeInOut;
animation-iteration-count: infinite;
animation-duration: 1.5s;
animation-direction: alternate;
}
<div id="heart" >
<img class="bottom" src="https://goo.gl/nN8Haf" width="100px">
</div>
I needed this for a project I was working on. I was trying to make it look as realistic as possible, and this is what I came up with.
#keyframes heartbeat {
0% {
transform: scale( .95 );
}
20% {
transform: scale( .97 );
}
30% {
transform: scale( .95 );
}
40% {
transform: scale( 1 );
}
100% {
transform: scale( .95 );
}
}
animation: heartbeat 1s infinite;