Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML Topics

HTML hreflang Attribute

Posted in HTML Tutorial
Updated on Oct 29, 2024
By Mari Selvan
👁️ 50 - Views
⏳ 4 mins
💬 1 Comment
HTML hreflang Attribute

Photo Credit to CodeToFun

🙋 Introduction

The hreflang attribute is an important aspect of HTML that allows web developers to specify the language and regional targeting of a hyperlink.

This attribute is primarily used within the <link> and <a> elements and plays a crucial role in ensuring that search engines and browsers understand the language and regional targeting of linked content.

🎯 Purpose of hreflang

The primary purpose of the hreflang attribute is to inform search engines about the language and regional targeting of a particular webpage.

This helps search engines deliver more relevant search results to users based on their language preferences and geographic location.

💎 Values

The hreflang attribute accepts language codes and optional region subcodes to define the language and regional targeting.

Here are a few examples of valid hreflang attribute values:

  • en: English
  • es: Spanish
  • fr-CA: French (Canadian)

These values help search engines understand the linguistic and regional context of the linked content.

📄 Example

Let's look at a simple example of how to use the hreflang attribute in an HTML link:

hreflang.html
Copied
Copy To Clipboard
<link rel="alternate" href="https://example.com/page" hreflang="en">
<link rel="alternate" href="https://example.com/page-es" hreflang="es">

🧠 How it Works

In this example, two alternative versions of a page are provided—one for English speakers and another for Spanish speakers.

The hreflang attribute is used to specify the language targeting for each version.

🔄 Dynamic Values with JavaScript

You can dynamically set the hreflang attribute using JavaScript.

This can be useful when dealing with multilingual or dynamically generated content. Here's a brief example:

hreflang.html
Copied
Copy To Clipboard
<script>
  // Dynamically set hreflang for a link
  document.getElementById("dynamicLink").hreflang = "fr";
</script>

🧠 How it Works

In this script, the hreflang attribute is dynamically set to fr for a link with the id dynamicLink. This can be beneficial when the language targeting needs to change based on user interactions or other dynamic conditions.

🏆 Best Practices

  • Ensure that the hreflang attribute is used consistently across different language versions of your content.
  • Use the appropriate language and region codes according to the ISO 639-1 and ISO 3166-1 standards.
  • Test the implementation to ensure that search engines correctly interpret the language and regional targeting.

🎉 Conclusion

The hreflang attribute is a valuable tool for web developers aiming to provide a better user 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.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (HTML hreflang Attribute), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy