Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a <img> tag the focus

Tags:

html

css

I have below code in html page:

<img id="image_java"  alt="image_not" src="images/java-icon.png">

in css page the below code:

#image_java: focus {
outline: 2px solid blue;
}

I have also tried:

img:focus{
outline: 2px solid blue;

}

but non of them seem to work, they are suppose to display a blue margin around the image when focus. Does any one know how to do that? thank you!!!

like image 352
MauricioTL Avatar asked Dec 06 '14 02:12

MauricioTL


People also ask

How do I focus an image in HTML?

Hover is for hovering and Focus is for focusing the clicked element.

How do you focus an image in CSS?

The :focus selector is used to select the element that has focus. Tip: The :focus selector is allowed on elements that accept keyboard events or other user inputs.

How do I target IMG tags?

You can target the img tag and not even worry about the div tag. These attribute selectors select by the "begins with". These select by "contains". Or you can select by the exact src.


2 Answers

Actually you CAN focus an <img> - with the help of tabindex:

img:focus {
    outline: 2px solid blue;
}
<img src="http://www.w3schools.com/images/w3logotest2.png" tabindex="0">
like image 69
Leo Avatar answered Oct 08 '22 06:10

Leo


If its an anchor tag you can also use

A:focus img{border: 2px solid blue;}

like image 45
Derek Moore Avatar answered Oct 08 '22 04:10

Derek Moore