Saturday, January 26, 2019

CSS Heart beating

We can make a heart with just css even having the only div.
  1. <div id="heart"></div>
<div id="heart"></div>

The most tricky part is to draw a heart. Luckily, it is pretty simple - it can be composed of 3 shapes - square and 2 circles. Square will be described with main css class, and 2 circles with ::before and ::after pseudo classes

And beating will be implemented with css @keyframe animation



Now let's have a look at the whole css.
  1. :root{
  2. --width: 80px;
  3. --color: rgb(255, 51, 0);
  4. }
  5. #heart{
  6. width: var(--width);
  7. height: var(--width);
  8. margin-left: 40px;
  9. background: var(--color);
  10. background-image: var(--color_gradient);
  11. border-color: transparent;
  12. border-style: solid;
  13. border-width: 0px;
  14. transform: rotate(45deg);
  15. animation-name: heartbeat;
  16. animation-duration: 1s;
  17. animation-iteration-count: infinite;
  18. }
  19. #heart::before{
  20. content: "";
  21. width: var(--width);
  22. height: var(--width);
  23. position: absolute;
  24. transform: translateX(-50%);
  25. background: var(--color);
  26. background-image: var(--color_gradient);
  27. border-color: transparent;
  28. border-style: solid;
  29. border-width: 0px;
  30. border-radius: 50%;
  31. }
  32. #heart::after{
  33. content: "";
  34. width: var(--width);
  35. height: var(--width);
  36. position: absolute;
  37. transform: translateY(-50%);
  38. background: var(--color);
  39. background-image: var(--color_gradient);
  40. border-color: transparent;
  41. border-style: solid;
  42. border-width: 0px;
  43. border-radius: 50%;
  44. }
  45. @keyframes heartbeat{
  46. 0% {transform: scale(1.0) rotate(45deg);}
  47. 20% {transform: scale(1.1) rotate(45deg);}
  48. 100% {transform: scale(1.0) rotate(45deg);}
:root{
  --width: 80px;
  --color: rgb(255, 51, 0);
}


#heart{
  width: var(--width);
  height: var(--width);
  margin-left: 40px;
  background: var(--color);
  background-image: var(--color_gradient);
  border-color: transparent;
  border-style: solid;
  border-width: 0px;
  transform: rotate(45deg);
  animation-name: heartbeat;
  animation-duration: 1s;
  animation-iteration-count: infinite;
}

#heart::before{
  content: "";
  width: var(--width);
  height: var(--width);
  position: absolute;
  transform: translateX(-50%);
  background: var(--color);
  background-image: var(--color_gradient);
  border-color: transparent;
  border-style: solid;
  border-width: 0px;
  border-radius: 50%;
}

#heart::after{
  content: "";
  width: var(--width);
  height: var(--width);
  position: absolute;
  transform: translateY(-50%);
  background: var(--color);
  background-image: var(--color_gradient);
  border-color: transparent;
  border-style: solid;
  border-width: 0px;
  border-radius: 50%;
}


@keyframes heartbeat{
  0% {transform: scale(1.0) rotate(45deg);}
  20% {transform: scale(1.1) rotate(45deg);}
  100% {transform: scale(1.0) rotate(45deg);}


You can try it on JSFiddler:
http://jsfiddle.net/AndrewBuntsev/r0hw9s6u/

No comments:

Post a Comment