Articles
Published · November 18, 2026

Syntax Theme Contrast: What WCAG 4.5:1 Means for Code Colors

WCAG asks for 4.5:1 contrast on text, and code tokens count. Here is what the ratio measures, what is exempt, and how to check a syntax theme.

Also in: Español中文हिन्दी

Quick answer

WCAG's Contrast (Minimum) criterion asks for a contrast ratio of at least 4.5:1 between text and its background. Syntax-highlighted code is text, so each token color in a theme needs 4.5:1 against the editor background. Cursor, selection and focus colors fall under a separate 3:1 rule. Neither threshold is rounded: 4.499:1 fails.

How it actually works

The ratio comes from one formula in WCAG's Contrast (Minimum) page: (L1 + 0.05) / (L2 + 0.05). L1 is the relative luminance of the lighter color and L2 the relative luminance of the darker one. The result runs from 1:1 (identical colors) to 21:1 (black on white).

Because luminance weights green most heavily and blue least, two colors that look equally "light" to you can score very differently. That is why checking by eye fails so often. Take mid-gray on white:

#767676 on #ffffff  ->  4.54:1  (passes 4.5:1)
#777777 on #ffffff  ->  4.48:1  (fails 4.5:1)

You can't tell those two grays apart on screen, yet only one passes. The criterion says the ratios are thresholds and the computed values should not be rounded, so 4.48 does not become 4.5.

Which code colors count as text

All of them. Keywords, strings, numbers, function names and comments are text rendered on the editor background, and the criterion measures contrast against the background the text sits on. The usual weak spot is comments. Themes dim them on purpose so code stands out, and they often land below 4.5:1. The only text with no requirement is text in an inactive component, pure decoration, text that is not visible, and logotypes. A comment is none of those. The same page notes that low contrast chosen for brand or style reasons is not exempt.

This is also why the background color must be set along with the text color. If a theme specifies one without the other, the pair cannot be evaluated, and WCAG treats that as a failure.

Curious how a theme's colors get attached to tokens in the first place? See how syntax highlighting assigns colors to tokens.

The large-text exception rarely applies

Large-scale text needs only 3:1, but WCAG defines large as at least 18 point, or 14 point bold. That is roughly 24px or 18.5px. Code is usually set well below that. Assume 4.5:1 for everything in the editor.

AA and AAA

4.5:1 is the AA level. MDN's color contrast guide lists AAA (criterion 1.4.6) at 7:1 for body text and 4.5:1 for large text, and advises aiming for AAA but at least meeting AA. For long reading sessions, the extra headroom is worth having on your primary colors even when AA is your formal target.

When to use it (and when to skip it)

Non-text contrast for cursor, selection and focus

Non-text Contrast sets 3:1 against adjacent colors for the visual information needed to identify user interface components and their states, and for parts of graphics required to understand content. In an editor, that points at the things you use to find your place: a focus ring on a button, a visible selection, a cursor. The page notes that a default user-agent focus style is exempt, but a focus indicator the author modifies must meet 3:1.

Apply the rule by the purpose of the color. A line-highlight band that is only decoration is not required to hit 3:1. A selection color you rely on to see what you are about to delete is.

What to skip

Inactive controls, logotypes and pure decoration have no contrast requirement. Skip those. Spend the time on the pairs people actually read: comments on the background, strings on the background, and anything that appears next to a selection or highlight, since conformance is judged for color pairs that appear adjacent in normal use.

Checking a theme

  1. List the background and each token color from the theme.
  2. Run each pair through a checker. MDN points to WebAIM's Color Contrast Checker, and to Firefox's Accessibility inspector for checking colors in a page.
  3. Flag anything under 4.5:1, with comments first.
  4. Repeat for light and dark variants. A dark theme that passes is no guarantee the light one does.
  5. Recheck selection and cursor colors against 3:1.

A pass is a floor, not a verdict on comfort. If a color clears the threshold by a hair, try it on the screen you actually use.

How Carets fits in

Carets is a native editor for iPhone, iPad and Mac, built for Markdown, plain text and code with syntax highlighting. Contrast checks are easy to do on your own files: keep a short Markdown note of your theme's background and token hex values, with the ratio next to each, and the whole audit stays in one plain-text file you can open anywhere. Code blocks in that note, like the fenced code blocks above, get the same highlighting you are testing, so you read the result in the colors you are checking.

Carets keeps files, projects and tags in one place on both iOS and Mac, and your notes stay plain text, so a contrast log sits beside the code it describes. Get Carets on the App Store for iPhone, iPad and Mac.

Frequently Asked Questions

Do comments in code have to meet 4.5:1?

WCAG has no exemption for comments. They are text, and unless the text is inactive or purely decorative, 4.5:1 applies. Dimmed comments are the most common place a theme falls short.

Does large text lower the requirement for code?

Large-scale text needs 3:1, but that means at least 18 point, or 14 point bold. Code at normal editor sizes is below that, so 4.5:1 applies.

Is 4.49:1 close enough?

No. WCAG says the ratios are thresholds and must not be rounded, so 4.499:1 fails.

What is the ratio for AAA?

7:1 for body text and 4.5:1 for large text, per MDN's summary of success criterion 1.4.6.