Country-aware address fields in HTML, CSS and vanilla JS
A copy-paste Country-aware address fields in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-address-country-fields" data-dcl-scheme="light" aria-labelledby="example-address-country-fields-title">
<header><h2 id="example-address-country-fields-title">Country-aware address fields</h2>
<p>Changing country immediately relabels the region and postal-code fields instead of forcing one wording on everyone.</p></header>
<div class="parts">
<fieldset>
<legend>International address</legend>
<div class="fields">
<div class="field">
<label for="example-address-country-fields-country">Country or territory</label>
<select id="example-address-country-fields-country" autocomplete="country" data-country>
<option value="gb" selected>United Kingdom</option>
<option value="us">United States</option>
<option value="ca">Canada</option>
<option value="au">Australia</option>
</select>
</div>
<div class="field">
<label for="example-address-country-fields-street">Street address</label>
<input type="text" id="example-address-country-fields-street" autocomplete="address-line1">
</div>
<div class="two-column">
<div class="field">
<label for="example-address-country-fields-city">Town or city</label>
<input type="text" id="example-address-country-fields-city" autocomplete="address-level2">
</div>
<div class="field">
<label for="example-address-country-fields-region"><span data-region-label>County</span></label>
<input type="text" id="example-address-country-fields-region" autocomplete="address-level1" aria-describedby="example-address-country-fields-region-hint" data-region>
<p class="hint" id="example-address-country-fields-region-hint" data-region-hint>Use a county or unitary authority.</p>
</div>
</div>
<div class="field">
<label for="example-address-country-fields-postcode"><span data-postcode-label>Postcode</span></label>
<input type="text" id="example-address-country-fields-postcode" autocomplete="postal-code" aria-describedby="example-address-country-fields-postcode-hint" data-postcode>
<p class="hint" id="example-address-country-fields-postcode-hint" data-postcode-hint>Use the full postcode, for example BN1 4QT.</p>
</div>
</div>
</fieldset>
<p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-address-country-fields");
const instance = Array.from(document.getElementsByClassName("example-address-country-fields")).indexOf(root);
if (instance > 0) {
const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
for (const element of [root, ...root.querySelectorAll("*")]) {
for (const attr of singleReferences) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
}
for (const attr of tokenReferences) {
if (element.hasAttribute(attr)) {
const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
element.setAttribute(attr, rewritten.join(" "));
}
}
const href = element.getAttribute("href");
if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
}
}
const preference = matchMedia("(prefers-color-scheme: dark)");
const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
preference.addEventListener("change", apply);
apply();
const country = root.querySelector("[data-country]");
const regionLabel = root.querySelector("[data-region-label]");
const postcodeLabel = root.querySelector("[data-postcode-label]");
const regionHint = root.querySelector("[data-region-hint]");
const postcodeHint = root.querySelector("[data-postcode-hint]");
const region = root.querySelector("[data-region]");
const postcode = root.querySelector("[data-postcode]");
const status = root.querySelector("[data-status]");
const labels = {
gb: { country: "United Kingdom", region: "County", postcode: "Postcode", regionHint: "Use a county or unitary authority.", postcodeHint: "Use the full postcode, for example BN1 4QT." },
us: { country: "United States", region: "State", postcode: "ZIP code", regionHint: "Use the state name or two-letter abbreviation.", postcodeHint: "Use the five-digit ZIP code; ZIP+4 is also fine." },
ca: { country: "Canada", region: "Province or territory", postcode: "Postal code", regionHint: "Use the province or territory name.", postcodeHint: "Use the six-character postal code, for example K1A 0B1." },
au: { country: "Australia", region: "State or territory", postcode: "Postcode", regionHint: "Use the state or territory name.", postcodeHint: "Use the four-digit postcode." }
};
function applyCountry() {
const copy = labels[country.value];
regionLabel.textContent = copy.region;
postcodeLabel.textContent = copy.postcode;
regionHint.textContent = copy.regionHint;
postcodeHint.textContent = copy.postcodeHint;
region.setAttribute("autocomplete", country.value === "us" ? "address-level1" : "address-level1");
postcode.setAttribute("inputmode", country.value === "us" || country.value === "au" ? "numeric" : "text");
status.textContent = copy.country + " labels are shown: " + copy.region + " and " + copy.postcode + ".";
}
country.addEventListener("change", applyCountry);
})();
</script></section>
<style>
.example-address-country-fields { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-address-country-fields a { color: #005c62; }
.example-address-country-fields button, .example-address-country-fields summary, .example-address-country-fields input, .example-address-country-fields select, .example-address-country-fields textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-address-country-fields button:hover, .example-address-country-fields summary:hover, .example-address-country-fields button[aria-selected="true"], .example-address-country-fields button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-address-country-fields dialog { color: #11292f; background-color: #ffffff; }
.example-address-country-fields [data-note] { color: #11292f; background-color: #e4edef; }
.example-address-country-fields [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-address-country-fields [data-success] { color: #14532d; background-color: #dcfce7; }
.example-address-country-fields [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-address-country-fields :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-address-country-fields[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-address-country-fields[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-address-country-fields[data-dcl-scheme="dark"] button, .example-address-country-fields[data-dcl-scheme="dark"] summary,
.example-address-country-fields[data-dcl-scheme="dark"] input, .example-address-country-fields[data-dcl-scheme="dark"] select, .example-address-country-fields[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-address-country-fields[data-dcl-scheme="dark"] button:hover, .example-address-country-fields[data-dcl-scheme="dark"] summary:hover,
.example-address-country-fields[data-dcl-scheme="dark"] button[aria-selected="true"], .example-address-country-fields[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-address-country-fields[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-address-country-fields[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-address-country-fields[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-address-country-fields[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-address-country-fields[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-address-country-fields[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-address-country-fields, .example-address-country-fields * { box-sizing: border-box; min-width: 0; }
.example-address-country-fields { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-address-country-fields h2, .example-address-country-fields h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-address-country-fields header p, .example-address-country-fields p { margin-block: 0.5rem; }
.example-address-country-fields ul, .example-address-country-fields ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-address-country-fields dl { margin-block: 0.5rem; }
.example-address-country-fields dt { font-weight: 700; }
.example-address-country-fields dd { margin: 0 0 0.6rem; }
.example-address-country-fields fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-address-country-fields legend { padding-inline: 0.25rem; font-weight: 700; }
.example-address-country-fields button, .example-address-country-fields input, .example-address-country-fields select, .example-address-country-fields textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-address-country-fields input, .example-address-country-fields select, .example-address-country-fields textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-address-country-fields input[type="checkbox"], .example-address-country-fields input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-address-country-fields input[type="file"] { padding: 0.45rem; }
.example-address-country-fields input[type="range"] { padding-inline: 0; }
.example-address-country-fields textarea { min-block-size: 6rem; resize: vertical; }
.example-address-country-fields button, .example-address-country-fields summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-address-country-fields button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-address-country-fields table { border-collapse: collapse; inline-size: 100%; }
.example-address-country-fields th, .example-address-country-fields td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-address-country-fields [hidden] { display: none !important; }
.example-address-country-fields [data-note], .example-address-country-fields [data-error], .example-address-country-fields [data-success], .example-address-country-fields [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-address-country-fields .parts { display: grid; gap: 1rem; }
.example-address-country-fields .field, .example-address-country-fields .fields { display: grid; gap: 0.4rem; }
.example-address-country-fields .fields { gap: 0.75rem; }
.example-address-country-fields .actions, .example-address-country-fields .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-address-country-fields .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-address-country-fields .plain-list { margin: 0; padding: 0; list-style: none; }
.example-address-country-fields .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-address-country-fields .choice input { margin-block-start: 0.25rem; }
.example-address-country-fields .choice-stack { display: grid; gap: 0.55rem; }
.example-address-country-fields .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-address-country-fields .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-address-country-fields .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-address-country-fields .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-address-country-fields .hint { font-size: 0.875rem; }
.example-address-country-fields .status { min-block-size: 1.5rem; margin-block: 0; }
.example-address-country-fields .two-column, .example-address-country-fields .split-layout { display: grid; gap: 1rem; }
.example-address-country-fields .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-address-country-fields .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-address-country-fields .visually-hidden {
position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
.example-address-country-fields .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-address-country-fields .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-address-country-fields { padding: 0.75rem; }
.example-address-country-fields .actions, .example-address-country-fields .inline-row { align-items: stretch; }
.example-address-country-fields .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-address-country-fields * { transition: none !important; } }
.example-address-country-fields fieldset { display: block; }
</style>
When to use
Use this when one checkout or account form serves several countries and the labels need to match local address conventions. Extend the small label map with data you have verified rather than guessing every territory.
Avoid
Do not use a fixed ZIP/postcode label for an international audience; it makes people guess which part of their address is required.
Accessibility
The country control is a native select and both text inputs keep persistent visible labels; only the label text and autocomplete hints change. The polite status announces the relabelling so screen reader users hear the context change. Main text contrast is 15.19:1 light and 13.07:1 dark; controls are 12.78:1 and 10.94:1.
Was this useful? Send feedback.