108

I have inspected some sites and they have a pound(#) sign in the url. What does it do?

 <a href="#" >Link name</a>
0

7 Answers 7

115

It's a "fragment" or "named anchor". You can you use to link to part of a document. Typically when you link to a page, the browser opens it up at the top of the page. But you link to a section half-way down, you can use the fragment to link to that heading (or whatever).

If there is no <a name="whatever"/> tag within the page, then the browser will just link to the top of the page. If the fragment is empty, then it will also just link to the top of the page.

For a fragment only <a href="#">Link name</a>, then that's just a link to the top of the current page.

You often see that kind of link used in conjuction with javascript. Standards compliant HTML requires a href attribute, but if you're planning to handle the request with javascript then "#" serves as a reasonable place holder.

5
  • 1
    +1 Although the official term is a URL fragment, not a "hash reference": w3.org/TR/WD-html40-970708/htmlweb.html#h-4.1.1
    – Jason Hall
    May 10, 2010 at 3:49
  • Yeah, I changed it to "fragment" in my answer, cause that's what most people use. I never knew it had an "official" name though :) May 10, 2010 at 3:50
  • 2
    Why browsers treat it as 'go to top of page': Technically, you changed the page. After clicking the link, you'll notice that the # is actually added to the URL in the address bar, and if you click the back button it will remove it again. I wouldn't say it's a link to part of a document, more a link to a place inside the document. Otherwise, basically the same thing I was typing...
    – animuson
    May 10, 2010 at 3:51
  • A URI ending with # is permitted by the generic syntax and could be considered as a kind of empty fragment. In MIME document types such as text/html or any XML type, empty identifiers to match this syntactically legal construct are not permitted. Web browsers typically display the top of the document for an empty fragment. -- en.wikipedia.org/wiki/Fragment_identifier
    – IcyBrk
    Jun 27, 2017 at 14:31
  • Also by "If no such representation exists, then the semantics of the fragment are considered unknown and are effectively unconstrained. " From rfc3986(tools.ietf.org/html/rfc3986#page-24)
    – IcyBrk
    Jun 27, 2017 at 14:39
27

... just to add a few extra useful tips.

You can access and change it with document.location.hash in JavaScript.

It can point to a named anchor (e.g. <a name="top"></a>) or to an element with a corresponding id (e.g. <div id="top"></div>).

Seeing one on its own (e.g. <a href="#" onclick="pop()">popup</a>) generally means a link is being used to run JavaScript exclusively. This is bad practice.

Any a element should have a href that points to a valid resource. If one does not exist, consider using another element, such as button.

11
  • 1
    That's a good point about it not particularly good practise... but just because it's not good doesn't mean you won't find it being used like that all over the place :) May 10, 2010 at 4:25
  • 1
    @alex, if it's bad practice, then tell us what the good practice would be to use instead.
    – Ryan Lundy
    May 12, 2011 at 21:20
  • @Kyralessa It was right there in the next paragraph. Anyway, I made an edit so hopefully it is clearer.
    – alex
    May 12, 2011 at 23:23
  • 2
    @alex, presumably one reason people are using links instead of buttons is that they want the look of links. How about a link to a page describing how to make a button look like a link? E.g. HTML button test
    – Ryan Lundy
    May 12, 2011 at 23:33
  • 1
    @alex, no more than is saying that using # on a link is bad practice. All the OP asked is what it does.
    – Ryan Lundy
    May 13, 2011 at 2:15
12

The pound sign (#) indicates to locate an anchor on the page. For example, if you include this somewhere on the page:

<a name="foo"></a>

or, more recently:

<div id="foo">*part of page*</div>

and then you click on a link on the page that has the href #foo, it will navigate to the anchor with the name or div with the id foo.

However, if you just have the href #, it will lead to the top of the page.

11

# indicates a link to an anchor.

I thougt I'd also mention something else:

Using '#' as the href for a link that activates JavaScript is bad because it scrolls the page to the top - which is probably not what you want. Instead, use javascript:void(0).

3
  • +1 for mentioning that clicking on these links forces the page to scroll to the top.
    – Chris Calo
    Nov 6, 2012 at 3:49
  • 4
    You no longer need an anchor. As of HTML5 (and maybe HTML 4) any element with an <id> tag may be targeted by a fragment identifier. See HTML5 docs: whatwg.org/specs/web-apps/current-work/multipage/… Jan 17, 2013 at 3:43
  • 3
    Don't use javascript:void(0) either - use a button if it's not a link.
    – alex
    Apr 6, 2015 at 23:49
2

This links back to the page itself. It's often used with links which actually run some JavaScript.

2

I think most of the posters here forgot how to use Internal Links.

A typical <a> element uses an href attribute to link to an external URL/URI (website). But most new developers do not realize you can also link to internal sections of your web page by using a "#" and an identifier instead. The easiest way to do this cross-browser, is using the following HTML:

This page link...

<a href="#section1">Go to Section 1</a>

...goes to this page location.

<a id="section1" name="section1"></a>

The "#section1" href value is called a "fragment identifier" and will appear in your browser's url when you click the link. Your page will then look for this identifier in your HTML page and automatically scroll down the page to it.

Notice I have used the anchor <a> tag as my receiver to the link. Traditionally this is how most web pages used to use these types of page links. Using anchors you avoid having to rename existing elements. It is also semantically correct and a better way to manage these types of bookmarks. But....it's ok practice to use a <div> or other HTML element with an id and name matching attribute assigned as the bookmark for the fragment identifier.

I like to use both id and name attributes with the fragment identifier, as HTML5 often does not support the name attribute on some elements, while id may not be recognized for the page marker identifier in older browsers.

This shortened, nameless version below is often used by developers as a default URL "stub" for an unknown URL added later to an anchor, to trigger a page refresh, or enable a link but let a Javascipt method capture the click event and route off somewhere else. This makes the "#" a nice fallback should the Javascript piece fail. It then becomes a nice Javascript-free refresh link. The "#" can also be a useful URL "filler for the "href" value when a missing or blank URL on an element might otherwise trigger some problem or style:

<a href="#">Go to the Top</a>
0

The specific question was "I have inspected some sites and they have a pound(#) sign in the url. What does it do?"

An example is then given:
<a href="#" >Link name</a>

A consistent valid answer is given for jumplinks (whatever you want to call them) however the CSS :target psuedoselector would absolutely makes use of the hash in a URL as well.

It doesn't change the answers. However gives another use case I thought would be valuable, to not belabor, see the below excellent link which explains:

However, https://css-tricks.com/on-target/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.