Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make elements that behave like anchor tags?

I was wondering, can you make an element that is not an anchor (<a>) tag (for ex. <div>, <span>, etc.) behave like an anchor tag? (Link to page on click).

Or, are there any other elements that behave the same?

like image 258
Maarten Wolfsen Avatar asked Oct 18 '25 11:10

Maarten Wolfsen


1 Answers

Only partially.

  • You can give any tag a tabindex to make it focusable.
  • You can give any tag a click event listener that sets location.href.
  • You can't make anything other than a real link react to :link and :visited in CSS

That leaves aside issues of getting (for example) screen readers to recognise the element as a link and annouce it to the user, and for search engines to recognize it as a link and follow it, and the JS simply failing.


In short: If you want a link, then use a real link. HTML is a semantic markup language. Use the semantics it comes with. Don't try to fake the behaviour that comes with the semantics using other elements.

like image 72
Quentin Avatar answered Oct 20 '25 03:10

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!