AdvancedLesson 29 of 307 min read

CSS performance, debugging and accessibility

Debug with developer tools, speed up your CSS, and make every page usable with focus styles, contrast and reduced motion.

In this lesson you will

  • Debug with developer tools
  • Speed up CSS and avoid layout shift
  • Apply core accessibility rules
  • Test with Lighthouse and the keyboard

About 18 minutes

Fast, fixable and fair

Your page looks right on your laptop. Then a friend opens it on an older phone on a slow connection: the text jumps as fonts load, the layout shifts when an image appears, and a keyboard user cannot see where they are. Good CSS is not only pretty. It is quick, easy to debug, and usable by everyone. This lesson covers all three.

Part 1: debugging with developer tools

Every browser has built-in developer tools. Open them with F12, or right-click an element and choose Inspect. They are the single best CSS teacher you have.

  1. Inspect an element. The Elements panel shows its HTML, and the Styles panel lists every rule that applies.
  2. Look for crossed-out lines. A struck-through declaration lost the cascade. The rule that beat it is listed above it.
  3. Toggle and edit live. Untick a declaration or change a value to see the effect instantly. Changes vanish on refresh, so copy the ones you like.
  4. Read the box model diagram. The Computed tab shows the margin, border, padding and content sizes of the element.
  5. Use the Layout tools. Flexbox and Grid badges draw overlays for tracks and gaps.
  6. Test devices. Toggle the device toolbar to try phone sizes, and emulate dark mode or reduced motion in the Rendering panel.

A reliable method when something looks wrong: make it obvious. Temporarily add outline: 2px solid red to the suspect elements. Outlines do not change layout, so you see the real boxes.

/* the debug trick: outline everything */
* { outline: 1px solid rgba(255, 0, 100, .4); }

Something is off: the box is wider than its container. Add the outline trick to find the cause, then fix it with box-sizing: border-box.

