Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling image link in focus

I'm trying to improve accessibility on a WordPress site where 'focus' is missing from the stylesheet.

I understand how to style text links, but I can't seem to apply the same to image links. I'd like to put a dashed line beneath images when in focus.

Here's what I've put - pretty simple:

a img:hover {border-bottom: 3px dashed #000;}
a img:focus {border-bottom: 3px dashed #000;}

It works for hover, but not focus!

Any ideas?

like image 422
Beth Avatar asked May 09 '26 04:05

Beth


2 Answers

Just move the pseudo-classes to the a element:

a:hover img {border-bottom: 3px dashed #000;}
a:focus img {border-bottom: 3px dashed #000;}
like image 95
Brett Donald Avatar answered May 12 '26 00:05

Brett Donald


User agents generally do not consider img elements as accepting focus. From the w3 CSS spec:

The :focus pseudo-class applies while an element has the focus (accepts keyboard events or other forms of text input).

You can enable focus on img elements by adding a tabindex attribute (<img src="foo.png" tabindex="0" alt="photo of foo">) but I wouldn't recommend it unless you have some specific reason for doing so. Unless the element actually accepts keyboard input adding it to the tabbing order adds no benefit for AT users (and indeed, may be confusing).

like image 25
steveax Avatar answered May 12 '26 02:05

steveax