close
The Wayback Machine - https://web.archive.org/web/20241129050558/https://www.geeksforgeeks.org/html-href-attribute/
Open In App

HTML href Attribute

Last Updated : 29 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

HTML href Attribute: It is used to specify the URL of the document. It contains the full address of the page that the user requests. It is used to link or connect one document to another document.

Supported tags:

HTML <a> href Attribute

It is used to specify the URL of the page that the link goes to. When the href attribute is not present in the <a>, then the element will not be a hyperlink.

Syntax:

<a href="URL">

Example: Below code illustrates the href attribute in <a> element. 

HTML
<!DOCTYPE html>
<html>

<head>
    <title>HTML href attribute</title>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>HTML a href Attribute</h2>
    <a href="https://ide.geeksforgeeks.org/">
        Click to open in the same tab
    </a>
    <br />
    <a href="https://ide.geeksforgeeks.org/" target="_blank">
        Click to open in a different tab
    </a>
</body>

</html>

Output:

HTML <base> href Attribute:

The HTML <base> href attribute is used to specify the base URL for all the relative URL of a page.

Syntax:

<base href="URL">

Example: Below code illustrates the href in <base> tag. 

HTML
<!DOCTYPE html>
<html>

<head>
    <title>HTML href Attribute</title>
  
    <!-- Declaring the base URL -->
    <base href="geeksforgeeks.org" target="_blank" />
</head>
<h2>GeeksforGeeks</h2>
<h3>HTML base href Attribute</h3>

<body>
    <img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210908192254/logo2.png" 
         width="200"
         height="200" />
</body>

</html>

Output:

BERJAYA

HTML <area> href Attribute:

The <area> href attribute is used to specify the URL of the target page. When the href attribute is not present in the <area>, an element that it will not be a hyperlink.

Syntax:

<area href="URL">

Example: Below code illustrates the use of href attribute in <area> tag. 

HTML
<!DOCTYPE html>
<html>

<head>
    <title>HTML href Attribute</title>
</head>

<body style="text-align: center">
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190227165729/area11.png" 
         alt="alt_attribute"
         width="300" height="119" 
         class="aligncenter" usemap="#shapemap" />
    <map name="shapemap">
      
        <!-- area tag contained image. -->
        <area shape="poly" coords="59, 31, 28, 83, 91, 83"
              href=
"https://media.geeksforgeeks.org/wp-content/uploads/20190227165802/area2.png" 
              alt="Triangle" />
        <area shape="circle" coords="155, 56, 26"
              href=
"https://media.geeksforgeeks.org/wp-content/uploads/20190227165934/area3.png" 
              alt="Circle" />
        <area shape="rect" coords="224, 30, 276, 82"
              href=
"https://media.geeksforgeeks.org/wp-content/uploads/20190227170021/area4.png" 
              alt="Square" />
    </map>
</body>

</html>

Output:

HTML <link> href Attribute:

The HTML <link> href attribute is used to specify the URL of the linked document. It mostly contains the URL of the stylesheet file. 

Syntax:

<link href = "url">

Example: Below code illustrates the use of href attribute in the <link> tag. 

HTML
<!DOCTYPE html>
<html>

<head>
    <title>HTML href Attribute</title>
    <link id="linkid" rel="stylesheet" 
          type="text/css" 
          href="style.css" 
          sizes="16*16" 
          hreflang="en-us" />
</head>

<body style="text-align: center">
    <h2>GeeksforGeeks</h2>
    <b>link href Attribute</b>
</body>

</html>

Output:

BERJAYA

how to use the link attribute

Supported Browsers:

HTML href Attribute – FAQs

What types of URLs can be used with the href attribute?

The href attribute can use absolute URLs (full web address) or relative URLs (path relative to the current page) and can link to other resources like files (PDF, images).

How do you open a link in a new tab using the href attribute?

To open a link in a new tab, use the target attribute along with href, like this: <a href=”https://www.example.com” target=”_blank”>Visit Example</a>.

Can the href attribute link to sections within the same page?

Yes, the href attribute can link to a specific section within the same page using an ID selector. Example: <a href=”#section1″>Go to Section 1</a> and <div id=”section1″>Content</div>.

How do you use the href attribute to link to an email address?

To link to an email address, use mailto: followed by the email. Example: <a href=”mailto:someone@example.com”>Email Us</a>.

What happens if the href attribute is empty or missing?

If the href attribute is empty (href=””), clicking the link typically does nothing. If it is missing, the browser does not treat it as a clickable link.



Previous Article
Next Article

Similar Reads

