Google Webfonts causes significant shifts in Cumulative Layout Shift (CLS) score due to Flash

Recently, our focus has shifted towards optimizing our website for the new "Core Web Vitals". We have identified issues related to flexbox row (which have been resolved), resulting in a poor CLS score on Desktop. On Mobile, it seems that Google Web Fonts are negatively impacting the CLS score.

By adding the following code snippet, we were able to improve the CLS score to 0. When using Google Web Font, the score is 0.326 (only for mobile, desktop appears fine).

<style>
*:not(i){
    font-family: sans-serif!important;
}
</style>

This is how we are currently utilizing Google Fonts.

<link rel="preconnect" href="https://fonts.gstatic.com/">
<link rel="stylesheet" rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&family=Source+Sans+Pro:wght@400;900&display=swap">

Why do we have both rel="stylesheet" and rel="preload"? We discovered that rel="preload" is not fully supported on browsers like Firefox, leading to incomplete font loading. This issue was mitigated by also including rel="stylesheet".

What have we tried so far?

We experimented with different values for &display=swap such as block, fallback, optional as suggested in various sources. However, these changes did not impact the CLS score, although there seemed to be no FOUT (tested under throttled "Fast 3G").

We also attempted to self-host all fonts, but this approach did not yield positive results either.

We strongly believe that the font usage is linked to the CLS score, as applying the CSS mentioned above results in a score of 0, which is our desired outcome. However, we still require web fonts. Additionally, altering the &display=swap value triggers another "warning" in Lighthouse, advising the use of swap for better user experience, despite causing the CLS problem. Any suggestions?

Here are the results post the CSS change as indicated above.

Score: 94 (Mobile) - 93 (Desktop) = Great..!

https://i.sstatic.net/HIIFx.png

And here are the outcomes without the aforementioned CSS and Google web fonts.

Score: 69 (Mobile) - 91 (Desktop) = Room for improvement on Mobile.. Everything looks good on Desktop.

https://i.sstatic.net/RxNYT.png

Answer №1

Two techniques I've found to address this particular issue are:

Adjust the fallback fonts to mirror the web font and leverage the [CSS Font Loading API] to switch classes upon webfont loading.

To see an example of utilizing the Font Loading API to trigger a callback once a specific font has loaded, check here. You can use a tool like this one to align the fallback font with the web font.

Employ chrome's @font-face descriptors override on the fallback font to match the webfont

The new feature Font descriptors override recently introduced in chrome 87 enables you to achieve the same outcome as option 1, minus the requirement for separate classes and JavaScript - simply target a specific font-family within a @font-face. The only drawback is that this functionality is currently exclusive to chrome version 87.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for customizing the CSS file of a React component imported from a UI framework

As I embark on building a large application utilizing React, I find myself navigating the new territory of React philosophy coming from a background of Css+Js+jQuery methods. One key requirement for my project is a reliable UI framework that aligns with Go ...

Get rid of the dashed outline surrounding the selected button

One of my divs has a button inside that, when clicked, creates an outline around it. Here is what it looks like: https://i.sstatic.net/pEr53.png **Code: ** .slide-arrow { color: #fff; background-color: #9E9997; border: none; padding: 16px 10px ...

Ensuring Consistent HTML5 Page Layout Across Various Browsers Using CSS

Hey everyone, I recently created an HTML5 page and utilized the <section> tag. However, I'm encountering issues with the CSS file I'm using. It seems to work perfectly on Firefox, but the layout is all over the place when viewed on Chrome a ...

Create a mobile-friendly version of the website that retains all the functionality and features of the desktop

Currently, I am in the process of optimizing my website for mobile devices. However, creating a separate subdomain and folder for a new mobile version is proving to be a challenge since it was not originally requested. How can I ensure that the website ap ...

What is the method for adjusting the position of the navbar toggler icon on a small screen in Bootstrap?

