2

Question

What accessibility downsides are there to set outline to none and instead use box-shadow to highlight an active input? Does it violate WCAG at all?

Background

User agent style sheets highlight an active element's outline on focus. To customize this highlighting we can use the :focus selector. This works well for rectangular shaped input elements, but doesn't look good on rounded inputs. A mismatching square is displayed. See below example where the red outline is square even though the input has rounded corners.

Red, square outline around input box with rounded corners

As outline is rectangular, to better match a rounded element, an alternative way to style is to replace outline with box-shadow. This require setting outline: none, which seems controversial from an accessibility perspective.

Example

input {
  border-radius: 999em;
  padding: 10px;
}

.outline:focus {
  outline: solid 15px red;
}

.box-shadow:focus {
  outline: none;
  box-shadow: 0 0 0 15px red;
}
<label for="with-outline">Outline</label>
<input name="with-outline" id="with-outline" class="outline">
<hr>
<label for="with-box-shadow">Box Shadow</label>
<input name="with-box-shadow" id="with-box-shadow" class="box-shadow">
TylerH
  • 20,799
  • 66
  • 75
  • 101
Jouni Kantola
  • 1,097
  • 7
  • 12
  • So what is your question? – Sato Takeru Jan 11 '21 at 09:00
  • Since the focus is clearly showing something then it's fine (it doesn't really matter if it's rectangular or circular) – Temani Afif Jan 11 '21 at 09:02
  • @George: Thank you for pointing out that the question was unclear. Restructured the whole post. – Jouni Kantola Jan 11 '21 at 11:29
  • I'm hoping the topic is clearer now after an edit. The intention was to discuss box-shadow vs outline. Whole web sites are made to explain how cancelling outline is an antipattern; http://www.outlinenone.com/. – Jouni Kantola Jan 11 '21 at 11:42
  • 1
    I've further edited this question and title to specify an objective specification for accessibility. Without such a metric, the question would ultimately be opinion-based. – TylerH Jan 11 '21 at 15:19

1 Answers1

3

Short Answer

No issues with using box-shadow or outline if you don't need to support IE8. There are a few considerations such as colour contrast but technically your current example passes current guidance (although I would encourage you to increase the colour contrast slightly).

Longer Answer

The main thing you need to consider is colour contrast. As long as the border is a 3:1 contrast ratio with the background then under current guidance you are OK.

However WCAG 2.2 comes into effect from June / July 2021 so you may as well prepare for that.

WCAG 2.2

As long as you have sufficient contrast and it is thick enough to comply with WCAG 2.2 (so you don't have to redo stuff when that comes into effect) you will be fine. The following is a summary of the upcoming rule changes (which the example you gave passes):

Change of contrast: The colour change for the focus indication area has a contrast ratio of at least 3:1 with the colours of the unfocused state.

The following is IN ADDITION to the above:

Contrast or thickness: The focus indication area has a 3:1 contrast ratio against all adjacent colours for the minimum area or greater, or has a thickness of at least 2 CSS pixels.

The focus needs a minimum 3:1 change and also needs to be:

  • either 2px thick and/or
  • 3:1 against ALL adjacent colors.

For WCAG 2.2 the following rules apply:

  • Minimum area: The focus indicator area is either:
    • at least as large as the area of a 1 CSS pixel thick perimeter of the unfocused component;
    • at least as large as a 4 CSS pixels border along the shortest side of the unfocused component, and no thinner than 2 CSS pixels.

Obviously WCAG 2.2 is just a draft but I think the principles are pretty close to final for borders and focus indicators.

You can see the WCAG 2.2+ understanding doc for contrast (minimum) here

Internet Explorer(IE) compatibility

The only other thing to consider is compatibility - if you still support IE8 you cannot use box-shadow or outline - and quite a few screen reader users are still using IE8 due to software compatibility.

However I would advise a different basic stylesheet for IE8 anyway as otherwise you are stuck in the dark ages! I personally only support back to IE9 as that is painful enough!

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
  • Thank you for the thorough answer! In essence, I got the answer I needed, that box-shadow is fine to use as an alternative to outline. I tried to keep the example simple, but I'll adjust it to the comments you made regarding contrast. The first version used colors I thought wouldn't take focus away from the outline vs box-shadow topic. – Jouni Kantola Jan 11 '21 at 10:49
  • 1
    FYI That is not the standard for SILVER, that is the standard for WCAG 2.x, and this is not linking to the correct document. Silver/WCAG 3.0 is completely different in terms of contrast. – Myndex Feb 27 '21 at 23:27
  • 1
    Rather than post a new answer, I edited this one for correctness. FUTURE: WCAG **3.0** (aka Silver) is taking an entirely different approach, and nothing in this answer is expected to apply. However, WCAG 2.2 will be valid even after Silver is the recommendation. – Myndex Feb 28 '21 at 00:48
  • 1
    I will edit my answer soon, unfortunately somebody has edited the original question so nothing in here makes sense & your edits do not improve that, but you are indeed correct I was obviously sleepy when I wrote this, I have no idea why I was talking about WCAG SILVER. And you are also correct, once again WCAG guidance is unclear as I will not be the only one considering contrast with a border as sufficient under "A focus indicator that is larger than the minimum area may have parts that do not meet the 3:1 contrast ratio, as long as an area equal to the minimum does meet the contrast ratio." – GrahamTheDev Feb 28 '21 at 10:58
  • 1
    Thanks for taking the time for pointing out those massive mistakes! – GrahamTheDev Feb 28 '21 at 10:59
  • 1
    Hi Graham, Yes the 3:1 & 2px thing has been a point of discussion as in if we need to re-write it... I'm working only on Silver/WCAG 3.0, and not much involved in 2.2, so I'm not sure the current status (I want to say I saw a solution in survey, but I might be thinking of something else). THAT SAID, since WCAG 2.x contrast is generally wrong, the 3:1 for this use case isn't necessarily ideal — the new contrast method for 3.0/Silver is going to specify and deal with these kinds of issues a little differently (in development). Cheers... – Myndex Feb 28 '21 at 16:45
  • One thing that would be great (if you have influence and input on 3.0) is to have a better way of getting feedback on drafts so that wording ambiguity can be caught early. For example [this answer I gave](https://stackoverflow.com/a/64435051/2702894) was very "tongue-in-cheek" as I know that an input should have a visible label and not a placeholder but it just shows how difficult it is to give a definitive answer and how the guidance is not clear (if there is a piece of guidance that makes it definitive I would love to see it). Is there any way I can get involved as would love to contribute? – GrahamTheDev Feb 28 '21 at 19:33
  • I have fixed the answer I believe, using the key parts from your suggestions, let me know if you still think there are errors etc. – GrahamTheDev Mar 01 '21 at 06:49
  • 1
    Hi Graham, I am one of the authors of 3.0, so yes. But you are welcome to interact directly, and in fact I encourage you to do so. The means is via posting an issue in the appropriate GitHub repository. For 2.2 use: https://github.com/w3c/wcag/issues and for 3.0/Silver use: https://github.com/w3c/silver/issues WCAG & W3 is about public involvement. If you see a problem, say so! – Myndex Mar 05 '21 at 05:07