How href attribute is different from src attribute in HTML ?
In HTML5, the href and src attributes play distinct roles in defining paths or URLs, each associated with specific HTML elements. The href attribute, commonly found in an anchor (<a>) elements, points to the destination of hyperlinks, facilitating navigation. The src attribute, used with elements like <img> and <script>, specifies
1 min read
HTML | <link> href Attribute
The HTML <link> href Attribute is used to specify the URL of the linked document. It mostly contains the URL of the stylesheet file. Syntax: <link href="URL"> Attribute Values: It contains the value i.e URL which specifies the URL of the linked document. Possible values are: absolute URL: It points to another website.Relative URL: iT po
1 min read
HTML | <area> href Attribute
The <area> href attribute is used to specify the URL of the target page. When the href attribute is not present in the <area> an element that it will not be a hyperlink. Syntax: <area href="URL"> Attribute Value: absolute URL: It points to another website.relative URL: It points to a file within a website.anchor URL: It points to
1 min read
HTML <base> href Attribute
HTML <base> href Attribute specifies the base URL for all relative URLs in a document, serving as the reference point for resolving relative links and resources. Syntax:  <base href="URL">Attribute Values: It contains the value i.e URL which specifies the absolute URL that defines the base URL of the page. for eg."www.geeksforgeeks.org"
1 min read
HTML <a> href Attribute
The HTML <a> href attribute is used to specify the URL of the page that the link points to. When the href attribute is not present in the <a> element, it will not function as a hyperlink. This attribute is essential for creating links to any address and is used in conjunction with the <a> tag. Syntax<a href="URL"> Link text
1 min read
How to hide span element if anchor href attribute is empty using JavaScript ?
The task is to hide the <span> tag when href attribute of <a> tag is not present or invalid. Let us consider we have <a> tag inside the <span> tag. It will look similar to this: <span><a href="www.google.com"></a></span> Now we add the id to the elements to identify them. <span id="outer"><a
2 min read
How to get an element by its href attribute ?
The task is to select the <a> element by its href attribute. A few of the techniques are discussed below. Here we are going to use JQuery to solve the problem. Approach: Use JQuery selector $('a[href=link_to_site]'). Example 1: This example using the approach discussed above. C/C++ Code <script src="https://ajax.googleapis.com/ajax/li
2 min read
How to set href attribute at runtime?
We know how to set the href attribute of the anchor tag int HTML, but sometimes we may require to set the href attribute at runtime i.e. for example, when a user provides us a url and we want to set it at runtime. We can do this with the help of jQuery. Example 1: In this example, using jquery, we set the attr() method to the url entered by the use
2 min read
HTML5 MathML href Attribute
The href attribute is used to specify the URL of the linked document. This attribute is accepted by all the MathML tags. Syntax: <element href="valid URL"> Attribute Value: href: This attribute is used to specify the URL of the linked document. Below example illustrate the href attribute in HTML5 MathML: Example: <!DOCTYPE html> <htm
1 min read
What does the "href" attribute specify in an anchor tag?
The "href" attribute in an anchor <a> tag specifies the URL (Uniform Resource Locator) of the destination page or resource that the link points to. It defines the hyperlink's target location, allowing users to navigate to another web page, document, or resource when they click on the link. Syntax<a href="URL">Link Text</a> Key Poi
2 min read
How to insert a JavaScript variable inside href attribute?
To insert a JavaScript variable inside the href attribute of an HTML element, <a > ... </a> tags are used. One of the attributes of 'a' tag is 'href'. href: Specifies the URL of the page the link goes to. Below are the Methods to use Variables inside this 'href' attribute: Table of Content Using onclick propertyUsing document.writeUsing
3 min read
How to change href attribute of a hyperlink using jQuery ?
Changing the href attribute of a hyperlink using jQuery refers to the process of modifying the URL a link directs to without altering the HTML manually. This capability is useful for creating dynamic web applications where link destinations need to change based on user interactions or other conditions. Table of Content Using the attr() methodUsing
2 min read
HTML DOM Location href Property
The Location href property in HTML is used to set or return the complete URL of the current page. The Location href property can also be used to set the href value point to another website or point to an email address. The Location href property returns a string that contains the entire URL of the page, including the protocol. Syntax: It returns th
2 min read
HTML | DOM Base href Property
The DOM Base href Property in HTML DOM is used to set or return the value of the href attribute of a <base> element. The href attribute is used to specify the base URL for all the relative URL in the webpage. Syntax: It returns the href property. baseObject.href It is used to set the href property. baseObject.href = URL Property Values: It co
2 min read
HTML | DOM Link href Property
The HTML | DOM Link href Property is used to set or return the URL of a Linked Document. Syntax: It return the href property.linkObject.hrefIt is used to Set the href property.linkObject.href = URL Property Values: It contains the value i.e URL which specify the URL of the link. absolute URL: It points to another website.relative URL: It points to
2 min read
HTML DOM Area href Property
The Area href Property in HTML DOM is used to set or return the value of the href attribute. It is used to specify the URL of the area. For example: https://manaschhabra:manaschhabra499@www.geeksforgeeks.org/ (manaschhabra is the username and manaschhabra499 is the password). Syntax: It returns the href property. areaObject.hrefIt is used to set th
2 min read
HTML DOM Anchor href Property
The Anchor href Property in HTML DOM is used to set or return the value of href attribute of a link. The href attribute is used to specify the link's destination. Syntax: It returns the href property. anchorObject.hrefIt is used to set the href property. anchorObject.href = URL Property Values: It contains the value i.e. URL which specify the URL o
2 min read
AngularJS ng-href Directive
The ng-href directive is used when we have an angular expression inside the href value. If href attribute is used then the problem is that if in case the link is clicked before AngularJS has replaced the expression with its value then it may go to the wrong URL and the link will be broken and will most likely return a 404 error while ng-href direct
2 min read
Node.js URL.href API
The url.href is an inbuilt application programming interface of class URL with in the url module which Gets and sets the serialized URL. Getting the value of the href property is equivalent to calling the url.toString() method.Setting the value of this property to a new value is equivalent to creating a new URL object using new URL(value). Each of
2 min read
Node.js urlObject.href API
The urlObject.href API is used to return the complete URL string along with the protocols(HTTP) and pathname or other search terms. Syntax: urlObject.hrefFor example: 'http://www.geeksforgeeks.com/login/password.html' Here, Protocol = http Path = /login Host = 'www' File Name = password.html Example 1: The below example illustrates the use of urlOb
1 min read
How to make workaround for window.location.href?
Given a URL, the task is to use the current address of the page and to perform an operation using this address. Example 1: This example points to the same URL and only redirects to the same address.URL = 'https://gitstr.herokuapp.com' <!DOCTYPE html> <html lang="en"> <head> <title>Example 1:</title> </head
2 min read
How to change the href value of a tag after click on button using JavaScript ?
JavaScript is a high-level, interpreted, dynamically typed, and client-side scripting language. HTML is used to create static web pages. JavaScript enables interactive web pages when used along with HTML and CSS. Document Object Manipulation (DOM) is a programming interface for HTML and XML documents. DOM acts as an interface between JavaScript and
4 min read
How to link back out of a folder using the a-href tag?
Introduction: Whenever we make a project it is generally distributed among various folders. That is generally done to improve the connectivity and readability for the various web developers working on the same project. For example: The above classification is just an example but the classification can be bigger. So if you have to call an image from
2 min read
SVG AElement.href Property
The SVG AElement.href property returns an SVGAnimatedLength object corresponding to the attribute of the given A element Syntax: AElement.href Return value: This property returns SVGAnimatedLength object that can be used to get the href of the AElement. Example 1: <!DOCTYPE html> <html> <body> <svg viewBox="0 0 100 100
1 min read
What is the Purpose of base href Tag in Angular ?
In this article, we will see what is base href tag in Angular, along with understanding their basic implementation with the help of examples. Base href TagThe base href is important for generating correct routes, in -case you are deploying your project in a subfolder. The base href element has the angular router to compose the navigation URLs. The
2 min read
How to style a href links in React using Tailwind CSS ?
In this article, we will learn how we can style a href link in Reactjs. Since we use Tailwind CSS, Tailwind CSS describes itself as the first applicable CSS framework. Rather than focusing on the performance of a style object, Tailwind focuses on how it should be displayed. This makes it easier for the developer to explore new styles and transform
2 min read
Which href value should we use for JavaScript links, "#" or "javascript:void(0)" ?
In this article, we will know which "href" should be used for JavaScript when we can use "#" or "javascript:void(0)". Let us understand it. Using the '#' value: The # is a fragment identifier used to refer to a specific section of the same document. When a user clicks a link with a # href, the browser will scroll to that section of the page specifi
3 min read
How to Add href to React-Bootstrap-Table Column Dynamically ?
In this article, we are going to learn how to add a href to the react-bootstrap table column dynamically. In this article, we will create a React Bootstrap table and an input form to add data dynamically with a href. Prerequisites: React JSReact-BootstrapSteps to Create React Application And Installing Modules:Step 1: Create your react-app by using
4 min read
How to set location and location.href using JavaScript ?
In this article, we will set the location and location.href using Javascript. Both location and location.href are used to set or return the complete URL of your current page. They return a string that contains the entire URL with the protocol. Syntax: location = "https://www.geeksforgeeks.org";or location.href = "https://www.geeksforgeeks.org";Both
1 min read
What is the difference between anchor href vs angular routerlink in Angular ?
Routing in Angular allows the users to create a single-page application with multiple views and allows navigation between them. Users can switch between these views without losing the application state and properties. In this example, we will learn the difference between anchor href vs angular routerlink in Angular. href AttributeThe href attribute
5 min read
three90RightbarBannerImg