Are you the use of bitmap pictures like PNGs and GIFs for icons for your web page? If that is so, you will have spotted they may be able to be relatively hefty in length, particularly in the event that they’re detailed or a lot of. This no longer handiest will increase the document length however too can decelerate your web page, because it calls for a couple of HTTP requests for each and every icon.

Whilst CSS sprites be offering a workaround, they don’t totally clear up the problem of huge document sizes. Some other drawback of bitmap icons is their loss of flexibility and scalability. Enlarging those icons or viewing them on high-resolution displays, like Apple’s retina show, incessantly ends up in a blurry look.

If those problems sound acquainted, it’s time to believe switching to icon fonts for a extra environment friendly and scalable answer.

Exploring the Benefits of Icon Fonts

An icon font is largely a choice of icons packaged right into a internet font, which may also be simply integrated right into a web page the use of the @font-face rule. As highlighted via Chris at CSS-Tips, icon fonts deliver a number of key benefits over conventional symbol icons:

  • Being vector-based, icon fonts are resolution-independent, making sure sharp and transparent show on more than a few display screen resolutions, together with high-resolution displays like retina presentations.
  • Icon fonts are scalable, making an allowance for length changes with none loss in high quality or readability.
  • As they’re fonts, you’ll be able to simply regulate their colour, transparency, text-shadow, and length the use of CSS.
  • They may be able to be animated with CSS3, including a dynamic detail in your web page.
  • The usage of @font-face for icon fonts is broadly supported, even in older browsers like Web Explorer 4 (with .eot).
  • The use of icon fonts ends up in fewer HTTP requests and a smaller total document length.

Right here’s an inventory of a few best loose icon fonts to be had:

All the time make sure that to check and cling to the licensing phrases prior to embedding any icon font to your web page, as an indication of appreciate for the writer’s effort and paintings.

Enforcing the @font-face Rule

As discussed previous, icon fonts are embedded in internet pages the use of the @font-face rule inside CSS. This rule permits us to outline a brand new font-family. Let’s take the ‘Internet Symbols’ font for example:

 @font-face{ 
 font-family: 'WebSymbolsRegular';
 src: url('fonts/websymbols-regular-webfont.eot');
 src: url('fonts/websymbols-regular-webfont.eot?#iefix') layout('embedded-opentype'),
 url('fonts/websymbols-regular-webfont.woff') layout('woff'),
 url('fonts/websymbols-regular-webfont.ttf') layout('truetype'),
 url('fonts/websymbols-regular-webfont.svg#WebSymbolsRegular') layout('svg');
 }

Within the HTML report, to show the icons, we merely use characters that correspond to each and every icon. Somewhat than making use of the font-family globally, we will be able to goal particular aspects via including a novel magnificence, like this:

 
  • h
  • i
  • j
  • A
  • I

Then, again within the CSS, we outline this new font-family for the category we added:

.icon-font {
    font-family: WebSymbolsRegular;
    font-size: 12pt;
}

Demo @font-face

Integrating Icons with Pseudo-elements

Pseudo-elements be offering any other inventive approach to incorporate icons. Believe the next HTML markup as our start line:

	 
  • Answer
  • Answer All
  • Ahead
  • Attachment
  • Symbol

Within the CSS, we will be able to beef up this record via the use of pseudo-elements to show icons prior to each and every merchandise:

ul.icon-font.pseudo li:prior to {
    font-family: WebSymbolsRegular;
    margin-right: 5px;
}

ul.icon-font.pseudo li:nth-child(1):prior to {
    content material: "h";
}

ul.icon-font.pseudo li:nth-child(2):prior to {
    content material: "i";
}

ul.icon-font.pseudo li:nth-child(3):prior to {
    content material: "j";
}

ul.icon-font.pseudo li:nth-child(4):prior to {
    content material: "A";
}

ul.icon-font.pseudo li:nth-child(5):prior to {
    content material: "I";
}

Demo Pseudo-element

Personal Use Unicode

There’s a state of affairs the place as a substitute of embedding icons into usual characters (A, B, C, D, and so forth.), they’re embedded in Unicode Personal Use Spaces to keep away from any clashing amongst characters. This system is utilized by gear like FontAwesome, the place the nature codes are incorporated within the stylesheet like so:

.icon-glass:prior to {
    content material: "f0c6";
}
 

To without delay insert the icon into HTML, the code f0c6 received’t render because it’s no longer a legitimate HTML-encoded personality.

HTML calls for a numerical reference markup to render particular characters. It calls for prefixing the hexadecimal quantity (representing the nature) with an ampersand (&), a hash (#), and an x. For example, f0c6 turns into in HTML. In FontAwesome, this code presentations a paper clip icon, like this:

paper clippaper clip

Demo Unicode

Font Subsetting

When your icon assortment comprises unused characters, you’ll be able to get rid of them via subsetting the font, which additionally reduces the font document length. A handy instrument for that is FontSquirrel’s @font-face generator.

Consult with the generator, upload your font, and make a choice Knowledgeable. Then make a choice Customized Subsetting for extra choices.

font subsetfont subset

For instance, the use of the Sociolico font for social media icons on our web page, we handiest want the icons for Dribble, Fb, and Twitter, represented via d, f, and t. Those characters are entered within the Unmarried Characters box as proven:

font subsetfont subset

Now, you’ll be able to obtain the font, which on this case, has been decreased to an insignificant 3Kb to 5Kb in length. Observe that handiest the characters d, f,, and t will render as icons; another characters will seem as steady letters.

Ultimate Idea

Whilst this tradition excludes the potential for including extremely detailed results like this, it provides a versatile, scalable, retina-ready answer with minimum document length. In case your design can endure the absence of excessive element, this icon-serving means is very really useful.

For the ones desperate to discover additional, beneath are some useful references, along side our demo and supply code to be had for obtain.

See demo
Obtain supply codes

Observe: This put up was once first printed at the fifth of Dec, 2012.

The put up A Information to Higher and Sharper UI Icons with Internet Fonts seemed first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/webfont-icons/

[ continue ]