In my Bootstrap 4 project, I have created a navbar with the brand logo centered and navlinks on both sides. However, when the screen size is reduced to a small size, the toggler button appears at the center right after the brand logo. I want the toggler bu ...

404 error occurs when Spring-Boot fails to include CSS file through ResourceLocations

My spring-boot application is currently running smoothly on a local computer, but I have encountered an issue. When I run mvn package, none of my CSS or JavaScript files located in /src/main/wepapp/css are included in the jar file created in the target d ...

Best practices for arranging several divs with CSS

I am in the process of creating a main header for several web pages I'm working on. The layout I have in mind includes an image, centered text below the image (all to the left of the main header), a main header that I want to be in the center of the p ...

How can I use JavaScript to show a div upon clicking an input element?

I am looking to make a block div visible when an input field is clicked. <input class="indifferent" type="radio" name="decision" value="indifferent"> Indifferent </br> <div class="input" style="display: none;"> Please assist with our com ...

CSS clearfix doesn't appear to be functioning properly

I've been experimenting with using clearfix instead of the clear both property, but it's not working as expected. Despite following tutorials, the issue persists. How can I troubleshoot this and make it function properly? * { marg ...

Can a list be transformed into a toolbar in a responsive design?

Here is my card with a nav section that includes multiple items (although I've only included one here for simplicity). <div class="col-md-3"> <div class="card"> <div class="card-header"> ...

Floating elements and Internet Explorer compatibility

In my code, I have two divs that are floating correctly in Chrome, Firefox, and Safari but not in Internet Explorer. In IE, the right div appears below the left div even though it should be floated to the right. Both of these divs are wrapped by an outer ...

Ways to expand the border horizontally using CSS animation from the middle

Currently, I am experimenting with CSS animation and I have a query regarding creating a vertical line that automatically grows in length when the page is loaded. I am interested in making the vertical line expand from the center in both upward and downwar ...

Is using 'box-sizing: borderbox' enough to avoid horizontal scrolling?

Currently undertaking The Odin Project, my task was to replicate the Google homepage. However, I have encountered a challenge that I am struggling to resolve. I attempted to use box-sizing: border-box; to prevent the page from extending beyond the width ...

Tips on adjusting the CSS to fix the scrolling issue caused by images in a carousel slider

I am facing an issue where a scroll is being created and it appears offset on different screen sizes: https://i.sstatic.net/KYTCN.jpg I need help to resolve this problem and make it responsive. Even after trying to add overflow: hidden; it still doesn&a ...

Tips for addressing the issue of mat-list-item not occupying the entire row's space

Hello everyone, I am currently trying to render an article.component.html within my article-list-component.html in a list format. When I use plain HTML, it renders correctly as shown in picture 1: Title - author - Date Here is the code for my article-list. ...

Guide to creating a toggle button

Can someone help me create a button that toggles between displaying block and display none when clicked? I've been trying to use Classlist.toggle with JavaScript, but I'm not sure if I have the correct classes targeted. let showCart = documen ...

What causes the discrepancy in CSS behavior between local and remote websites?

My chrome extension enhances facebook chatbox with jquery autocompletion. I am trying to make the suggestion list menu horizontal by modifying the jquery-ui.css. When changing display:block to display:inline, the list becomes horizontal in a local HTML fil ...

Chrome and Firefox failing to render background property in CSS

I encountered an issue with my HTML code that I tested on both Chrome and Firefox. The background color of the first div is not displaying correctly in either browser. .box-orange { // without any position declaration background: orange; heigh ...

display:none !important style elements shown (jQuery)

According to the jQuery documentation on .show() A reminder: If you are using !important in your styles, like display: none !important, you will need to override this style by using .css('display', 'block !important') if you want ...

Transform the check box into a button with a click feature

I've customized a checkbox to resemble a button, but I'm encountering an issue where the checkbox is only clickable when you click on the text. Is there a way to make the entire button clickable to activate the checkbox? .nft-item-category-lis ...