Pre-loading fonts - css

Do i need to use both rel="preload" and font-display="swap". Since rel="preload" prefetches the required font, is it necessary to add font-display attribute too?
<link rel="preload stylesheet" href="https://fonts.googleapis.com/css2?family=Magra&display=swap" as="font">

It depends on the intended result.
With font-display: swap, text is immediately rendered using the fallback typeface specified in your font stack while the request is made for your font. The fallback is whatever system font comes next in your font stack as listed in your font-family property.
Once the font is downloaded, it will be swapped in for the fallback.
This is just one option and you don't need to use it. For example, the immediate rendering of text when using font-display: swap creates a flash of unstyled text (FOUT) for the user and possible shifts in layout when the fonts swap, which can be undesirable.
You could opt for font-display: fallback, which attempts to account for this by hiding any text on screen for the first 100ms, optimistically hoping that your font downloads in that time frame.
If your font downloads within the first 100ms, it gets applied to the first visible text shown to the user (no FOUT).
If not, at 100ms the text gets rendered with the fallback typeface and the same pattern as font-display: swap resumes with your font getting swapped in once it eventually downloads.

Related

CSS `font-display: swap` is it possible to choose the fallback-font?

I do use font-display: swap to prevent the FOIT- Flash of invisible Text-Effekt.
But if I do so, I see first a serif font (used as first loaded font) and finally I see the webfont tha was loaded:
I'd like to use arial as default for the swap-functionality as this might not be so notable as it is with the serif-font-family.
Is there a way to force the font-family which is used for swap-Fallback?

Google fonts and font-display

We are currently loading a couple of fonts on our site from google fonts using the following link
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons%7CRoboto:300,300i,400,400i,500,700&display=block">
As you can see, at the end of the query string, we have display=block to stop the font from rendering before it's loaded (as it adds font-display: block). However, this adds block to all declarations of every font face. Is there a way to only add this to the material icons font without having to make 2 separate requests?

Why chrome shows rendered font which is different to computed font family?

I would like to know how chrome chooses which font to render? I am asking this because using Chrome Developer Tools I can see that the font family computed is different from the font family rendered and this is confusing. Similar questions on Stackoverflow was not of much help in this particular instance.
My computer font family looks like:
font-family: museo-sans, sans-serif, Futura;
Rendered font looks like:
Helvetica—473 glyphs
In this article, it is mentioned that chrome maps a rendered font to a the computed font listed. What does this mean exactly and why does it do that? Is there a way to control which font is rendered?
Same as any other browser: if it can't find the first font, it tries the next, and so on and so on until it runs out of rules. If no fonts match, then the font is inherited from the parent element, all the way up to the document level, where it'll just pick the browser's default font.
In this case, things are a bit weird, because the order you're showing is "a real font" followed by "a generic CSS class that always resolves, but without any guarantee as to which font that will be, just that it'll be a sans-serif font", followed by the real font "futura".
So Chrome will try museo, won't find it, sees the generic "sans-serif" and just picks a known sans-serif font for you. Usually that's something like Arial or Helvetica, but the CSS spec doesn't say anything about which font it has to be, specifically. It just needs to be a sans-serif font.
The weird part here is that the ordering you chose means that the "futura" at the end will never be checked. The browser will always find a suitable font once it hits serif, sans-serif, cursive, fantasy, or monospace

How does CSS font-weight work with emojis?

I'm blocked by a question, on some Android device, I type some emoji and save them to database.Then load them and display in html page, but the emoji is invisible if set a font-weight: bold.If set font-weight: normal can display.
How does css font-weight work?
How does the browser parse CSS when it sees font-weight:bold or font-weight:normal?
System will call a bold font file (e.g., see Google Fonts which will list different weights of a font family) and if you don't have that font file on your device, it will fail to display the characters (actually for regular characters system tend to load a similar font in this case, but emoji characters are not regular).
You can include webfonts and contain the bold family in <head> section. E.g.,
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>

How to change default font before font-face loads?

In Firefox when a page loads it shows a default font (such as Times New Roman) for a moment (depending on connection speed) before it renders using the specified font-face. I understand this cannot be faster but how can I set, for example, Arial as the default font before it changes when the font-face loads?
Specifying an extra font in your font-family property will solve your problem.
font-family: "YourFontFaceFont", arial;
This will cause arial to be used until "YourFontFaceFont" is available.

Resources