Country select
A country select lets people choose one country by typing to filter. The full list of countries, their names in the user's language, and their flags are built in; the value is a two-letter country code.
<tk-field label="Country"> <tk-country-select name="country"></tk-country-select> </tk-field>
Put it in a aria-label. You don't write any options.
Anatomy
| Part | What it is |
|---|---|
tk-country-select |
the element; one Tab stop |
| field | a text input: shows the chosen country, or what people type |
| flag | leads the field and each option; decorative |
| clear button | appears while the field has text |
| chevron | turns over while the menu is open |
| menu | every country, sorted by name in the user's language |
| option | one country; a check marks the chosen one |
Countries and names
<tk-country-select aria-label="Pays" locale="fr" value="DE"></tk-country-select>
- The list covers all 249 ISO 3166-1 countries and territories, plus Kosovo (
XK). - Names come from the browser in the page's language (
langon<html>), or the browser's language when it isn't set.localesets the language for one field; the list re-sorts for it. - The value is the upper-case alpha-2 code, such as
GBorUS. A lower-casevalueis converted. Store and submit the code, not the name. - Flags load the first time one is shown, from the library's own files. Until then, their space is kept empty, so nothing shifts.
Filtering
- Type part of a name, in any case and anywhere in the name: "kingdom" finds "United Kingdom".
- Accents don't matter: "curacao" finds "Curaçao".
- The two-letter code works too: "gb" finds the United Kingdom.
- Nothing is chosen until people choose an option. Leaving the field restores the chosen country. People can't add a country that isn't in the list.
Dial codes
<tk-field label="Phone country"> <tk-country-select dial-code value="NL"></tk-country-select> </tk-field>
dial-code adds the calling code to each name: "Netherlands (+31)". Typing digits, such as
"44", then finds countries by code. Territories with no calling code show none.
Compact
<tk-country-select compact dial-code aria-label="Country calling code" autocomplete="tel-country-code" value="US" ></tk-country-select>
compact shrinks the field to the flag and the calling code, to sit beside a number
field. Use it with dial-code. The clear button is hidden, and the menu stays wide enough
for full names. The field fits about five characters, so a typed name scrolls inside it;
read the matches in the menu. Focusing the field selects the code, so typing replaces it.
Name a compact field with aria-label: a visible label above it would name the whole row.
bare also removes the field's border, background, focus ring, and chevron, for use
inside another field's box.
Sizes
<tk-country-select aria-label="Country" value="JP"></tk-country-select> <tk-country-select size="large" aria-label="Country" value="JP"></tk-country-select>
size |
Height | Text | Flag |
|---|---|---|---|
medium (default) |
32px | Body Small (14/20) | 24×16px |
large |
36px | Body Medium (16/24) | 30×20px |
States
<tk-country-select aria-label="Country" value="IT" disabled></tk-country-select> <tk-country-select aria-label="Country" value="IT" readonly></tk-country-select>
The states are
disabled: can't be focused or opened, and isn't submitted.readonly: shows the country and submits it, but can't be edited or opened. The flag stays; the clear button and chevron are hidden.- Error: set
erroron the Field.
When to use
- Use a country select whenever people pick a country: sign-up, checkout, address, billing.
- Use a
Phone input for phone numbers. It includes the country code picker. - Use a
Combobox with your own options for other long lists, such as currencies or time zones.
Content
- Label: "Country", or what the country is for: "Country of residence", "Billing country". Sentence case, no colon.
- Error: say how to fix it: "Select your country of residence".
More in
Do / Don't
| ✅ Do | ❌ Don't |
|---|---|
| Use it for every country field | Build your own country list on a combobox |
| Store and submit the alpha-2 code | Submit the country name |
Turn on dial-code for phone fields |
Show calling codes where nobody needs them |
| Let the name carry the meaning | Rely on the flag alone |
Related
Combobox : the same field for your own long lists.Phone input : a phone number field with a compact country select.Field : the label, helper, and error around it.Select family : how these compare.
API
Attributes
| Attribute | Type | Default | What it does |
|---|---|---|---|
value |
string | '' |
the chosen country's alpha-2 code; '' means none |
locale |
string | the page's language | the language for country names and their order |
dial-code |
boolean | false |
shows and searches calling codes |
compact |
boolean | false |
flag and calling code only, for a phone field |
bare |
boolean | false |
no border, background, focus ring, or chevron; for use inside another field's box |
autocomplete |
string | country |
the autofill hint; use tel-country-code for a phone prefix |
placeholder |
string | — | the hint shown while the field is empty |
name |
string | — | the form field name |
required |
boolean | false |
an empty value blocks form submission; no visible marker (the Field adds the asterisk) |
disabled |
boolean | false |
not focusable, can't open, left out of the form |
readonly |
boolean | false |
focusable and submitted, but can't be edited or opened |
size |
medium | large |
medium |
height 32 / 36px |
<option> children are ignored: the list is built in.
Events
change fires when someone chooses a country or clears the field. It doesn't fire while
they type, or when you set value in code.
In a form, the code submits under name, and a form reset restores the starting value.
CSS
::part(field): the field.::part(listbox): the menu.
Tokens
Everything else uses
| Part | Tokens |
|---|---|
| flag | icon-size-16 tall (icon-size-20 at large), 3:2; radius-2; a 1px border/subtle edge |
The flag's colors are its own.
Accessibility
Built in:
- Everything in
Combobox : one Tab stop, the match count announced as people type, focus kept in the field. - Flags are hidden from screen readers. The country name is the accessible name of each option and of the field.
Your part:
- Give it a name: a Field label, or
aria-labelwhen it is compact. - If you set
locale, use a real language tag, such asfrorpt-BR.
The keyboard is
Contrast limits
- Field border: 1.33:1 in light and 2.00:1 in dark, under the 3:1 minimum; 4.22:1 in high contrast. Always show a label.