<style>
 body { font-family:Arial; padding:20px; }
 .wrap { width:260px; background:#EFEAFD; padding:10px; }
 .box { width:100%; padding:20px; border:4px solid #5A28C8; background:#fff; }
</style>
<div class="wrap"><div class="box">I overflow my parent.</div></div>

.box { box-sizing: border-box; }

<style>
 body { font-family:Arial; padding:20px; }
 .wrap { width:260px; background:#EFEAFD; padding:10px; }
 .box { width:100%; padding:20px; border:4px solid #5A28C8; background:#fff; box-sizing:border-box; }
</style>
<div class="wrap"><div class="box">I overflow my parent.</div></div>

Common bugs and their causes

  • My style does nothing. A typo in the property, a missing semicolon on the line above, a more specific rule winning, or the stylesheet not loaded. Check the Styles panel for crossed-out or invalid lines.
  • Horizontal scroll on phones. Something is wider than the screen. Often an image without max-width: 100%, a fixed width, or a long unbroken word. Find it with the outline trick.
  • z-index does not work. It only applies to positioned elements, and stacking contexts can trap it.
  • Margins collapse. Vertical margins of neighbouring blocks merge. Use padding, flex gap or display: flow-root.
  • Height collapses to zero. The children are floated or absolutely positioned. Use flex or grid instead.
  • Flex child will not shrink. Add min-width: 0 so long content can wrap or truncate.

Part 2: performance

CSS speed matters because the browser cannot draw the page until it has read your stylesheet. Heavier, slower CSS means a slower first view.

  • Keep it small. Remove unused rules. Compress with minification, and serve it with gzip or Brotli, which hosts usually do automatically.
  • Avoid giant frameworks if you use only a sliver of them.
  • Load fonts wisely. Use font-display: swap and only the weights you need. Two or three font files is plenty.
  • Reserve space for images. Give images width and height attributes or an aspect-ratio so the layout does not jump. That jump is called layout shift, and Google measures it.
  • Animate cheap properties. transform and opacity are smooth. Animating width, top or box-shadow forces re-layout.
  • Use content-visibility: auto on long off-screen sections, so the browser skips drawing them until needed.
  • Use will-change sparingly. It reserves resources. Add it only to something that is about to animate.
.article-section {
  content-visibility: auto;
  contain-intrinsic-size: 800px; /* estimated height to avoid jumps */
}

img { aspect-ratio: 16 / 9; width: 100%; height: auto; }

@font-face {
  font-family: 'Sora';
  src: url(sora.woff2) format('woff2');
  font-display: swap;
}

Reserve the image space so nothing jumps: give the .thumb box a 16 / 9 aspect ratio and full width. Then notice the layout no longer shifts.

<style>
 body { font-family:Arial; padding:20px; max-width:360px; }
 .thumb { background:linear-gradient(135deg,#6C2BE0,#46D3DC); border-radius:12px; /* aspect ratio + width */ }
</style>
<div class="thumb"></div>
<p>The text below stays put because the box already holds its space.</p>

.thumb { aspect-ratio: 16 / 9; width: 100%; }

<style>
 body { font-family:Arial; padding:20px; max-width:360px; }
 .thumb { background:linear-gradient(135deg,#6C2BE0,#46D3DC); border-radius:12px; aspect-ratio:16 / 9; width:100%; }
</style>
<div class="thumb"></div>
<p>The text below stays put because the box already holds its space.</p>

Part 3: accessibility in CSS

Accessibility means people with different abilities can use your site: someone with low vision, a person who cannot use a mouse, a person sensitive to motion. CSS has a big say in all of it.

  • Contrast. Body text needs a contrast ratio of at least 4.5 to 1 against its background, large text 3 to 1. Developer tools show the ratio when you inspect a colour.
  • Visible focus. Never write outline: none without a replacement. Use :focus-visible to show a clear ring only for keyboard users.
  • Respect motion preferences. Wrap big animations in @media (prefers-reduced-motion: no-preference), or switch them off under reduce.
  • Resizable text. Size fonts in rem, not pixels, so users who enlarge text in settings are respected.
  • Tap targets. Buttons and links should be at least 44 by 44 pixels, with room around them.
  • Do not rely on colour alone. A red border for an error should also have text or an icon.
  • Hide properly. display: none hides from everyone including screen readers. Use a visually-hidden class for text only screen readers should hear.
:focus-visible {
  outline: 3px solid #5A28C8;
  outline-offset: 3px;
  border-radius: 4px;
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

Give the link a clear keyboard focus ring using :focus-visible. Click in the result and press Tab to see it.

<style>
 body { font-family:Arial; padding:20px; }
 a { color:#5A28C8; font-weight:700; margin-right:16px; }
 /* focus-visible rule */
</style>
<a href="#">Home</a><a href="#">Lessons</a><a href="#">Tools</a>

a:focus-visible { outline: 3px solid #5A28C8; outline-offset: 3px; border-radius: 4px; }

<style>
 body { font-family:Arial; padding:20px; }
 a { color:#5A28C8; font-weight:700; margin-right:16px; }
 a:focus-visible { outline: 3px solid #5A28C8; outline-offset: 3px; border-radius: 4px; }
</style>
<a href="#">Home</a><a href="#">Lessons</a><a href="#">Tools</a>

Check your work

  • Run the page through the free W3C CSS validator to catch typos.
  • Use the Lighthouse tab in Chrome developer tools for performance and accessibility scores, with specific fixes listed.
  • Unplug your mouse and try the page with only Tab, Enter and Space.
  • Zoom the page to 200 percent and make sure nothing breaks.
  • Test on a real phone, not only the emulator.
Common mistake

Removing the focus outline because it looks ugly. Keyboard users then cannot tell where they are. Restyle it, never remove it.

Fix the biggest thing first

Run Lighthouse, pick the single largest issue, fix it, run again. Small, measured steps beat a giant rewrite.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which is the best first step when a style does nothing?

  1. Reinstall the browser
  2. Inspect the element and look for crossed-out or invalid declarations
  3. Add !important everywhere
  4. Delete the file

The Styles panel shows what won and why.

Which properties are cheapest to animate?

  1. width and top
  2. transform and opacity
  3. margin
  4. box-shadow

They avoid re-layout.

What prevents layout shift from images?

  1. Reserving space with width and height or aspect-ratio
  2. Using bigger images
  3. Removing alt text
  4. Using float

The browser knows the size before the image loads.

What minimum contrast ratio is recommended for normal text?

  1. 1.5 to 1
  2. 3 to 1
  3. 4.5 to 1
  4. 10 to 1

4.5 to 1 is the WCAG AA target.

What should you do about the focus outline?

  1. Remove it
  2. Restyle it with :focus-visible
  3. Hide it with opacity
  4. Ignore it

Keyboard users need a clear indicator.

Summary

  • Inspect, find crossed-out rules, and use outlines to debug.
  • Reserve image space and animate transform and opacity.
  • Keep focus visible, contrast high and motion optional.
  • Test with Lighthouse, the keyboard and a real phone.

Next lesson

Next: the capstone, where you build a design system and a complete styled landing page.

Questions

Why does my page scroll sideways on mobile?

Something is wider than the screen. Use the outline trick to find it, and check images and fixed widths.

Does accessibility hurt design?

No. Good contrast, clear focus and readable sizes improve the experience for everyone.

How fast should CSS be?

Keep critical CSS small, ideally under about 50 KB compressed, and remove what you do not use.

Progress is saved in this browser.