👀 Live Preview
Visible language switcher links with hreflang on anchor elements:
Each link’s hreflang tells search engines the language of the destination page. Use descriptive link text so users know which version they are choosing.

The hreflang attribute lets you tell search engines and browsers which language or region a linked page is intended for. It is essential for multilingual websites because it helps Google and other search engines show the correct localized version to each user. You will most often see it on <link rel="alternate"> tags in the document <head>, and sometimes on visible <a> language-switcher links.
ISO 639-1 (en, es).
fr-CA, en-GB.
SEO link pattern.
Global fallback URL.
Where it applies.
Set hreflang at runtime.
hreflangThe primary purpose of the hreflang attribute is to inform search engines about the language and regional targeting of a particular webpage. When you publish the same content in multiple languages or for different countries, search engines need a clear signal about which URL belongs to which audience.
This helps search engines deliver more relevant results to users based on their language preferences and geographic location. For example, a user searching in French from Canada should see your French Canadian page (fr-CA) rather than the English version or French pages meant for France (fr-FR).
hreflang vs langThe lang attribute on <html> describes the language of the current page’s content. hreflang describes the language of the page you are linking to in href. Use both together on multilingual sites.
Add hreflang alongside href on a <link> or <a> element. For SEO, place alternate-language links in the document head:
<link rel="alternate" hreflang="en" href="https://example.com/page">
<link rel="alternate" hreflang="es" href="https://example.com/page-es"><link> and <a> elements that include an href attribute.rel="alternate" on <link> elements in the <head>.en, fr-CA.x-default for a fallback page when no other language or region matches.href for hreflang annotations when possible.The hreflang attribute accepts language codes and optional region subcodes to define language and regional targeting:
hreflang="en" — English (any region).hreflang="es" — Spanish (any region).hreflang="fr-CA" — French as used in Canada.hreflang="en-GB" — English as used in the United Kingdom.hreflang="x-default" — Fallback page for users whose language or region does not match any other alternate.These values help search engines understand the linguistic and regional context of the linked content. Follow the ISO 639-1 standard for language codes and ISO 3166-1 for region codes.
<!-- Language only -->
<link rel="alternate" hreflang="de" href="https://example.com/de/">
<!-- Language + region -->
<link rel="alternate" hreflang="en-US" href="https://example.com/en-us/">
<!-- Global fallback -->
<link rel="alternate" hreflang="x-default" href="https://example.com/">| Use Case | hreflang Value | Notes |
|---|---|---|
| English page | en | Language only |
| Spanish page | es | Language only |
| French (Canada) | fr-CA | Language + region |
| Fallback / picker | x-default | When no match exists |
| SEO pattern | link rel="alternate" | In document head |
| Applicable elements | link, a | Must have href |
| Element | Supported? | Notes |
|---|---|---|
<link rel="alternate" href="..."> | Yes | Primary SEO use in <head> |
<a href="..."> | Yes | Language switcher links; less common for SEO than <link> |
<link rel="stylesheet"> | No | hreflang is for alternate page versions, not stylesheets |
<input>, <form> | No | Not valid on form controls |
Alternate links for SEO, a full multilingual head block with x-default, and dynamic hreflang values with JavaScript.
Visible language switcher links with hreflang on anchor elements:
Each link’s hreflang tells search engines the language of the destination page. Use descriptive link text so users know which version they are choosing.
Two alternative versions of a page—one for English speakers and one for Spanish speakers:
<head>
<link rel="alternate" href="https://example.com/page" hreflang="en">
<link rel="alternate" href="https://example.com/page-es" hreflang="es">
</head>In this example, the hreflang attribute specifies the language targeting for each alternate URL. Search engines read these annotations to understand that the English page and Spanish page are equivalent content in different languages.
x-defaultA complete set of alternates including a global fallback page:
<head>
<link rel="alternate" hreflang="x-default" href="https://example.com/">
<link rel="alternate" hreflang="en" href="https://example.com/en/">
<link rel="alternate" hreflang="fr-CA" href="https://example.com/fr-ca/">
<link rel="alternate" hreflang="de" href="https://example.com/de/">
</head>Every language version should list all alternates, including itself. The x-default entry points to a page for users whose language is not listed—often a language picker or global homepage.
You can dynamically set the hreflang attribute using JavaScript when dealing with multilingual or dynamically generated content:
<a id="dynamicLink" href="https://example.com/fr/">Version française</a>
<script>
document.getElementById("dynamicLink").hreflang = "fr";
</script>In this script, the hreflang attribute is dynamically set to fr for a link with the id dynamicLink. This is useful when language targeting needs to change based on user interactions, A/B tests, or content loaded from an API. You can also use setAttribute("hreflang", "fr").
aria-current="page" on the active switcher link.lang — Set lang on <html> for the page content language; use hreflang on links to alternate versions.Each language URL gets hreflang and href.
Google reads reciprocal hreflang annotations.
Engine matches locale to the correct alternate URL.
Users find content in their language; you avoid duplicate-content confusion.
The hreflang attribute is supported in all modern browsers on <link> and <a> elements. Its primary consumer is search engines rather than visual browser behavior—users do not see a different UI when hreflang is present.
All major browsers parse hreflang on links and anchors. Search engines rely on it for international SEO.
Bottom line: Use hreflang confidently for multilingual SEO; validate annotations with Google Search Console or similar tools.
hreflang consistently across all language versions of your contentx-default when you have a global fallback pagehreflang (linked page language) and lang (current page language)hreflang on each language versionThe hreflang attribute is a valuable tool for web developers aiming to provide a better experience for multilingual audiences. By accurately specifying language and regional targeting, you can enhance the discoverability and relevance of your content in search engine results.
Remember to use appropriate ISO language and region codes, keep annotations reciprocal across all versions, and pair visible language switchers with proper <link rel="alternate"> tags in the head.
hreflangBookmark these before launching a multilingual site.
Tells search engines which URL matches which language.
Purposeen, es, fr-CA
ValuesHead link pattern
SyntaxEvery version links to all
SEOGlobal fallback URL
Fallbackhref. Search engines use it to show the correct localized version to users in different countries.<link rel="alternate"> in the document head (primary SEO use) and <a> anchor elements for visible language switchers. Both require an href attribute.lang on <html> describes the language of the current page’s content. hreflang describes the language of the page you are linking to.x-default is a special value for a fallback page when no other language or region matches the user—commonly a language picker or global homepage.linkElement.hreflang = "fr" or use setAttribute("hreflang", "fr") on <link> or <a> elements at runtime.Practice the hreflang attribute with alternate language links in the Try It editor.
5 people found this page helpful