How Do You Write Accessible CSS for Modern Websites?

Learn How Do You Write Accessible CSS for Modern Websites? with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

Accessible CSS means writing styles that allow as many people as possible to use and understand a website, including people who navigate with a keyboard, have low vision, use magnification, experience motion sensitivity, or interact with different devices and input methods.

Accessibility is not only an HTML or JavaScript concern. CSS directly affects whether content can be read, whether controls can be identified, whether focus can be seen, whether layouts work at different sizes, and whether animations are comfortable to use. Good CSS should support the semantic structure and interaction behavior already provided by HTML.

What Does Accessible CSS Mean?

Readable

Text remains clear and understandable.

Focusable

Interactive elements have a visible keyboard focus state.

Responsive

Content remains usable across different viewport sizes.

Understandable

States such as errors and disabled controls are clearly communicated.

Comfortable

Animations and visual effects do not create unnecessary discomfort.

Why Is CSS Accessibility Important?

Imagine a website where the text is too small, keyboard focus is invisible, buttons are distinguishable only by color, or content becomes clipped when users increase text size. The HTML may still be technically present, but the experience can become difficult or impossible for some users.

CSS accessibility focuses on removing these visual and interaction barriers. It does not mean every website must look identical or avoid visual design. It means the design should remain usable when people interact with it in different ways.

Visible Keyboard Focus

One of the most important accessibility rules in CSS is to make keyboard focus visible. Users who navigate with a keyboard need to know which interactive element is currently active.

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 3px;
}

The :focus-visible pseudo-class is useful for styling a focus indicator when the browser determines that a visible focus indication is appropriate, particularly for keyboard interaction.

Why You Should Not Remove outline

A common CSS mistake is:

button:focus {
  outline: none;
}

Removing the default outline is not automatically wrong, but removing it without providing another obvious focus indicator makes keyboard navigation difficult.

Instead, replace the browser outline with a deliberate focus style:

button:focus-visible {
  outline: 3px solid #f97316;
  outline-offset: 3px;
}

Color Contrast and Accessible Text

Text needs sufficient visual contrast against its background to remain readable. A light gray paragraph on a white background may look elegant but can be difficult to read, especially for users with low vision.

Difficult to Read

This text has low contrast against the background.

Better

This text provides stronger contrast.

Do not choose text and background colors purely for visual appearance. Always evaluate whether the resulting combination remains readable.

Do Not Use Color as the Only Signal

Color should not be the only way you communicate a state. For example, making invalid input red without any other indication may not provide enough information to every user.

Better Error Design

Invalid email

Enter an email address such as name@example.com.

The error can use color as a visual cue, but text and appropriate semantic or programmatic information should provide the actual meaning.

Accessible Form Styling

CSS should support the structure of accessible HTML forms. Form fields should have meaningful labels, and their visual design should make the relationship between labels and controls clear.

<div class="field">

  <label for="email">
    Email address
  </label>

  <input
    id="email"
    name="email"
    type="email"
  >

</div>
.field {
  display: grid;
  gap: 0.5rem;
}

label {
  font-weight: 600;
  color: #334155;
}

input {
  width: 100%;
  padding: 0.75rem 1rem;

  border: 1px solid #94a3b8;
  border-radius: 8px;
}

input:focus-visible {
  outline: 3px solid
    rgba(37, 99, 235, 0.3);

  outline-offset: 2px;
  border-color: #2563eb;
}

Accessible Buttons and Links

Buttons and links should remain visually recognizable as interactive controls. CSS should not make an interactive element look like ordinary text when its purpose is to be clicked or activated.

Button

Continue

Be careful when styling links and buttons to ensure the visual design still communicates their different roles.

Link Underlines and Readability

Links inside blocks of text should be easy to identify. Underlines are a simple way to provide a visual distinction without relying only on color.

article a {
  color: #1d4ed8;
  text-decoration: underline;
  text-decoration-thickness: 0.08em;
  text-underline-offset: 0.15em;
}

Readable Font Sizes

CSS should use font sizes that are practical for the content and interface. Avoid making body text unnecessarily small just to fit more content on the screen.

body {
  font-size: 1rem;
  line-height: 1.6;
}

p {
  max-width: 70ch;
}

Relative units such as rem are useful because they allow typography to participate more naturally in user and document-level scaling.

Line Height and Text Readability

Text that is packed too tightly can be difficult to read. A reasonable line-height gives paragraphs enough vertical separation.

