KodingKaro
CSS · Advanced · Performance Optimization

Stylesheet loading and unused CSS

A page should load only the styles it needs. Extra rules can increase transfer and maintenance costs, but performance work should be based on measurement.

✓ What you will learn in this topic

  • Explain the key idea: Remove unused styles and reduce layout shifts; measure what matters.
  • Apply the concept: The image maintains a predictable 16:9 or 3:2 area while loading, reducing sudden content movement.
  • Avoid this common mistake:

Concept explanation

Remove obsolete CSS and avoid shipping large unused libraries when a small component is enough. Split styles only when the application’s loading strategy benefits from it. Avoid importing the same stylesheet through multiple routes.

Reserve image and embed space with dimensions or aspect ratio to reduce layout shifts. A visually fast page is not only a small stylesheet; fonts, images, and content stability also matter.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

Expected result: The image maintains a predictable 16:9 or 3:2 area while loading, reducing sudden content movement.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.course-card__image {
  width: 100%;
  aspect-ratio: 3 / 2;
  object-fit: cover;
  background: #e2e8f0;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Optimizing selector speed based on myths while ignoring oversized assets, layout shifts, and expensive visual effects.

Best practice

Measure real user impact, remove unused CSS, reserve media dimensions, and prefer efficient motion properties.

Practice exercise

Audit a page for unused styles, unreserved image space, and animations of layout properties; propose measured improvements. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Remove unused styles and reduce layout shifts; measure what matters.
  • Measure real user impact, remove unused CSS, reserve media dimensions, and prefer efficient motion properties.

Knowledge check

Why set an aspect ratio for media?

Reveal the explanation

A predictable media box helps surrounding content remain stable.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

Expected result:

The image maintains a predictable 16:9 or 3:2 area while loading, reducing sudden content movement.

Hands-on practice task

Audit a page for unused styles, unreserved image space, and animations of layout properties; propose measured improvements.

In this section

Other lessons in Performance Optimization

Broaden your knowledge

Related learning topics