The kbq-flag component shows a country flag. It doesn't contain images โ it decorates the content passed to it: it controls the shape and shadow, and provides accessibility. So the flag looks the same in the product regardless of the image source.
To work with kbq-flag, we recommend the country-flag-icons package:
npm install country-flag-iconsPackage features:
kbq-flag renders the markup you give it, and there are three ways to hand it over.
A projected <img>. The recommended form โ nothing is bypassed, and the browser crops the image itself.
<kbq-flag decorative><img src="โฆ/DE.svg" alt="" /></kbq-flag>A projected inline <svg>, written in the template. Angular compiles the SVG namespace directly, so nothing is stripped and nothing has to be bypassed.
<kbq-flag decorative>
<svg viewBox="0 0 5 3">โฆ</svg>
</kbq-flag>The svg input. Use it for flags that arrive as a string โ this is what country-flag-icons exports.
import { inject } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { DE } from 'country-flag-icons/string/3x2';
protected readonly flag = inject(DomSanitizer).bypassSecurityTrustHtml(DE);<kbq-flag decorative [svg]="flag" />The bypass is not optional here: <svg> is not on Angular's sanitizer allowlist, so a plain string is stripped down to nothing and the flag disappears.
Only pass bypassSecurityTrustHtml markup that is known at build time โ a constant imported from a flag package or from your own set. Markup that arrives over the network must never be bypassed: validate the country code, then look the flag up in the local set (see "If the package doesn't have the flag you need"). Bypassing a value that a request can influence turns the flag into a stored-XSS sink.
Do not bind [innerHTML] on <kbq-flag> itself. The host element is where projected content lands, so that write replaces it โ use the svg input, which renders into a slot of its own.
This is the primary format โ use it in most scenarios: inline with text, lists, selects. A flag with a different ratio (e.g. 4:3) takes the aspectRatio input, which accepts any CSS aspect-ratio value:
<kbq-flag decorative aspectRatio="4 / 3" [svg]="flag" />The input wins over the ratio the shape implies, so shape="square" together with an aspectRatio is no longer square. It also wins over the --kbq-flag-aspect-ratio CSS variable, which is still the way to set one ratio for a whole group of flags at once: the variable is declared at zero specificity, so a plain class rule redefines it โ no !important, no extra selectors.
Whatever ratio the box is given, the flag is cropped to it โ the same way for a projected <img> and for an inline <svg>. Pick a source close to the target ratio so the crop stays small.
For the square shape, take images from the package's 1x1/ directory. Don't crop the rectangular flag yourself โ a simple center crop can remove meaningful elements of the coat of arms or flag. The package already provides 1:1 versions for every country, and some flags โ for example EU and KR โ are redrawn from scratch so they don't lose meaning in this format.
Use an image with a 1:1 aspect ratio.
Flags get a thin inset shadow (an inset outline) along the edge. Without it a flag blends into the background:
That is why the shadow color depends on the theme: in the light theme the shadow is dark, in the dark theme it lightens.
A flag denotes a country or region, not a language. Do not use a flag to pick an interface language: one language is spoken in several countries (Spanish is not only Spain), and one country may have several languages.
First check availability via hasFlag(code) and do not render an empty gap:
import { hasFlag } from 'country-flag-icons';
hasFlag('RU'); // true
hasFlag('ZZ'); // falseThen it depends on the situation.
Unknown or invalid code. Show a neutral placeholder (a gray rectangle, square, circle or a globe icon). Empty space is acceptable only if it does not break the layout.
You need a non-country flag (organization, historical or disputed territory, federal subject). The package is a third-party product limited to the ISO 3166-1 standard: such flags don't belong there. Add them to your project's internal set so that the way kbq-flag is used doesn't change.
Supranational flags. The EU flag is available as a separate 1:1 version; for the rest (ASEAN, UN, etc.) create your own images in the project.
It is a real country, but the flag really is missing. This is unlikely โ the ISO 3166-1 standard is fully covered โ but first update the package version. If the flag is outdated or missing, open an issue or PR in the package repository, and temporarily use your own image in your project.
You need to show the flag at a large size. In the package the flags are redrawn and simplified for small sizes. To show them at a large size, use a third-party package with the original detail โ most such sources don't simplify it.
If the flag carries meaning (for example, it is the only indicator of the selected country), pass a text caption:
<kbq-flag label="Germany"><img src="โฆ/DE.svg" alt="" /></kbq-flag>If there is already adjacent text (inline, option, block), the flag is decorative โ mark it decorative so it is hidden from screen readers and does not duplicate the caption.
<kbq-flag decorative><img src="โฆ/DE.svg" alt="" /></kbq-flag>
GermanyIn short: a flag must always have a text alternative โ either visible text next to it (then the flag is decorative), or a label caption for the screen reader (when there is no visible text). Only the third case is not allowed โ when there is neither.
A flag with no label is hidden from screen readers by default, so the forbidden case is never announced as a nameless graphic. That is a fail-safe, not a substitute for the markup above: the default also hides a name that comes from the flag image itself (an alt attribute or an SVG <title>), so repeat that name in label when the flag carries meaning.
The component supports styling via CSS. For example, you can add rounding, a shadow, and a gradient imitating folds.
Choose a flag size whose dimensions are multiples of a pixel โ otherwise the image edges will be blurry.