Reputation: 273452
I have a very long page that looks like this
<h2 id="chapter55">Chapter 55</chapter>
I link to it like this: page.html#chapter55
, and surprise, it jumps straight away to chapter 55.
That was a surprise because I didn't know you could anchor to any tag, I thought only <a...
tags could be anchored to.
Can I trust this to work? Is this a standard, or at least widely supported?
Upvotes: 1
Views: 1330
Reputation: 201528
Using the id
attribute on almost any element has been standard since HTML 4.0, which was approved in 1997. (HTML5 RC extends things a bit farther, allowing id
on literally any element.)
It has been supported by relevant browsers for a long time. Netscape 3 was probably the last browser that didn’t support the id
attribute.
The HTML5 RC even requires the use of id
when setting target anchors: it declares a name
as obsolete (though it will keep working in browsers).
Note that you can link even to an entire section (this may have some benefits especially if you want to highlight the destination in CSS using the :target
pseudo-class):
<div id="chapter55">
<h2>Some heading</h2>
Chapter content
</div>
Upvotes: 0
Reputation: 17035
Yes, since HTML 4 you can make almost every element an anchor by assigning an id, like this:
<h1><a name="heading">Heading</a></h1>
<a href="#heading">go to heading</a>
is equal too
<h1 id="heading">Heading</h1>
<a href="#heading">go to heading</a>
Upvotes: 0
Reputation: 4503
Yes this works with any tags, you can jump to any ID on the page like so: does html anchor work with any tag? This link jumps to the related sidebar ==>
You're most probably a little confused because you need an a
tag to link to the specified ID, but the ID itself can be on any tag you want. :)
<a href="#h-related">Jump to the related sidebar!</a>
Upvotes: 9