question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Font bundling for HTML

See original GitHub issue

Currently, icons are bundled as svg files that are inserted by JavaScript in the <i> tag.

As far as I understand, majority of icon packs for web are bundled as fonts instead. This allows them to be seamlessly integrated into different text styles (primarily by matching font size and color).

Lucide-preview.html seems to be doing exactly this. However, there is not documentation on how to use this approach in real web apps.

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ericfenniscommented, Oct 15, 2021

I understand you questions. But I can tell you why we not recommend using icon fonts for web development. There is a reason why icon fonts are still really popular and that’s because icon fonts were there first. Svgs were for long time not supported on every browser so icon fonts was the solution. But nowadays svgs are supported everywhere. The reason why people still use it, i don’t know. maybe it is easier. But there are a lot of good reasons why you should not use icon fonts:

1. You always include everything

If you use the lucide Icon font, you include all the 500+ icons, but you maybe use between 0 - 50 icons in you website (depends on you project). So that’s not very efficient in loading. A lucide icon font file is somewhere between 60-120kb depends on the fontfile type. And you maybe want to load the additional css file for css classes, that’s extra 36kb. And you also add 2 extra http request to you website to have the icons instead of loading one javascript file.

Javascript library

The javascript library is currently on 25kb gzipped if you use all the 500+ icons, (also not ideal). image

But where the magic happens with the lucide Javascript library (or the other official lucide packages) if you use bunders like Webpack, rollup or something else that supports Threeshaking. You can bundle only the icons you use.

Here is an example of using the three-shaking ability of the lucide javascript package:

import { createIcons, Menu, ArrowRight, Globe } from 'lucide';

createIcons({
  icons: {
    Menu,
    ArrowRight,
    Globe,
  },
});

An here you can see the rollup vizualizer that we only include the three icons. Added only 5kb (not gzipped): image The bundled javascript of the whole site only 1.2 kb gzipped: image

The lucide library also doesn’t require an additional css file. If bundle it with you other javascript you don’t add an extra request to you payload.

More about threeshaking: https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking

2. Icon fonts are not very good for accessibility

  • Because icon fonts are threaded as text, screen readers try to read these as well. lead to unexpected behaviour and annoying experience for the people who need this.
  • Stylesheet/font overrides, some users uses their own font to make site more readable for them selfs (Like dislexic people https://opendyslexic.org/), icon font will replaced as wel. Make the icons disappear.

3. Styling icons

Maybe you like to have icons seamlessly integrated with for example fonts-size. But styling icons can be a pain in the ass if you don’t want depend on the styling of the text. For Example simply aligning the icon in the middle of the line requires the use of font-size, letter-spacing, line-height. make it really hard constantly overriding the styling of the text make it look good.

All the lucide icons are shipped with the stroke="currentColor" attribute in the svg element. That means that the svg icon will always have the color bases on the font color that is used.

You can scale icons based on font size by simply using ‘ems’:

svg.lucide {
  width: 1em;
  height: 1em;
}

Here nice article about aligning/scaling svgs with text: https://blog.prototypr.io/align-svg-icons-to-text-and-say-goodbye-to-font-icons-d44b3d7b26b4

4. Unexpected rendering of icons by using fonts

Svgs are rendered as vectors on screen, make it look more sharper on screen then icon fonts (especially hdpi screens, like retina ) Icons fonts are threaded like text and gets extra processing to make the “text” more readable:

Close up image

Glyps can be rendered weirdly, misplaced and not well alligned. image

Extra articles about Font vs SVG.

0reactions
ericfenniscommented, Oct 18, 2021

@marchellodev My pleasure!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Web fonts - Learn web development - MDN Web Docs - Mozilla
In this article we will go further, exploring web fonts in detail. ... html { font-family: "myFont", "Bitstream Vera Serif", serif; }
Read more >
Font bundle & HTML Export - Scrite
To address all this, we now bundle hand picked fonts for Indian Languages into the binary of the Scrite App. That way the...
Read more >
The Best Font Loading Strategies and How to Execute Them
Zach Leatherman wrote up a comprehensive list of font loading strategies that have been widely shared in the web development field.
Read more >
html - How to add some non-standard font to a website?
The way to go is using the @font-face CSS declaration which allows authors to specify online fonts to display ...
Read more >
How To Load and Use Custom Fonts with CSS | DigitalOcean
The text of the index.html file in your browser will be using the browser's local default font. In most cases, this will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found