body {
  line-height: 1.6;
}

p {
  margin-block: 0 1rem;
}

The ideal spacing depends on the font, size, language, and interface, so typography should be tested with actual content rather than chosen only from isolated examples.

Do Not Prevent Text Scaling

Responsive CSS should avoid unnecessary restrictions that prevent content from adapting when users increase text size or zoom the page. Fixed heights and aggressive overflow rules can cause content to become clipped.

/* Risky for text-heavy content */

.card {
  height: 100px;
  overflow: hidden;
}

Content should be allowed to grow when its text needs more space. Prefer flexible sizing where possible.

.card {
  min-height: 100px;
  padding: 1rem;
}

Responsive Layouts and Accessibility

A responsive layout is an accessibility feature because content should remain usable across different viewport sizes and device orientations. Avoid designing components that depend on a single fixed desktop width.

.container {
  width: min(
    calc(100% - 2rem),
    70rem
  );

  margin-inline: auto;
  padding-inline: 1rem;
}

Flexible widths allow the content area to adapt rather than forcing horizontal scrolling for ordinary content.

Avoid Horizontal Overflow

Horizontal scrolling can be appropriate for certain content such as wide data tables or code blocks, but normal page content should not unexpectedly extend beyond the viewport.

img {
  max-width: 100%;
  height: auto;
}

.container {
  width: min(
    100% - 2rem,
    1200px
  );

  margin-inline: auto;
}

Accessible Images with CSS

CSS can help images fit their containers, but accessibility begins with the HTML image element and appropriate alternative text when needed. CSS cannot replace meaningful alt text.

<img
  src="profile.jpg"
  alt="Developer profile photo"
  class="profile-image"
>
.profile-image {
  width: 100%;
  max-width: 320px;
  height: auto;
  border-radius: 12px;
}

Reduced Motion with prefers-reduced-motion

Animations can improve interfaces, but some users prefer reduced motion. CSS provides the prefers-reduced-motion media feature so styles can adapt to a user's operating-system preference.

@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms;
    animation-iteration-count: 1;
    transition-duration: 0.01ms;
    scroll-behavior: auto;
  }

}

The exact reduced-motion strategy should depend on the design. The important concept is to avoid assuming that every user wants the same amount of motion.

Do Not Hide Important Information with CSS

CSS visibility techniques should be chosen carefully. display: none removes an element from the rendered layout, while visibility: hidden hides the element while preserving its layout space.

.hidden {
  display: none;
}

.invisible {
  visibility: hidden;
}

These are not interchangeable. Before hiding content, consider whether users still need access to the information. A visual-only design should not unintentionally hide important information from assistive technology users.

Visually Hiding Content Without Removing It

Sometimes a page needs content to remain available to assistive technology while being visually hidden. This requires a carefully designed utility class rather than display: none.

.visually-hidden {
  position: absolute;

  width: 1px;
  height: 1px;

  padding: 0;
  margin: -1px;

  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;

  border: 0;
}

This pattern is commonly used for information such as visually hidden labels or helper text. It should not be used to hide important content simply because the design does not currently have room for it.

Accessible Disabled States

Disabled controls should be visually understandable, but CSS should not make them so faint that users cannot read the content.

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

Remember that visual styling alone does not actually disable a control. The HTML disabled attribute or appropriate application behavior must handle the actual interaction state.

Accessible Hover States

Hover styles are useful for pointer users, but hover should never be the only way to discover that something is interactive.

button {
  background-color: #f97316;
  color: white;
}

button:hover {
  background-color: #ea580c;
}

button:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 3px;
}

The button now provides visual feedback for pointer interaction and a separate focus indicator for keyboard navigation.

Accessible Target Size

Interactive controls should have enough physical space to operate comfortably. CSS can help by providing reasonable padding, minimum dimensions, and spacing between controls.

button,
a.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;

  min-height: 44px;
  padding-inline: 1rem;

  border-radius: 8px;
}

The correct dimensions depend on the component, surrounding spacing, platform, and interaction model. Avoid packing many small controls directly next to each other.

Accessible Tables and Overflow

Wide tables can require horizontal scrolling on small screens. It is often better to make the table container scroll than to shrink the text to an unreadable size.

.table-wrapper {
  width: 100%;
  overflow-x: auto;
}

table {
  min-width: 700px;
  border-collapse: collapse;
}

The page remains usable while preserving the readability of the table content.

Accessible Dark Mode

