Skip to content

Technique SCR31:Using script to change the background color or border of the element with focus

About this Technique

This technique is Sufficient to meet 2.4.7: Focus Visible.


HTML CSS, script

This technique relates to 2.4.7: Focus Visible (Sufficient).


This purpose of this technique is to allow the author to use JavaScript to apply CSS, in order to make the focus indicator more visible than it would ordinarily be. When an element receives focus, the background color or border is changed to make it visually distinct. When the element loses focus, it returns to its normal styling. This technique can be used on any HTML user agent that supports Script and CSS, regardless of whether it supports the :focus pseudo class.


Example 1

In this example, when the link receives focus, its background turns yellow. When it loses focus, the yellow is removed. Note that if the link had a background color to begin with, you would use that color rather than "" in the script.

<script>function toggleFocus(el) { ="yellow" ? "inherit" : "yellow";
<a href="example.html" 
 onfocus="toggleFocus(this)" onblur="toggleFocus(this)">focus me</a>



  1. Tab to each element in the page
  2. Check that the focus indicator is visible

Expected Results

  • Step #2 is true
Back to Top