惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
F
Fortinet All Blogs
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
罗磊的独立博客
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
U
Unit 42
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
G
Google Developers Blog
H
Help Net Security

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
CSS Color Contrast: The WCAG Rules Every Developer Should...
Snappy Tools · 2026-05-11 · via DEV Community

Snappy Tools

Color contrast is one of the most commonly overlooked accessibility requirements in web development — and one of the easiest to fail accidentally. You pick a beautiful grey text on a white background, it looks fine on your calibrated monitor, and then someone with low vision or a washed-out phone screen can't read it at all.

This post covers how contrast ratios work, what WCAG requires, and how to check your colors before they ship.

What Is a Contrast Ratio?

The contrast ratio between two colors is a number from 1:1 (no contrast — same color) to 21:1 (maximum contrast — black on white). It is calculated from the relative luminance of each color, which is a perceptual measure of how bright a color appears to the human eye.

The formula is:

contrast ratio = (L1 + 0.05) / (L2 + 0.05)

Enter fullscreen mode Exit fullscreen mode

where L1 is the luminance of the lighter color and L2 is the luminance of the darker one. Luminance itself is computed from the RGB values after gamma-correcting them (which is why the math is not as simple as just comparing hex codes).

Fortunately, you do not need to calculate this by hand. SnappyTools has a free Color Contrast Checker that computes the ratio instantly and shows you exactly which WCAG levels you pass.

WCAG 2.1 Requirements

The Web Content Accessibility Guidelines define three conformance levels — A, AA, and AAA — and contrast requirements that differ by text size:

Element AA minimum AAA enhanced
Normal text (< 18pt or < 14pt bold) 4.5:1 7:1
Large text (≥ 18pt or ≥ 14pt bold) 3:1 4.5:1
UI components and graphics 3:1

Level AA is the legal baseline in most jurisdictions (it's what the ADA, EN 301 549, and WCAG 2.1 compliance frameworks reference). Level AAA is aspirational — aim for it on body text where possible.

Common Failures (And How to Spot Them)

Light grey on white

/* Fails AA — ratio ~4.1:1 */
color: #767676;
background: #ffffff;

Enter fullscreen mode Exit fullscreen mode

The classic mistake. #767676 on white is almost exactly at the AA threshold and fails it narrowly. Use #595959 or darker for reliable compliance.

Brand colors with white text

Bright brand colors frequently fail:

  • #FF6B6B (coral) on white: ~3.0:1 — fails AA for normal text
  • #FFD700 (gold) on white: ~1.7:1 — fails everything

If your brand color fails contrast on white, either darken the shade for UI text or use dark text on the brand color instead.

Placeholder text

CSS ::placeholder styling inherits from the input but is typically rendered at reduced opacity. The effective contrast of opacity: 0.5 on a #555 placeholder over white is much lower than #555 measured directly. Check your placeholder styling explicitly.

Link underline color

When text-decoration-color is set separately from color, both need adequate contrast. A common pattern that fails: grey underline color on a white background used to subtly de-emphasise links.

Checking Contrast in Your Workflow

There are three points where it's worth checking:

1. During design — before a pixel is written to CSS. Design tools like Figma have contrast plugins (Stark, Contrast) that flag issues while you're still moving colors around.

2. During development — when you translate the design to code. Use a browser-based tool so you can quickly iterate on hex values. The Color Contrast Checker at SnappyTools lets you enter hex codes or use a color picker and shows the ratio and pass/fail for all WCAG levels at once.

3. During audit — before shipping. Chrome DevTools has a contrast ratio indicator in the color picker (accessible when inspecting an element). axe DevTools and Lighthouse both flag contrast failures automatically.

Quick Reference: Ratios That Always Pass

Ratio Passes
≥ 7:1 AAA normal text, AAA large text
≥ 4.5:1 AA normal text, AAA large text
≥ 3:1 AA large text, AA UI components
< 3:1 Fails everything

Text Over Images and Gradients

WCAG contrast rules assume a flat background. When text sits over an image or gradient, you need to ensure the minimum contrast is met at the worst-case point in the image.

Common solutions:

  • Text shadowtext-shadow: 0 1px 4px rgba(0,0,0,0.8) works but can look harsh
  • Semi-transparent scrimbackground: linear-gradient(transparent, rgba(0,0,0,0.6)) from the bottom up
  • Solid background strip — old-fashioned but reliable

None of these are checkable by automated tools — you need a human eye (or a per-pixel luminance calculation) to verify.

One Last Thing: Colour Is Not the Only Signal

WCAG 1.4.1 says you cannot convey information using colour alone. A red/green "error/success" indicator needs a second signal (icon, label, border shape) to be accessible to colour-blind users. This is separate from the contrast ratio — a fully accessible red error state has high contrast and an error icon.


Color contrast takes about 30 seconds to check per color pair. Making it part of your review workflow is one of the highest-ROI accessibility habits you can build.

Use the free Color Contrast Checker — no signup, runs in your browser, shows WCAG AA and AAA results instantly.