1

how to access img tag inside a href tag to set focus to <img> tag?

eg:

<a href ='#' ><img class="img1" src="abc.png"/> </a>

The a .img1:focus {} didn't work. Not able to access <img> inside an <a href></a> tag

If I add class to tag, I can add focus to tag but tag & tag are of diff size & causing issue. eg: , then a .test:focus{} is working, But I need focus for tag

Maria
  • 11
  • 2

3 Answers3

1

You need a space between a and .img1 ( the way you wrote it means "a tag with img1 class" ) this way :

a .img1:focus {}

This means "element with img1 class inside a tag"

EDIT : in the link @jdv provided in the comments of your initial answer, don't focus (pun unintended) on the accepted answer, but on the second one. You can fix your problem by adding a tabindex property to your img tag. Like this :

<a href ='#' ><img class="img1" src="abc.png" tabindex="0"/> </a>
Aurelien
  • 1,497
  • 7
  • 15
0

try out this code

<a href ='#' id='link'><img class="img1" src="abc.png"/> </a>

a#link .img1:focus{}
Bagzie
  • 482
  • 5
  • 7
0

focus is often associated with form elements like input field I advise you use hover like this instead

a img:hover{
  width: 500px;
}
<a><img class="img1" src="http://via.placeholder.com/350x150"/></a>
Friday Ameh
  • 1,664
  • 1
  • 10
  • 15