Step 6: Layout Shifts Fixed

The Solution

Using NgOptimizedImage directive with proper width and height attributes prevents layout shifts by reserving space for images before they load.

Benefits:

  • No layout shifts during loading
  • Automatic lazy loading
  • Proper aspect ratio maintained
  • Better Core Web Vitals scores
  • Automatic image optimization

Fixed Code:


<div class="card">
  <img
    [ngSrc]="card.imageUrl"
    width="1080"
    height="720"
    [alt]="card.title"
    [priority]="card.id <= 4"
  >
  <h3> {{card.title }}</h3> </div>
          

Notice how the layout remains stable as images load. The space is reserved immediately, and images fade in smoothly. Priority images above the fold load first.

Card 1

Card 1

Card 2

Card 2

Card 3

Card 3

Card 4

Card 4

Card 5

Card 5

Card 6

Card 6

Card 7

Card 7

Card 8

Card 8

Card 9

Card 9

Card 10

Card 10

Card 11

Card 11

Card 12

Card 12

Card 13

Card 13

Card 14

Card 14

Card 15

Card 15

Card 16

Card 16

Card 17

Card 17

Card 18

Card 18

Card 19

Card 19

Card 20

Card 20