Dark mode is a visual preference, not automatically an accessibility improvement. A dark interface still needs readable text, sufficient contrast, visible focus indicators, and clear interactive states.

@media (prefers-color-scheme: dark) {

  body {
    background-color: #0f172a;
    color: #f8fafc;
  }

  input,
  textarea,
  select {
    background-color: #1e293b;
    color: #f8fafc;

    border-color: #64748b;
  }

}

Color schemes should be designed around readability and consistency rather than treating dark mode as simply "invert every color."

Avoid Text Inside Background Images

Placing important text over a background image can create unpredictable contrast because the image itself may contain both light and dark areas. If text overlays an image, use a reliable background treatment or overlay to maintain readability.

.hero {
  position: relative;

  background:
    linear-gradient(
      rgba(0, 0, 0, 0.55),
      rgba(0, 0, 0, 0.55)
    ),
    url("hero.jpg");

  background-size: cover;
  background-position: center;

  color: white;
}

Treat the overlay as part of the visual design and test it against the actual images used in production.

CSS and Content Order

Visual layouts can sometimes be rearranged using CSS Grid or Flexbox. Be careful not to create a visual reading order that differs unexpectedly from the underlying document structure.

.layout {
  display: grid;

  grid-template-columns:
    1fr 2fr;

  gap: 2rem;
}

The CSS layout should support the logical structure already provided by HTML. Do not use visual positioning to create confusing reading or navigation sequences.

Accessible CSS and Semantic HTML

CSS cannot fix incorrect HTML semantics by itself. Styling a div to look like a button does not automatically make it behave like a native button.

Avoid

<div class="button">
  Submit
</div>

Prefer

<button type="submit">
  Submit
</button>

The accessible foundation starts with semantic HTML. CSS should enhance that foundation rather than attempting to recreate native control behavior visually.

Accessible CSS Reset

CSS resets can provide a useful baseline, but accessibility-related defaults should not be removed blindly. For example, removing all outlines or setting aggressive global rules can create usability problems.

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
}

img {
  max-width: 100%;
  height: auto;
}

Keep a reset focused on predictable rendering rather than treating every browser default as something that must be removed.

Common CSS Accessibility Mistakes

Removing Focus Outlines

Removing keyboard focus without replacing it makes interactive controls difficult to navigate.

Using Color as the Only Indicator

Error, status, and interaction information should not depend only on a user's ability to distinguish colors.

Tiny Text

Extremely small typography can make important content difficult to read, especially on mobile devices or under magnification.

Fixed Heights on Text Content

Fixed-height containers can clip text when content grows or users increase text size.

Overusing Animations

Large or continuous motion can be uncomfortable for some users. Respect reduced-motion preferences where appropriate.

Styling Divs as Buttons

Visual appearance does not provide the native keyboard, focus, and interaction behavior of a semantic button.

Accessible CSS Best Practices

  • Keep keyboard focus clearly visible.
  • Use strong enough text and background contrast for readable content.
  • Do not rely only on color to communicate meaning.
  • Use responsive widths instead of unnecessarily fixed layouts.
  • Allow text-heavy components to grow with their content.
  • Use semantic HTML and let CSS enhance it rather than replace native controls.
  • Respect prefers-reduced-motion when animations are significant.
  • Keep interactive controls visually recognizable and sufficiently spaced.
  • Test forms with keyboard navigation, not only a mouse or touch screen.
  • Check the actual content at different viewport sizes and zoom levels.
  • Use DevTools and accessibility testing tools during development.

A Practical Accessible Component

<article class="card">

  <h2>Developer Tools</h2>

  <p>
    Explore tools that help you
    work more efficiently.
  </p>

  <a
    href="#tools"
    class="card-link"
  >
    Explore tools
  </a>

</article>
.card {
  width: min(
    100%,
    420px
  );

  padding: 1.5rem;

  border: 1px solid #cbd5e1;
  border-radius: 12px;

  background: white;
  color: #1e293b;
}

.card h2 {
  margin-top: 0;
}

.card p {
  line-height: 1.6;
}

.card-link {
  display: inline-flex;
  align-items: center;

  min-height: 44px;
  margin-top: 1rem;

  color: #1d4ed8;
  font-weight: 600;

  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.card-link:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 3px;
}

@media (prefers-reduced-motion: reduce) {
  .card-link {
    scroll-behavior: auto;
  }
}

The component uses semantic HTML, readable text, flexible sizing, a visible link, a keyboard focus state, and a layout that can adapt to smaller screens.

