24 ways

to impress your friends

CSS for Accessibility by Ann McMeekin

CSS is magical stuff. In the right hands, it can transform the plainest of (well-structured) documents into a visual feast. But it’s not all fur coat and nae knickers (as my granny used to say). Here are some simple ways you can use CSS to improve the usability and accessibility of your site.

Even better, no sexy visuals will be harmed by the use of these techniques. Promise.

Nae knickers

This is less of an accessibility tip, and more of a reminder to check that you’ve got your body background colour specified.

If you’re sitting there wondering why I’m mentioning this, because it’s a really basic thing, then you might be as surprised as I was to discover that from a sample of over 200 sites checked last year, 35% of UK local authority websites were missing their body background colour.

Forgetting to specify your body background colour can lead to embarrassing gaps in coverage, which are not only unsightly, but can prevent your users reading the text on your site if they use a different operating system colour scheme.

All it needs is the following line to be added to your CSS file:

body {background-color: #fff;}

If you pair it with

color: #000;

… you’ll be assured of maintaining contrast for any areas you inadvertently forget to specify, no matter what colour scheme your user needs or prefers.

Even better, if you’ve got standard reset CSS you use, make sure that default colours for background and text are specified in it, so you’ll never be caught with your pants down. At the very least, you’ll have a white background and black text that’ll prompt you to change them to your chosen colours.

Elbow room

Paying attention to your typography is important, but it’s not just about making it look nice.

Careful use of the line-height property can make your text more readable, which helps everyone, but is particularly helpful for those with dyslexia, who use screen magnification or simply find it uncomfortable to read lots of text online.

When lines of text are too close together, it can cause the eye to skip down lines when reading, making it difficult to keep track of what you’re reading across.

So, a bit of room is good.

That said, when lines of text are too far apart, it can be just as bad, because the eye has to jump to find the next line. That not only breaks up the reading rhythm, but can make it much more difficult for those using Screen Magnification (especially at high levels of magnification) to find the beginning of the next line which follows on from the end of the line they’ve just read.

Using a line height of between 1.2 and 1.6 times normal can improve readability, and using unit-less line heights help take care of any pesky browser calculation problems.

For example:

  1. p {
  2. font-family: "Lucida Grande", Lucida, Verdana, Helvetica, sans-serif;
  3. font-size: 1em;
  4. line-height: 1.3;
  5. }
  6. Source: /code/css-for-accessibility/1.txt

or if you want to use the shorthand version:

  1. p {
  2. font: 1em/1.3 "Lucida Grande", Lucida, Verdana, Helvetica, sans-serif;
  3. }
  4. Source: /code/css-for-accessibility/2.txt

View some examples of different line-heights, based on default text size of 100%/1em.

Further reading on Unitless line-heights from Eric Meyer.

Transformers: Initial case in disguise

Nobody wants to shout at their users, but there are some occasions when you might legitimately want to use uppercase on your site.

Avoid screen-reader pronunciation weirdness (where, for example, CONTACT US would be read out as Contact U S, which is not the same thing – unless you really are offering your users the chance to contact the United States) caused by using uppercase by using title case for your text and using the often neglected text-transform property to fake uppercase.

For example:

  1. .uppercase {
  2. text-transform: uppercase
  3. }
  4. Source: /code/css-for-accessibility/3.txt

Don’t overdo it though, as uppercase text is harder to read than normal text, not to mention the whole SHOUTING thing.

Linky love

When it comes to accessibility, keyboard only users (which includes those who use voice recognition software) who can see just fine are often forgotten about in favour of screen reader users.

This Christmas, share the accessibility love and light up those links so all of your users can easily find their way around your site.

The link outline

AKA: the focus ring, or that dotted box that goes around links to show users where they are on the site.

The techniques below are intended to supplement this, not take the place of it. You may think it’s ugly and want to get rid of it, especially since you’re going to the effort of tarting up your links.

Don’t.

Just don’t.

The non-underlined underline

If you listen to Jacob Nielsen, every link on your site should be underlined so users know it’s a link.

You might disagree with him on this (I know I do), but if you are choosing to go with underlined links, in whatever state, then remove the default underline and replacing it with a border that’s a couple of pixels away from the text.

The underline is still there, but it’s no longer cutting off the bottom of letters with descenders (e.g., g and y) which makes it easier to read.

This is illustrated in Examples 1 and 2.

You can modify the three lines of code below to suit your own colour and border style preferences, and add it to whichever link state you like.

  1. text-decoration: none;
  2. border-bottom: 1px #000 solid;
  3. padding-bottom: 2px;
  4. Source: /code/css-for-accessibility/4.txt

Standing out from the crowd

Whatever way you choose to do it, you should be making sure your links stand out from the crowd of normal text which surrounds them when in their default state, and especially in their hover or focus states.

A good way of doing this is to reverse the colours when on hover or focus.

Well-focused

Everyone knows that you can use the :hover pseudo class to change the look of a link when you mouse over it, but, somewhat ironically, people who can see and use a mouse are the group who least need this extra visual clue, since the cursor handily (sorry) changes from an arrow to a hand.

So spare a thought for the non-pointing device users that visit your site and take the time to duplicate that hover look by using the :focus pseudo class.

Of course, the internets being what they are, it’s not quite that simple, and predictably, Internet Explorer is the culprit once more with it’s frustrating lack of support for :focus. Instead it applies the :active pseudo class whenever an anchor has focus.

What this means in practice is that if you want to make your links change on focus as well as on hover, you need to specify focus, hover and active.

Even better, since the look and feel necessarily has to be the same for the last three states, you can combine them into one rule.

So if you wanted to do a simple reverse of colours for a link, and put it together with the non-underline underlines from before, the code might look like this:

  1. a:link {
  2. background: #fff;
  3. color: #000;
  4. font-weight: bold;
  5. text-decoration: none;
  6. border-bottom: 1px #000 solid;
  7. padding-bottom: 2px;
  8. }
  9.  
  10. a:visited {
  11. background: #fff;
  12. color: #800080;
  13. font-weight: bold;
  14. text-decoration: none;
  15. border-bottom: 1px #000 solid;
  16. padding-bottom: 2px;
  17. }
  18.  
  19. a:focus, a:hover, a:active {
  20. background: #000;
  21. color: #fff;
  22. font-weight: bold;
  23. text-decoration: none;
  24. border-bottom: 1px #000 solid;
  25. padding-bottom: 2px;
  26. }
  27. Source: /code/css-for-accessibility/5.txt

Example 3 shows what this looks like in practice.

Location, Location, Location

To take this example to it’s natural conclusion, you can add an id of current (or something similar) in appropriate places in your navigation, specify a full set of link styles for current, and have a navigation which, at a glance, lets users know which page or section they’re currently in.

Example navigation using location indicators.

and the source code

Conclusion

All the examples here are intended to illustrate the concepts, and should not be taken as the absolute best way to format links or style navigation bars – that’s up to you and whatever visual design you’re using at the time.

They’re also not the only things you should be doing to make your site accessible.

Above all, remember that accessibility is for life, not just for Christmas.

About the author

Ann McMeekin Ann McMeekin is passionate about accessibility and good design, whether on the web or in the real world, and doesn't believe that one has to be sacrificed to achieve the other. This is something she's argued for several years, both in her work (currently as a Web Accessibility Consultant for the RNIB, and previously as a web designer) and to anyone who'll sit still long enough to listen. She blogs for herself at http://www.pixeldiva.co.uk and for work at http://www.rnib.org.uk/wacblog.

Responses

Got something to add? Post to your own site and tag it 24ways07 and annmcmeekin to be included here.

Your comments

  1. § Richard Rutter:

    if you are choosing to go with underlined links, in whatever state, then remove the default underline and replacing it with a border that’s a couple of pixels away from the text.

    That’s good advice, although on some occasions IE6 can choke on links with borders (it’s a form of the Peekaboo bug). Worth bearing in mind if things start going wrong in your cross-browser testing.

  2. § pauldwaite:

    The border styles for replacing underlined links could be re-written like this:

    border-bottom-style: solid;
    border-width: 1px;

    Then the border would pick up whatever colour’s already being used for links, without having to duplicate that value.

    you can add an id of current (or something similar) in appropriate places in your navigation

    I’ve used a strong or em element instead, so that there’s still some indication of the current page for screen readers and non-CSS folks..

  3. § pixeldiva:

    Paul: it’s interesting that you’re using the strong or em element, but unfortunately, while it’ll do the job for non-CSS folks, screen reader users probably won’t get the benefit. I’m not aware of any screen reader which by default vocalises strong or em elements, and the main ones don’t even include options to turn that vocalisation on.

    Screen reader users will get an indication of the current page from the usual things (e.g., page titles and headings) anyway, so they’re not particularly losing out from not getting the extra visual information that this technique offers.

  4. § John Sutherland:

    Great article!

    As pauldwaite rightly says you can miss off the colour value from the border as it defaults to the text colout, but you can still use the shortcut version: border: 1px solid; works as you might expect.

  5. § Michael Niggel:

    Thank you for mentioning to specify a background image. I use a non-white default background color, and the number of sites that assume I don’t is enormous. It looks quite unprofessional every time I come across one. Along the same thread, if you want your links to be blue, you need to specify that color as well. If you specify any one of text, background, or link colors, (or background image,) you need to specify all the others.

  6. § Kilian Valkhof:

    Thank you for your very nice article!

    I wasn’t aware of IE’s problem with :focus (but you can use a keyboard with it? impressive!) but now that I know, :active is getting added! Though I wonder why you advice against removing the focus outline (or rather, replacing it with another visual cue) Aren’t we past the “all links should be blue and underlined” way of thinking by now?

  7. § Mike Holloway:

    I like your article :)
    pixeldiva: You pointed out that paul’s strong/em wouldn’t be read out, but I’m interested to know if your idea (using id’s) are read out in screen readers, as in my experience they aren’t either.

  8. § pixeldiva:

    Michael: You’re not the only one with a non-white default background colour :). It’s quite startling how many sites forget it, which is why I mentioned it.

    Kilian: The reason to leave the focus outline is that it adds an extra element rather than just colour, in the event that someone is only distinguishing links with colour rather than underline or whatever. It can also help distinguish links even when extra effort has gone in to highlight the links, and is what most users expect to be displayed as they tab around the page. I’m not suggesting all links should be blue and underlined, but given that this is there to help users and it’s not heinously ugly, there’s no real reason to remove it.

    Mike: you’re right in that screen readers won’t read out the id’s, but this technique is not aimed at screen reader users – it’s intended to give users can see a bit of a boost. Screen reader users aren’t being specifically disadvantaged by not getting this information, as I mentioned in my comment above.

    My intention with this article was to give some tips that are predominantly intended to help those who can see the page and who are often forgotten about because it’s easy to focus on those who entirely can’t see the page.

  9. § J-P Stacey:

    Really interesting. It’s good to hear what a lot of people think are specialist ideas about accessibility getting an airing.

    From past experience if you start with the principles of accessible CSS then you’re less likely to write unmaintainable CSS, or CSS where one specifier suddenly conflicts with another and you can’t work out why. Always pairing background and foreground colours in the same pair of braces is a must for clarity (even if it’s background-color:inherit), for example.

    (I think Yahoo!‘s login page still has the no-background CSS bug. Tsk tsk.)

  10. § Richard Morton:

    This is very interesting, and I must admit that I haven’t before considered moving underlines to make the text more readable. Something to play with in the New Year.

    I’ve used the same sort of technique for reversing of links with focus but I still find there are some problems with it. Even in your Example navigation using location indicators it doesn’t quite work (at least in IE6.0 – which I know is probably the worst one to use but many people still do). If you hover over one of the links and then tab through the links with the keyboard, you get the old dotted lines round the link back.

  11. § pixeldiva:

    Richard: I’m not sure what you mean by not quite working in relation to the dotted link surround – that’s supposed to be there. Can you clarify a bit?

  12. § Richard K:

    There is a good reason not to specify the background colour (or provide an option to change it instead): dyslexics sometimes have their background colour set to something other than white as it helps them read on screen. Bad for aesthetics for everyone else, but this is an article about accessibility…

  13. § Michael:

    Is there anywhere I can get ahold of some screen reading software that uses the focus attribute. I normally just check websites I build, which thankfully isn’t very often as my codings a bit naff, in emulation software. If I could download something more appropriate would be good and should hopefully allow me to start getting my coding skills more accessible.

Commenting is closed for this article.

24 ways: day 13