HTML Anchors with 'name' or 'id'




Problems:

  • anchor tag in html - When one wants to refer some part of a webpage with the "http://wikitechy.com/#wikiTechy" method, which one of the following html code should use,

anchor tag in html with example :

<h1><a name="wikiTechy"/>wikiTechy Title</h1>
or

html class :

<h1 id="wikiTechy">wikiTechy Title</h1>
  • Both code works fine, but the problem is they are equal, or do they have semantic differences?

Solution 1:

  • You shouldn’t use anchor html tag <h1><a name="wikiTechy"/>wikiTechy Title</h1> in any HTML served as text/html, because the XML empty element syntax isn’t supported in text/html.
  • anchor link html - However, <h1><a name="wikiTechy">wikiTechy Title</a></h1> will work in HTML4 and it is not valid in HTML5 as currently drafted.
  • <h1 id="wikiTechy">wikiTechy Title</h1> is work in both HTML4 and HTML5. This won’t work in Netscape 4, but you’ll probably use a dozen other features that don’t work in Netscape 4.

Solution 2:

  • anchor tag html - if you are going to be linking to that area in the page such as page.html#wikiTechy and wikiTechy Title isn't a link you should be using:
<h1 id="wikiTechy">wikiTechy Title</h1>
  • If you instead put an anchor html code <a> reference around it you're headline will be influenced by an html anchor tag <a> specific CSS within your site.
  • html5 anchor is just extra markup, and you shouldn't need it.
  • It'd highly recommend to going with placing an id on the headline, not only is it better formed, but it will allow to either address that object in Javascript or CSS

Solutions 3:

html anchor link - Try the following code:

<h1 id="wikiTechy">wikiTechy Title</h1>
  • Don't use an anchor html unless you want a link.

Solution 4:

According to the HTML 5 specification
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.

  • 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 and 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 and stop the algorithm here.
  • Otherwise, there is no indicated part of the document.


Related Searches to HTML Anchors with 'name' or 'id'