On many websites I see links that have href=\"#\"
. What does it mean? What is it used for?
The problem with using href="#" for an empty link is that it will take you to the top of the page which may not be the desired action. To avoid this, for older browsers or non-HTML5 doctypes, use
<a href="javascript:void(0)">Goes Nowhere</a>
The main use of anchor tags - <a></a>
- is as hyperlinks. That basically means that they take you somewhere. Hyperlinks require the href
property, because it specifies a location.
A hash - #
within a hyperlink specifies an html element id to which the window should be scrolled.
href="#some-id"
would scroll to an element on the current page such as <div id="some-id">
.
href="//site.com/#some-id"
would go to site.com
and scroll to the id on that page.
href="#"
doesn't specify an id name, but does have a corresponding location - the top of the page. Clicking an anchor with href="#"
will move the scroll position to the top.
See this demo.
This is the expected behavior according to the w3 documentation.
An example where a hyperlink placeholder makes sense is within template previews. On single page demos for templates, I have often seen <a href="#">
so that the anchor tag is a hyperlink, but doesn't go anywhere. Why not leave the href
property blank? A blank href
property is actually a hyperlink to the current page. In other words, it will cause a page refresh. As I discussed, href="#"
is also a hyperlink, and causes scrolling. Therefore, the best solution for hyperlink placeholders is actually href="#!"
The idea here is that there hopefully isn't an element on the page with id="!"
(who does that!?) and the hyperlink therefore refers to nothing - so nothing happens.
Another question that you may be wondering is, "Why not just leave the href property off?". A common response I've heard is that the href
property is required, so it "should" be present on anchors. This is FALSE! The href
property is required only for an anchor to actually be a hyperlink! Read this from w3. So, why not just leave it off for placeholders? Browsers render default styles for elements and will change the default style of an anchor tag that doesn't have the href property. Instead, it will be considered like regular text. It even changes the browser's behavior regarding the element. The status bar (bottom of the screen) will not be displayed when hovering on an anchor without the href property. It is best to use a placeholder href value on an anchor to ensure it is treated as a hyperlink.
See this demo demonstrating style and behavior differences.
The href attribute defines the URL of the resource of a link. If the anchor tag does not have href tag then it will not become hyperlink. The href attribute have the following values:
1. Absolute path: move to another site like href="http://www.google.com"
2. Relative path: move to another page within the site like herf ="defaultpage.aspx"
3. Move to an element with a specified id within the page like href="#bottom"
4. href="javascript:void(0)", it does not move anywhere.
5. href="#" , it does not move anywhere but scroll on the top of the current page.
6. href= "" , it will load the current page but some browsers causes forbidden errors.
Note: When we do not need to specified any url inside a anchor tag then use
<a href="javascript:void(0)">Test1</a>