Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

href — link address or base URL

The href attribute specifies a URL used by a, area, link, and base elements. Its meaning depends on the element: it identifies a hyperlink destination, the address of a related resource, or the document base URL. Relative URLs are resolved against the base URL.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

href="URL"

href is not a global attribute. On a and area, it specifies a hyperlink destination; without it, these elements do not create hyperlinks. Its value must be a valid URL, optionally surrounded by spaces. It can be absolute or relative, including a fragment such as #intro.

On link, href identifies the address of a related resource, while rel specifies the relationship, such as stylesheet. When present, the URL in href must be non-empty. A link element must have at least one of href or imagesrcset; href alone does not determine how the resource is processed.

On base, href specifies the document base URL used to resolve relative URLs. A document may contain at most one base element. When it has href, it must precede other elements with URL-valued attributes. It also changes how fragment links such as #intro are resolved.

In the example, getAttribute('href') returns the stored attribute value, whereas the guide.href property returns the serialized, resolved URL. The explicit base makes the result independent of where the file is saved. An empty href="" on a is not equivalent to omitting the attribute: it still creates a hyperlink.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>href example</title>
  <base href="https://example.org/docs/">
</head>
<body>
  <a id="guide" href="guide.html#intro">Guide</a>
  <pre id="result"></pre>
  <script>
    const guide = document.getElementById('guide');
    document.getElementById('result').textContent =
      guide.getAttribute('href') + '\n' + guide.href;
  </script>
</body>
</html>

Result

guide.html#intro
https://example.org/docs/guide.html#intro

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources