components/flag-icon-css
A curated collection of all country flags in SVG with ready-to-use CSS and SASS for easy integration. Install via npm or Yarn, import the stylesheet, then add fi and fi-xx classes to an empty span; add fis for square flags. Includes CDN support and SASS options for custom paths and country subsets.
Installation:
npm install flag-icons
or via CDN:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lipis/[email protected]/css/flag-icons.min.css" />
First Use Case: Display a flag for Greece inline with text:
<span class="fi fi-gr"></span> <!-- Default flag -->
<span class="fi fi-gr fis"></span> <!-- Squared flag -->
us, gb, jp).sass/_variables.scss – Customization options for SASS.Basic Flag Display:
<!-- Default (proportional) -->
<span class="fi fi-us"></span>
<!-- Squared version -->
<span class="fi fi-us fis"></span>
Dynamic Flag Rendering (Laravel Blade):
@php
$countryCode = 'ca'; // Canada
$isSquared = true;
@endphp
<span class="fi fi-{{ $countryCode }} {{ $isSquared ? 'fis' : '' }}"></span>
SASS Integration (Custom Build with Country Exclusion):
@use "node_modules/flag-icons/sass/flag-icons" with (
$flag-icons-included-countries: ("us", "ca", "jp") // Only include these flags
);
Compile with npm run dev or laravel-mix. Note: as of v7.6.0, the file reference used by the "exclude countries" workflow was corrected — if you previously had to patch paths manually, you can drop that workaround.
Flag as Background Image:
.country-badge {
background: url('/node_modules/flag-icons/flags/1x1/us.svg') no-repeat center;
background-size: contain;
}
Accessibility:
<span class="fi fi-gb" aria-label="United Kingdom"></span>
Laravel Mix/Webpack:
Import the CSS in resources/js/app.js:
import 'flag-icons/css/flag-icons.min.css';
Ensure the flags/ directory is copied to public/ via mix.copy().
Dynamic Country Codes: Fetch codes from an API (e.g., REST Countries) and map them to flags.
Dark Mode: Use SASS variables to override colors:
@use "node_modules/flag-icons/sass/flag-icons" with (
$flag-icons-bg: #f0f0f0, // Light background for flags
$flag-icons-opacity: 0.8
);
Trimming the Bundle:
The $flag-icons-included-countries option is now reliably resolved (v7.6.0 fixed an outdated file reference); use it to cut the build down to only the flags your app actually shows.
Missing Flags:
gb for UK, not uk).SASS Path Issues:
$flag-icons-path in SASS points to the correct directory (default: node_modules/flag-icons/flags).npm run dev after updating SASS variables.CDN Caching:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lipis/[email protected]/css/flag-icons.min.css" integrity="..." crossorigin="anonymous">
File Size:
SVG Optimization:
<img src="...">), ensure your Laravel asset pipeline handles them (e.g., via mix.copy()).Rendering glitches (fixed in v7.6.0):
overflow: hidden, contain, or filter hacks) to mask this, remove them after upgrading.7.6.0 is worth a visual spot-check on flags previously worked around.Flag Not Loading?
/flags/1x1/us.svg).flags/ directory is accessible in public/.SASS Errors:
npm run dev to recompile SASS after changes.fi-gr vs. fi-gb).Visual regressions vs. old version?
Custom Flags:
flags/ directory and reference them with fi-custom (requires CSS updates).Flag Size:
fi-2x, fi-3x, etc., for larger flags:
<span class="fi fi-us fi-2x"></span>
Flag Animations:
.fi.fi-us {
transition: transform 0.3s;
}
.fi.fi-us:hover {
transform: scale(1.2);
}
Laravel View Composers:
// app/Providers/AppServiceProvider.php
View::composer('*', function ($view) {
$view->with('countryFlags', ['us', 'ca', 'gb']); // Preload common flags
});
Localization:
trans():
<span class="fi fi-fr"></span> {{ trans('countries.fr') }}
How can I help you explore Laravel packages today?