th...">

How to get the <img> tag in focus

I have the code below on the html page:

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

the css page has the code below:

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

I also tried:

 img:focus{ outline: 2px solid blue; 

}

but not one of them seems to work, they should display a blue marker around the image when focusing. Does anyone know how to do this? thanks!!!

+9
html css
source share
3 answers

You cannot β€œfocus” an image if you do not have an interactive element or go to it using the tab. Try adding an interactive element on a div wrapper like this:

Html

 <a class="imageAnchor" href="#"> <img id="image_java" alt="image_not" src="http://www.w3schools.com/images/w3logotest2.png" /> </a> 

CSS

 .imageAnchor:focus img{ border: 2px solid blue; } 

http://jsfiddle.net/4x7wg7sb/1/

+7
source share

Actually, you can focus <img> - with tabindex :

 img:focus { outline: 2px solid blue; } 
 <img src="http://www.w3schools.com/images/w3logotest2.png" tabindex="0"> 
+23
source share

Try using the border property instead of the outline property. It should look like this:

 img:hover { border: 2px solid blue; } 

Edit: Use hovering instead of focus

-2
source share

All Articles