← Back to Tutorials Chapter 7

Optimization & Accessibility

CSS Math Functions

calc()

Performs calculations with mixed units.

div {
  width: calc(100% - 40px);
  height: calc(100vh - 80px);
}

min() and max()

Pick the smallest or largest value from a list.

div {
  width: min(800px, 100%);
  font-size: max(1rem, 2.5vw);
}

clamp()

Clamps a value between a minimum and maximum.

p {
  font-size: clamp(1rem, 2.5vw, 2rem);
}
Tip: clamp() is perfect for fluid typography that scales gracefully across screen sizes.

Website Optimization

Minify CSS

Remove whitespace, comments, and unnecessary characters to reduce file size.

/* Before */
h1 { color: blue; font-size: 24px; }

/* After minified */
h1{color:blue;font-size:24px;}

Reduce HTTP Requests

Accessibility

Contrast

Ensure sufficient contrast between text and background colors (WCAG AA requires a ratio of at least 4.5:1).

body {
  color: #1a1a1a;
  background: #ffffff;
}

Focus Styles

Always provide visible focus indicators for keyboard users.

a:focus,
button:focus {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

Screen Reader Text

Hide text visually while keeping it accessible to screen readers.

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

Semantic Website Layout

Use semantic HTML elements and style them with CSS for a clear, accessible page structure.

<header>
  <nav>...</nav>
</header>
<main>
  <article>...</article>
  <aside>...</aside>
</main>
<footer>...</footer>
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

main {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 20px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

footer {
  margin-top: auto;
}
Semantic HTML layout with CSS styling

Practice Task

Build a semantic page layout with <header>, <nav>, <main>, <aside>, and <footer>. Add skip navigation, visible focus styles, and use clamp() for responsive font sizing.