Accessibility Testing Checklist

Keyboard

Can you reach every important interactive control with the keyboard?

Focus

Can you clearly see which control currently has focus?

Contrast

Is important text readable against its background?

Zoom

Does content remain usable when the page is enlarged?

Mobile

Does the interface remain usable on narrow screens?

Motion

Does the design respect users who prefer reduced motion?

Key Takeaways

Accessible CSS helps ensure that visual design and interaction remain usable for people with different needs and input methods.

A visible keyboard focus state is one of the most important accessibility considerations in CSS.

Good contrast improves readability, but color should not be the only way to communicate errors, status, or meaning.

Responsive layouts should allow content to adapt rather than relying on rigid fixed dimensions.

Avoid fixed heights on text-heavy components when those heights can cause content clipping.

Use prefers-reduced-motion when appropriate to provide a less animated experience.

Semantic HTML provides the accessible foundation; CSS should enhance that foundation rather than recreate native controls visually.

Accessibility should be tested in real interaction scenarios, including keyboard navigation, different viewport sizes, zoom, and reduced-motion preferences.

Code Examples

css
/* Accessible focus styles */

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 3px;
}
    
css
/* Readable typography */

body {
  font-size: 1rem;
  line-height: 1.6;
  color: #1e293b;
  background: #ffffff;
}

p {
  max-width: 70ch;
}
    
css
/* Responsive container */

.container {
  width: min(
    calc(100% - 2rem),
    70rem
  );

  margin-inline: auto;
  padding-inline: 1rem;
}

img {
  max-width: 100%;
  height: auto;
}
    
css
/* Respect reduced motion */

@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms;
    animation-iteration-count: 1;
    transition-duration: 0.01ms;
    scroll-behavior: auto;
  }

}
    
css
/* Accessible interactive control */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;

  min-height: 44px;
  padding: 0.75rem 1rem;

  border: 0;
  border-radius: 8px;

  background: #f97316;
  color: white;

  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.button:hover {
  background: #ea580c;
}

.button:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 3px;
}

.button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
    
html
<!DOCTYPE html>
<html>
<head>

  <style>

    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      padding: 2rem;

      font-family:
        Arial,
        sans-serif;

      font-size: 1rem;
      line-height: 1.6;

      color: #1e293b;
      background: #f8fafc;
    }

    .card {
      width: min(
        100%,
        500px
      );

      margin-inline: auto;
      padding: 2rem;

      background: white;

      border: 1px solid
        #cbd5e1;

      border-radius: 12px;
    }

    .field {
      display: grid;
      gap: 0.5rem;
      margin-bottom: 1rem;
    }

    label {
      font-weight: 600;
    }

    input {
      width: 100%;

      min-height: 44px;
      padding: 0.75rem 1rem;

      font: inherit;

      border: 1px solid
        #94a3b8;

      border-radius: 8px;
    }

    input:focus-visible {
      outline: 3px solid
        rgba(37, 99, 235, 0.3);

      outline-offset: 2px;

      border-color: #2563eb;
    }

    button {
      min-height: 44px;
      padding: 0.75rem 1rem;

      border: 0;
      border-radius: 8px;

      background: #f97316;
      color: white;

      font: inherit;
      font-weight: 600;
      cursor: pointer;
    }

    button:focus-visible {
      outline: 3px solid #2563eb;
      outline-offset: 3px;
    }

    @media (prefers-reduced-motion: reduce) {
      *,
      *::before,
      *::after {
        scroll-behavior: auto;
        animation: none;
        transition: none;
      }
    }

  </style>

</head>

<body>

  <main class="card">

    <h1>
      Contact Us
    </h1>

    <form>

      <div class="field">
        <label for="name">
          Name
        </label>

        <input
          id="name"
          name="name"
          type="text"
        >
      </div>

      <div class="field">
        <label for="email">
          Email address
        </label>

        <input
          id="email"
          name="email"
          type="email"
        >
      </div>

      <button type="submit">
        Send Message
      </button>

    </form>

  </main>

</body>
</html>
    

Pro Tip

Do not treat accessibility as a final CSS checklist. Build it into your component styles from the beginning: keep focus visible, avoid color-only communication, use flexible dimensions, preserve readable text, support reduced motion, and test the interface with a keyboard as well as a mouse. The best accessible CSS usually comes from simple, predictable styling rather than special accessibility-only hacks.

More CSS lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.