On this page12 sections
Lecture 031: Optimization & Accessibility
| Category | 6: CSS in Depth |
| Lecture | 031 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
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-motionandprefers-color-scheme
1. Fast CSS
| Habit | Why |
|---|---|
One or two external files, linked in <head> | Fewer downloads; the browser can cache them |
| Minify for production | Removes spaces and comments (tools: Lightning CSS, cssnano, or your build tool) |
| Delete unused CSS | Old 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 files | Each one waits for the previous file |
Use font-display: swap for web fonts | Text 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.
/* 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
<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
: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
<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:
<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
| Goal | CSS |
|---|---|
| Hide from everyone | display: none |
| Hide visually, keep for screen readers | the "visually hidden" class below |
.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
- Which two properties are cheapest to animate?
- What is minifying?
- What's the minimum contrast ratio for normal text?
- Limit paragraphs to a comfortable line length.
- Which media query detects that a visitor wants less motion?
- How do you hide text visually but keep it for screen readers?
Practice Answers
transformandopacity.- Removing spaces, line breaks and comments to make the file smaller.
- 4.5 : 1
p { max-width: 65ch; }@media (prefers-reduced-motion: reduce)- Use a "visually hidden" class: tiny, clipped and absolutely positioned.
Key Takeaways
- Fewer, minified files; unused CSS removed; short selectors.
- Animate
transformandopacityonly. - Contrast,
remsizes, ~65ch lines and visible focus keep text accessible. - Respect
prefers-reduced-motionandprefers-color-scheme.