href

The href attribute in HTML defines the URL for a link, facilitating navigation between web pages, sections, or file downloads when using the <a> tag.

href Attribute

The href attribute in HTML specifies the destination URL of a hyperlink. It is a key component of the <a> (anchor) tag, allowing users to navigate to different web pages, sections within the same page, or download files.

Syntax

index.html
<a href="URL">

Example

index.html
<a href="https://institute.qarpeo.com" target="_blank">Visit https://institute.qarpeo.com</a>
index.html
<a href="https://institute.qarpeo.com" target="_blank">
  <img border="0" alt="Qarpeo" src="logo_w3s.gif" width="100" height="100">
</a>
index.html
<a href="https://institute.qarpeo.com" target="_blank">Visit https://institute.qarpeo.com</a>

Linking to an Email Address

index.html
<a href="mailto:[email protected]">Send email</a>

Linking to a Phone Number

index.html
<a href="tel:+4733378901">+47 333 78 901</a>

Linking to Another Section on the Same Page

index.html
<a href="#section2">Go to Section 2</a>

Linking to JavaScript

::atter-href ::
index.html
<a href="javascript:alert('Hello World!');">Execute JavaScript</a>

Definition and Usage

  • The href attribute defines the URL or path for a hyperlink.
  • If omitted, the <a> tag will not act as a clickable link.
Using href="#top" or href="#" creates a link that scrolls to the top of the current page.

Values

  • URL: Specifies the address of the destination resource.
Using href="#top" or href="#" helps create a quick scroll-to-top feature.

Applies To

The href attribute is applicable to the following elements:

Conclusion

The href attribute is essential for defining hyperlinks in HTML, enabling seamless navigation between pages, sections, and downloadable resources. It applies to elements like <a>, <area>, <link>, and <base>. Proper use of href ensures an efficient and user-friendly browsing experience.