Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS font-family fallback for icon font?

My IDE inspects my CSS files, and complains if I put a font-family rule which does not have a generic fallback. In general, I have to agree with my IDE, and I will happily add the font callback.

Example:

.selector {
  font-family: Arial;  /* IDE complains. */
  font-family: Arial, sans-serif;  /* IDE is happy. */
}

However, sometime the font-family is an icon font (I think fontawesome is one of those), and a fallback does not really make sense. Or does it?

.my-icon {
  font-family: 'my-icon-font';  /* IDE complains. */
  font-family: 'my-icon-font', serif;  /* IDE is happy, but it does not make sense. */
}

Question

Could there be any sensible fallback that would make sense to append to a font-family rule with an icon font?

Additional motivation

In my case, it is mainly my IDE that is nudging me to add a generic font callback. As a last resort I could disable, suppress or ignore this inspection.

However, in other teams there might be strict rules about code conventions, perhaps even a mechanism that blocks commits if they do not comply.

Or what if I am the author of a code inspection tool, or in the process of defining the coding conventions to be used in a project? Then I definitely want to know what would be the "correct" or smartest way to do this :)

like image 461
donquixote Avatar asked Jun 06 '19 00:06

donquixote


People also ask

What is fallback font in CSS?

So basically fallback fonts are used when the current font isn't available. For example, if your CSS selector looked like this: h1 { font-family: 'Roboto', 'Open Sans', Helvetica, Arial, sans-serif; }

How do I change my fallback font?

Steps to change the default fallback font for editing text: Go to Edit > Preferences > Content Editing > Font Options.

What are icon fonts CSS?

Icon fonts are fonts that contain symbols and glyphs instead of letters or numbers. They're popular for web designers since you can style them with CSS the same way as regular text. Also, since they're vector's they're easily scale-able.


2 Answers

As discussed in the comments, this is a bogus warning.

As you point out yourself, there is no generic font family that is suitable as a fallback for an icon font - nor can there be, as icon fonts are not standardized.

The relevant spec (CSS Fonts Module Level 3) only lists the following generic families:

  • serif
  • sans-serif
  • cursive
  • fantasy
  • monospace

None of these is suitable - as they are all explicitly not intended for icon/picture fonts.

So you should probably find a way to suppress the warning. Ideally, the system would allow you to add a list of fonts that the warning should be suppressed for (such as icon fonts). Otherwise, you'll just have to suppress the warning entirely.


Side note: Fallback fonts are only really necessary if you want to use a font that is installed on the user's computer - such as standard Windows fonts like Arial. In that case you need a fallback, because you cannot be sure what the user has installed.

If you use a web font that the browser can download, there should never be a need for a fallback font. It is still nice to include in case the font download fails, but IMHO not as important.

like image 162
sleske Avatar answered Oct 13 '22 13:10

sleske


You can add "fantasy":

.my-icon {
  font-family: 'my-icon-font', fantasy;
}
like image 29
mtonev Avatar answered Oct 13 '22 12:10

mtonev