What is href =# in html?

About hyperlinks:

The main use of anchor tags - would scroll to an element on the current page such as

.

href="//site.example/#some-id" would go to site.example and scroll to the id on that page.

Scroll to Top:

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.

Hyperlink placeholders:

An example where a hyperlink placeholder makes sense is within template previews. On single page demos for templates, I have often seen 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.

About anchor tags:

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.

[Hypertext REFerence] The HTML code used to create a link to another page. The HREF is an attribute of the anchor tag, which is also used to identify sections within a document. The HREF contains two components: the URL, which is the actual link, and the clickable text that appears on the page, called the "anchor text."

Relative [implicit] and Absolute [explicit] Links

In the first part of the following example, the URL comprises only the file name EXAMPLES.HTML, which is the Web page [Web document] to link to. Because only the file name is given, it is implied that EXAMPLES.HTML resides in the same folder as the Web page with the link. Relative links are widely used because there are no changes required if the pages are moved to a different folder or if the folder is renamed. Every link points within.

However, if EXAMPLES.HTML were on a different website, the full URL with an // prefix has to be used, as in the second example. In both cases, the words SAMPLE PRODUCTS are the anchor text, which the user sees and clicks. See hypertext.

❮ HTML tag

Example

The href attribute specifies the link's destination:

Visit W3Schools

Try it Yourself »

More "Try it Yourself" examples below.

Definition and Usage

The href attribute specifies the URL of the page the link goes to.

If the href attribute is not present, the or href="#" to link to the top of the current page!

Browser Support

Attribute
href Yes Yes Yes Yes Yes

Syntax

Attribute Values

ValueDescription
URL The URL of the link.

Possible values:

  • An absolute URL - points to another web site [like href="//www.example.com/default.htm"]
  • A relative URL - points to a file within a web site [like href="default.htm"]
  • Link to an element with a specified id within the page [like href="#section2"]
  • Other protocols [like //, ftp://, mailto:, file:, etc..]
  • A script [like href="javascript:alert['Hello'];"]

More Examples

Example

How to use an image as a link:



Try it Yourself »

Example

How to link to an email address:

Send email

Try it Yourself »

Example

How to link to a phone number:

+47 333 78 901

Try it Yourself »

Example

How to link to another section on the same page:

Go to Section 2

Try it Yourself »

Example

How to link to a JavaScript:

Execute JavaScript

Try it Yourself »

❮ HTML tag


What is a href means?

What is the HTML a href attribute? In HTML, the inline a [anchor] element denotes a hyperlink from one web address to another. All functional a elements must contain the href [hypertext reference] attribute inside the opening a tag. The href attribute indicates the destination of the hyperlink.

Why href is used in HTML?

Chủ Đề