0

This is my code

<a href="https://www.w3schools.com">
    <div style="width:200px; border:1px solid black;">
        Visit W3Schools.com!
    </div>
</a>

My div width is 200PX. But my link is activated right side of the white space. How to activate link only within the div width, ie within 200px?

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Asif Iqbal
  • 1,132
  • 1
  • 10
  • 23
  • 3
    related: https://stackoverflow.com/q/1827965/8620333 – Temani Afif Aug 13 '22 at 08:20
  • if you want to have accurate detail about what is happening and why it covers the whole width, check this: https://stackoverflow.com/a/758491/8620333 and here is a demo to show you that it's more complex than what you might think: https://jsfiddle.net/L8u3eghf/ – Temani Afif Aug 13 '22 at 08:38

3 Answers3

2

Depending on what you're trying to do:

<a style="display:inline-block;" href="https://www.w3schools.com"><div style="width:200px; border:1px solid black;">Visit W3Schools.com!</div></a>

Or

<a style="display:block; width:fit-content;" href="https://www.w3schools.com"><div style="width:200px; border:1px solid black;">Visit W3Schools.com!</div></a>
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
-2

Instead of nesting the div in the a- element, make it its parent.

<div> <div style="width:200px; border:1px solid black;">
<a href="https://www.w3schools.com">Visit W3Schools.com!</a>
</div>

Have fun coding!

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 17 '22 at 16:59
-2

Try This:

<div style="width:200px; border:1px solid black;">
    <a style="display:flex; width: 100%;" href="https://www.w3schools.com">
        Visit W3Schools.com!
    </a>
</div>