HTML allows developers to include country-specific information using ISO country codes. These codes are essential for personalizing content, improving localization, and enhancing user experience on the web. At The Coding College, we’ve prepared a detailed guide on how to use these codes effectively.
What are ISO Country Codes?
ISO country codes are standardized two-letter codes that represent countries. They are defined by the ISO 3166-1 alpha-2 standard and are widely used in HTML, localization, and internationalization.
Why Use ISO Country Codes in HTML?
- Localization: Serve country-specific content dynamically.
- SEO Benefits: Boost your site’s visibility in country-based searches.
- Consistency: Maintain a global standard for representing countries.
Common ISO Country Codes
Country | ISO Code |
---|---|
United States | US |
United Kingdom | GB |
Canada | CA |
Australia | AU |
India | IN |
Germany | DE |
France | FR |
Japan | JP |
China | CN |
Brazil | BR |
How to Use ISO Country Codes in HTML
ISO country codes are often used in the lang
attribute alongside language codes to provide country-specific content.
Example:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Country-Specific Content</title>
</head>
<body>
<h1>Welcome to The Coding College</h1>
<p lang="en-GB">Welcome to The Coding College - British English version.</p>
<p lang="en-US">Welcome to The Coding College - American English version.</p>
</body>
</html>
In the example above:
lang="en-US"
specifies English content for the United States.lang="en-GB"
specifies English content for the United Kingdom.
Full List of ISO Country Codes
Country | ISO Code | Country | ISO Code |
---|---|---|---|
Afghanistan | AF | Malaysia | MY |
Argentina | AR | Mexico | MX |
Belgium | BE | Netherlands | NL |
Egypt | EG | New Zealand | NZ |
Finland | FI | South Africa | ZA |
Greece | GR | South Korea | KR |
Italy | IT | Sweden | SE |
Kenya | KE | Switzerland | CH |
Russia | RU | Turkey | TR |
For a complete list, refer to the official ISO website or our tutorials at The Coding College.
Using ISO Country Codes for Personalization
You can use ISO country codes to dynamically tailor your website content based on the user’s location. For example:
- Detect the user’s country using their IP address.
- Serve relevant content using ISO country codes.
Example:
const userCountry = "IN"; // Example: Detected country code
if (userCountry === "IN") {
document.body.innerHTML = "<h1>Welcome, India!</h1>";
} else if (userCountry === "US") {
document.body.innerHTML = "<h1>Welcome, USA!</h1>";
}
Best Practices for ISO Country Codes
- Combine with Language Codes: Use
lang
attributes for country-specific languages (e.g.,en-US
,fr-CA
). - SEO Optimization: Use ISO country codes in URL structures for regional targeting (e.g.,
example.com/us/
). - Use Standard Codes: Stick to ISO 3166-1 alpha-2 for compatibility across platforms.
Conclusion
ISO country codes are powerful tools for creating globally accessible and regionally relevant content. By incorporating these codes into your HTML, you enhance accessibility, SEO, and user satisfaction.
For more HTML tips and resources, visit The Coding College and take your web development skills to the next level!