country-flag-icons

Vector (*.svg) country flag icons in 3x2 aspect ratio.


Keywords
country, flag, icons, vector, svg, react
License
MIT
Install
npm install country-flag-icons@1.5.11

Documentation

country-flag-icons

npm version npm downloads

Vector country flag icons in 3:2 aspect ratio.

Also provides 1:1 crops of the 3:2 flags (not custom 1:1 flags).

  • Optimized for small size on screen (little detail, minimalism).
  • Small file size.
  • Comes with React components for all flags (exported from /react subpackage).

See 3:2 flags

See 1:1 flags (just 1:1 crops of the 3:2 flags, not custom 1:1 flags)

GitHub

On March 9th, 2020, GitHub, Inc. silently banned my account (and all my libraries) without any notice. I opened a support ticked but they didn't answer. Because of that, I had to move all my libraries to GitLab.

Install

npm install country-flag-icons --save

API

hasFlag(country: string): boolean

Tells whether there's a flag for a country.

import { hasFlag } from 'country-flag-icons'

hasFlag('US') === true
hasFlag('ZZ') === false

countries: string[]

The list of supported countries.

import { countries } from 'country-flag-icons'

countries.includes('US') === true
countries.includes('ZZ') === false

Web

Flags can be linked directly from this library's gitlab pages website.

<img
  alt="United States"
  src="http://catamphetamine.gitlab.io/country-flag-icons/3x2/US.svg"/>

Flags can also be used in the form of CSS classes imported from country-flag-icons/3x2/flags.css where all flag icons are inlined as background-image data URLs. CSS flag icon height can be set via --CountryFlagIcon-height CSS variable.

In that case, the default flag icon height is 1em, and to change it, just set a font-size:

/* Set flag icon height to 24px. */
[class*=' flag:'], [class^='flag:'] {
  font-size: 24px;
}

Unicode

Unicode flag icons are available under the /unicode export.

import getUnicodeFlagIcon from 'country-flag-icons/unicode'

getUnicodeFlagIcon('US') === '🇺🇸'
getUnicodeFlagIcon('ZZ') === '🇿🇿'

Unicode flag icons ("Regional Indicator Symbols") were introduced in 2010 in Unicode version 6.0.

Older operating systems might not support Unicode flags, either rendering "missing" (rectangle) characters (if their system doesn't support country flags), or displaying two-letter country codes instead of emoji flag images. For example, Windows 10 currently (01.01.2020) doesn't support Unicode country flags, and displays two-letter country codes instead of emoji flag images.

React

React components for all flags are available at /react/3x2 export.

import Flags from 'country-flag-icons/react/3x2'

<Flags.US title="United States" className="..."/>

Alternatives

  • FlagKit (flagpack npm package) — 4x3 and 1x1 SVG flag icons. MIT licence.

Credits

I used Google image search for flag references, and various country flag packs (including FlagKit / flagpack) for design ideas. Sometimes there was no need to re-draw a flag — usually in cases when a flag is just a set of colored stripes and there already is an SVG version of it somewhere at Wikipedia or some other free flag pack, so in those cases I simply copied those flags (because they look the same in every flag pack).

Some countries officially use their "mother" country flag (those used to be colonies). For example, BV (Bouvet Island) and SJ (Svalbard and Jan Mayen) use the flag of Norway; GP (Guadeloupe) and RE (Réunion) use the flag of France.

CSS flag icons feature has been submitted by @mindplay-dk.

3:2 to 1:1 flag icons transform offsets submitted by @mindplay-dk.