What is it when a link has a pound "#" sign in it

Html

Html Problem Overview


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

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

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

... 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.

Solution 3 - Html

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.

Solution 4 - Html

# 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).

Solution 5 - Html

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

Solution 6 - Html

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.

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>

Solution 7 - Html

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuke101View Question on Stackoverflow
Solution 1 - HtmlDean HardingView Answer on Stackoverflow
Solution 2 - HtmlalexView Answer on Stackoverflow
Solution 3 - HtmlElectroBitView Answer on Stackoverflow
Solution 4 - HtmlNathan OsmanView Answer on Stackoverflow
Solution 5 - Htmluser181548View Answer on Stackoverflow
Solution 6 - HtmlStokelyView Answer on Stackoverflow
Solution 7 - HtmlRogelioView Answer on Stackoverflow