HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
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:
<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:
<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:
Author
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
If you have any doubts regarding this article (HTML hreflang Attribute), please comment here. I will help you immediately.