Should I make HTML Anchors with 'name' or 'id'?
According to the HTML 5 specification, 5.9.8 Navigating to a fragment identifier:
For HTML documents (and the text/html MIME type), the following processing model must be followed to determine what the indicated part of the document is.
- Parse the URL, and let fragid be the <fragment> component of the URL.
- If fragid is the empty string, then the indicated part of the document is the top of the document.
- If there is an element in the DOM that has an ID exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
- If there is an a element in the DOM that has a name attribute whose value is exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
- Otherwise, there is no indicated part of the document.
So, it will look for
id="foo", and then will follow to
Edit: As pointed out by @hsivonen, in HTML5 the
a element has no name attribute. However, the above rules still apply to other named elements.
HTML anchor link as name and not id
No, this is not possible. There are some work-arounds:
Obviously, you could add the
id property to the target element:
<h1 name="main" id="main">Main</h1>
2. Add an anchor
If the target needs to be identified by name, add an anchor:
But in HTML5 the
name attribute for the
a element is obsolete.
3. Use scripting to add the
You could add a script that will add the missing
id attribute on page load:
// find first element that has the name attribute set to "main":
var element = document.querySelector('[name=main]');
// copy that value into the id attribute
You should then put this script block at the end of the document, so just before the closing
4. Add script to the source link:
You could replace the
href value with a script that will do the action based on the
class="">skip to main content</a>
As you tagged the question with HTML5, it should be noted that in HTML5 there is a clear moving away from using the
name attribute as a target.
id attributes are preferred in HTML5. That makes sense, because
name values do not have to be unique, while
id values have to be.
does html anchor work with any tag?
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>
Link to a specific part of a web page doesn't work
you just need to give id to an anchor like for example
<a href="#sec" id="exploreBtn">Explore</a> on pressing this you will go to
sec id, which should be declared like this:
Should HTML anchors wrap the heading or stand on its own?
It can disrupt assistive technologies. In your example, when a screen reader navigates to the anchor, it has no deterministic way of knowing that it should read aloud the preceding paragraph.
Also, when you open a URL to
http://somedomain.com/somepath#anchor the browser will automatically scroll to the element that has
id=anchor, so it's important to put the anchor on the element that makes sense.
Any element can have an anchoring id, it does not need to be an
Adding a #href anchor to a webpage
If you inspect the HTML on that page you'll see how it's being done.
The # sign in an anchor tag's href will tell the browser to move to the element that has a matching ID (where ID = everything that comes after the # sign).
<a href="#MyLocation">My Link</a>
<div id="MyLocation">The anchor destination</div>
You can also include it at the end of your URL and it will take the user directly to the destination on page load.
Html - How to Scroll Vertical in Select Box
How to Click a Button on a Website Using Puppeteer Without Any Class, Id ,... Assigned to It
Img Src Tag Not Displaying Image
React: Action on Variable Change
How to Prevent VS Code from Breaking Up Long HTML Lines into Multiple Lines
Angular - Show Empty Row by Default in Drop Down List and Disable Button
Nested Ngfor Looping Over an Array of Objects
Parent Div Don't Wrap Child Div
Html Select Only One Checkbox in a Group
Two Images, Side by Side Responsive
Make First Column Fixed and Next Column Scrollable in HTML Table
Angular Detect When Scroll Reached Certain Point
Splitting the HTML Page Using Div
How to Remove Blue Highlight on Hover in Select, Option Inputs HTML for Chrome
Playing M3U8 Files With HTML Video Tag
How to Pass Boolean Variable of False in a Url
Css Negation Pseudo-Class :Not() For Parent/Ancestor Elements