Install flag-icons via npm or Yarn
mainYou can install the flag-icons package using npm or Yarn to include the SVG flag collection and CSS in your project.
npm install flag-icons
# or
yarn add flag-iconsrepository·main·Indexed 11 days ago
https://github.com/lipis/flag-iconsA curated collection of country flags provided as SVGs with accompanying CSS for web integration. Version 7.5.0 supports installation via npm or Yarn, CDN usage, and SASS integration with customizable country inclusions and paths. Flags are displayed using ISO 3166-1-alpha-2 codes via CSS classes such as .fi and .fi-xx.
You can install the flag-icons package using npm or Yarn to include the SVG flag collection and CSS in your project.
npm install flag-icons
# or
yarn add flag-iconsTo display a flag, use an element (typically a <span>) with the following classes:
.fi: The base class for flag icons..fi-xx: The country identifier, where xx is the ISO 3166-1-alpha-2 code (e.g., gr for Greece)..fis: Add this class to create a squared version of the flag..fib: Use this class instead of .fi if you are applying the flag to an element other than a <span> (like a <div>).Note on sizing: When using .fib, the flag is set with background-size: contain, background-position: 50%, and background-repeat: no-repeat. You must manually set the dimensions to a 4:3 ratio, or add the flag-icon-squared class for a square ratio.
<!-- Standard 4:3 flag -->
<span class="fi fi-gr"></span>
<!-- Squared flag -->
<span class="fi fi-gr fis"></span>If you prefer not to install the package locally, you can include the CSS directly in your HTML using a CDN link.
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@7.3.2/css/flag-icons.min.css"
/>To use the flags, you must first import the CSS. You can do this via a direct module import in JavaScript, via a CDN link in HTML, or using SASS.
import "/node_modules/flag-icons/css/flag-icons.min.css";You can integrate the flags into your SASS workflow. You can either use the default configuration or provide custom configuration to override the flag path or include only specific countries.
@use "node_modules/flag-icons/sass/flag-icons";
// or with custom configuration
@use "node_modules/flag-icons/sass/flag-icons" with (
// Override path to flags directory
$flag-icons-path: "node_modules/flag-icons/flags",
// Include only specific country flags
$flag-icons-included-countries: ("gr", "de", "gb")
);The project uses SVGO to optimize SVG files. The configuration uses a preset-default and includes specific plugins to ensure ID uniqueness and clean SVG attributes.
Key plugins used:
preset-default: Applies the standard SVGO optimization suite.prefixIds: Prevents ID collisions by prefixing IDs with the filename (basename). It uses a hyphen - as a delimiter.convertStyleToAttrs: Converts CSS styles into SVG attributes.removeDimensions: Removes width and height attributes.removeScriptElement: Removes <script> elements.removeStyleElement: Removes <style> elements.sortAttrs: Sorts SVG attributes alphabetically.module.exports = {
plugins: [
{
name: "preset-default",
},
{
name: "prefixIds",
params: {
delim: "-",
prefix: (_, info) => {
if (info.path != null && info.path.length > 0) {
return getBasename(info.path).split(".")[0];
}
return "prefix";
},
},
},
"convertStyleToAttrs",
"removeDimensions",
"removeScriptElement",
"removeStyleElement",
"sortAttrs",
],
};