On this page12 sections
  1. Learning Objectives
  2. 1. Fast CSS
  3. 2. Smooth Animations
  4. 3. Readable Text
  5. 4. Visible Focus
  6. 5. Don't Rely on Colour Alone
  7. 6. User Preferences
  8. 7. Hiding Content the Right Way
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 031: Optimization & Accessibility

Category6: CSS in Depth
Lecture031 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelIntermediate

Good CSS is fast: small files that load quickly and animations that stay smooth. It's also accessible: readable for people with low vision, usable with a keyboard, comfortable for people sensitive to motion. Most of it comes down to a few habits.


Learning Objectives

  • Keep stylesheets small and fast to load
  • Animate the cheap properties
  • Keep contrast, focus and text size accessible
  • Respect prefers-reduced-motion and prefers-color-scheme

1. Fast CSS

HabitWhy
One or two external files, linked in <head>Fewer downloads; the browser can cache them
Minify for productionRemoves spaces and comments (tools: Lightning CSS, cssnano, or your build tool)
Delete unused CSSOld rules still download and get checked
Keep selectors short.card-title is quicker to read and match than body div.main ul li a span
Avoid @import in CSS filesEach one waits for the previous file
Use font-display: swap for web fontsText shows immediately (Lecture 010)

2. Smooth Animations

Animate transform and opacity. The browser can do these on the graphics card without recalculating the layout. Animating width, height, top or margin makes the whole page recalculate on every frame.

css
/* smooth */
.card:hover { transform: translateY(-4px); }

/* janky on slow phones */
.card:hover { margin-top: -4px; }

3. Readable Text

  • Contrast: at least 4.5 : 1 for normal text (browser DevTools show the ratio when you pick a colour).
  • Size: use rem, so text grows when visitors increase their browser's font size.
  • Line length: about 60–75 characters, e.g. max-width: 65ch.
  • Line height: 1.5 or more for paragraphs.

Example

html
<p style="max-width: 65ch; line-height: 1.6; font-size: 1.05rem; color: #212529;">
  This paragraph is limited to about 65 characters per line, has comfortable line spacing and
  strong contrast, so it's easy to read for a long time, even on a phone in bright sunlight.
</p>

4. Visible Focus

css
:focus-visible { outline: 3px solid #f59f00; outline-offset: 2px; }

Never remove focus outlines without a replacement (Lecture 008).


5. Don't Rely on Colour Alone

Example

html
<style>
  .err { color: #c92a2a; }
  .err::before { content: "⚠ "; }
  a.inline { color: #1971c2; text-decoration: underline; }
</style>

<p class="err">Password must be at least 8 characters.</p>
<p>Read the <a class="inline" href="#">terms</a>: underlined, not just coloured.</p>

6. User Preferences

Example: these follow the visitor's device settings:

html
<style>
  .panel { padding: 14px; background: #f8f9fa; color: #212529; border-radius: 8px; }
  @media (prefers-color-scheme: dark) {
    .panel { background: #212529; color: #f8f9fa; }
  }
  .spin { display: inline-block; animation: spin 2s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }
  @media (prefers-reduced-motion: reduce) {
    .spin { animation: none; }
  }
</style>

<div class="panel">Light or dark, depending on your device setting. <span class="spin">⚙</span></div>

7. Hiding Content the Right Way

GoalCSS
Hide from everyonedisplay: none
Hide visually, keep for screen readersthe "visually hidden" class below
css
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

Use it for labels that are obvious visually but needed by screen readers.


Common Beginner Mistakes

1. Animating width or top. Use transform instead.

2. Font sizes in px only. Visitors who enlarge text in their browser settings can't.

3. Pale grey text. It fails contrast checks.

4. Ignoring reduced-motion settings. Some visitors feel ill from motion.


Practice Questions

  1. Which two properties are cheapest to animate?
  2. What is minifying?
  3. What's the minimum contrast ratio for normal text?
  4. Limit paragraphs to a comfortable line length.
  5. Which media query detects that a visitor wants less motion?
  6. How do you hide text visually but keep it for screen readers?

Practice Answers

  1. transform and opacity.
  2. Removing spaces, line breaks and comments to make the file smaller.
  3. 4.5 : 1
  4. p { max-width: 65ch; }
  5. @media (prefers-reduced-motion: reduce)
  6. Use a "visually hidden" class: tiny, clipped and absolutely positioned.

Key Takeaways

  • Fewer, minified files; unused CSS removed; short selectors.
  • Animate transform and opacity only.
  • Contrast, rem sizes, ~65ch lines and visible focus keep text accessible.
  • Respect prefers-reduced-motion and prefers-color-scheme.