{"id":303361,"date":"2020-02-19T08:36:00","date_gmt":"2020-02-19T15:36:00","guid":{"rendered":"https:\/\/css-tricks.com\/?p=303361"},"modified":"2020-03-04T07:53:16","modified_gmt":"2020-03-04T14:53:16","slug":"understanding-web-accessibility-color-contrast-guidelines-and-ratios","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/understanding-web-accessibility-color-contrast-guidelines-and-ratios\/","title":{"rendered":"Understanding Web Accessibility Color Contrast Guidelines and Ratios"},"content":{"rendered":"\n
What should you do when you get a complaint about the color contrast in your web design? It might seem perfectly fine to you because you\u2019re able to read content throughout the site, but to someone else, it might be a totally different experience. How can put yourself in that person\u2019s shoes to improve their experience?<\/p>\n\n\n\n
There are some relatively easy ways to test contrast. For example, you can check the site on your phone or tablet in bright sunlight (not reliable*<\/a>), or add a CSS filter to mimic a grayscale view<\/a>). But\u2026 you don\u2019t have to trust your eyes.<\/strong> Not everyone has your exact eyes anyway, so your subjective opinion can possibly be a faulty measurement. <\/p>\n\n\n\n You can mathematically<\/strong> know if two colors have enough contrast between them. <\/p>\n\n\n\n\n\n\n\n The W3C has a document called Web Content Accessibility Guidelines (WCAG) 2.1 that covers successful contrast guidelines<\/a>. Before we get to the math, we need to know what contrast ratio scores we are aiming to meet or exceed. To get a passing grade (AA), the contrast ratio is 4.5:1 for most body text and 3:1 for larger text. <\/p>\n\n\n The guidelines were created for anyone using a standard browser, with no additional assistive technology. The contrast ratios that the WCAG suggests were based initially on earlier contrast standards and adjusted to accommodate newer display technologies, like antialiased text, so content would be readable by people with a variety of visual or cognitive difficulties, whether it be due to age, sickness, or other losses of visual acuity. <\/p>\n\n\n\n We\u2019re basically aiming to make text readable for someone with 20\/40 vision, which is equivilent to the vision of someone 80 years old. Visual acuity of 20\/40<\/a> means you can only read something at 20 feet away that someone with perfect 20\/20 vision could read if it was 40 feet away.<\/p>\n\n\n\n So, say your design calls for antialiased text because it looks much smoother on a screen. It actually sacrifices a bit of contrast and ding your ratio. The WCAG goes into more detail on how scoring works<\/a>.<\/p>\n\n\n\n There are other standards that take contrast in consideration, and the WCAG used some of these considerations to develop their scoring. One is called the Human Factors Engineering of Computer Workstations<\/em><\/a> (ANSI\/HFES 100-2007) was published in 2007 and designated as an American standard for ergonomics. It combined and replaced two earlier standards that were created by separate committees. The goal of the combined standard was to accommodate 90% of computer users, and cover many aspects of computer use and ergonomics, including visual displays and contrast. So, that means we have physical screens to consider in our designs.<\/p>\n\n\n The contrast ratio explains the difference between the lightest color brightness and the darkest color brightness in a given range. It\u2019s the relative luminance<\/a> of each color.<\/p>\n\n\n\n Let\u2019s start with an egregious example of a teal color text on a light gray background. <\/p>\n\n\n\n It\u2019s worth calling out that some tools, like WordPress, provide a helpful warning for this when there\u2019s a poorly contrasted text and background combination. In the case of WordPress, a you get notice in the sidebar.<\/p>\n\n\n\n \u201cOK,\u201d you say. \u201cPerhaps you think that teal on gray color combination is not exactly great, but I can still make out what the content says.\u201c (I\u2019m glad one of us can because it\u2019s pretty much a muddy gray mess to me.)<\/p>\n\n\n\n The contrast ratio for that fine piece of hypertext is 1.47:1.<\/strong> <\/p>\n\n\n\n I wanted a better understanding of what the contrast scores were actually checking and came to find that it requires the use of mathematics\u2026<\/em> with a side of understanding the differences between human and computer vision. This journey taught me about the history of computer vision and a bit about biology<\/a>, and gave me a small review of some math concepts<\/a> I haven\u2019t touched since college.<\/p>\n\n\n\n Here\u2019s the equation:<\/p>\n\n\n\n This seems simple, right? But first we need to determine the relative luminance for each color to get those variables.<\/p>\n\n\n We mentioned it in passing, but it\u2019s worth going deeper into relative luminance,<\/strong> or the relative brightness of any color expressed into a spectrum between 0 (black) and 1 (white).<\/p>\n\n\n\n To determine the relative luminance for each color, we first need to get the RGB notation for a color. Sometimes we\u2019re working with HEX color values and need to covert that over to RGB. There are online calculators that will do this for us, but there\u2019s solid math happening<\/a> in the background that makes it happen. Our teal hex color, Next, we take each value of the RGB color and divide each one by 255 (the max integer of RGB values) to get a linear value between 0 and 1. <\/p>\n\n\n\n So now with our teal color it looks like this:<\/p>\n\n\n\nHow did the W3C arrive at these ratios?<\/h3>\n\n\n
What does the ratio mean?<\/h3>\n\n\n
<h1>Title of Your Awesome Site<\/h1><\/code><\/pre>\n\n\n\n
h1 {\n background-color: #888888;\n color: #1ABC9C;\n}<\/code><\/pre>\n\n\n\n
(L1 + 0.05) \/ (L2 + 0.05)<\/code><\/pre>\n\n\n\n
L1<\/code> is the relative luminance of the lighter of the colors.<\/li>
L2<\/code> is the relative luminance of the darker of the colors.<\/li><\/ul>\n\n\n\n
OK, back to relative luminance<\/h3>\n\n\n
#1ABC9C<\/code>, becomes an RGB of
26, 188, 156<\/code>. <\/p>\n\n\n\n
Component<\/th> Equation<\/th> Value<\/th><\/tr><\/thead> Red<\/td> 26\/255<\/td> 0.10196078<\/td><\/tr> Green<\/td> 188\/255<\/td> 0.73725490<\/td><\/tr> Blue<\/td> 156\/255<\/td> 0.61176471<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n