I was reading through the GitHub project and noticed that icons extend from a BaseIcon class, which in turn extends from a BaseComponent. From what I saw, you can use the selector, for example, <AngleUpIcon>
, to display the SVG of that icon. However, I would like to understand how PrimeNG manages to display icons using classes, for example:
<i class="pi pi-user" style="font-size: 2.5rem"></i>
Find a clear explanation of how PrimeNG achieves the functionality of displaying icons using classes instead of HTML tags, possibly through custom components or other mechanisms.
I was reading through the GitHub project and noticed that icons extend from a BaseIcon class, which in turn extends from a BaseComponent. From what I saw, you can use the selector, for example, <AngleUpIcon>
, to display the SVG of that icon. However, I would like to understand how PrimeNG manages to display icons using classes, for example:
<i class="pi pi-user" style="font-size: 2.5rem"></i>
Find a clear explanation of how PrimeNG achieves the functionality of displaying icons using classes instead of HTML tags, possibly through custom components or other mechanisms.
Share Improve this question edited Mar 18 at 8:15 DarkBee 15.5k8 gold badges72 silver badges117 bronze badges asked Mar 18 at 7:02 José AlvaradoJosé Alvarado 111 silver badge2 bronze badges1 Answer
Reset to default 1https://github/primefaces/primeicons/blob/master/primeicons.css — this is the explanation you're looking for.
Basically, they use PrimeIcons, which are anized as a font. .pi
CSS class specifies that it should use this font, and adds some rendering hints:
.pi {
font-family: 'primeicons';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
display: inline-block;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
While the specific class, e.g. .pi-user
, simply specifies which symbol to render before your empty element:
.pi-user:before {
content: "\e939";
}
This char code corresponds to the necessary icon in the font.
Obviously, having the whole icon set as a single file reduces the number of HTTP requests, which is the primary reason to anize it that way.