Animated Heart with HTML and CSS: Pseudo-Elements and Pulse Animation

Animated heart preview

Demo: open the heart animation .

A heart can be built from a rotated square and two circular pseudo-elements. The animation scales the whole shape to create a pulse.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Animated Heart</title>
  <style>
    * { box-sizing: border-box; }
    body {
      min-height: 100vh;
      margin: 0;
      display: grid;
      place-items: center;
      background: #160d18;
    }
    .heart {
      position: relative;
      width: 120px;
      aspect-ratio: 1;
      background: #ff3d68;
      transform: rotate(45deg);
      animation: pulse 1.2s ease-in-out infinite;
      box-shadow: 0 0 45px rgb(255 61 104 / 55%);
    }
    .heart::before,
    .heart::after {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background: inherit;
    }
    .heart::before { left: -50%; }
    .heart::after { top: -50%; }
    @keyframes pulse {
      0%, 100% { transform: rotate(45deg) scale(.9); }
      50% { transform: rotate(45deg) scale(1.08); }
    }
    @media (prefers-reduced-motion: reduce) {
      .heart { animation: none; }
    }
  </style>
</head>
<body>
  <div class="heart" role="img" aria-label="A red heart"></div>
</body>
</html>

Save it as heart.html and open it in a browser. Change the .heart size, background color, glow, or animation duration to customize the result. The reduced-motion rule respects visitors who have disabled nonessential animation in their operating system.