Guide to automatically converting Google fonts to base64 for inline use

I am currently in the process of creating my own personal blog using next.js.

While everything is going smoothly with the use of Google Fonts for font styling, I have encountered an issue with initial content shift upon loading. This occurs when a new font is being loaded as not all fonts have the same spacing and sizes.

My main question now is how can I prevent this content shift from happening?

Although many suggestions recommend adding a loading screen until all resources are loaded, I believe that bundling everything directly into the HTML file would be the ideal solution. NextJS already handles inline styles for me using <style/> tags, and next-images takes care of converting small images into base64 format for inline display. So, my concern lies in automatically inlining the fonts without having to manually adjust numerous @font-face declarations every time I want to change the font.

Currently, this is what I am using:

<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet">

What I envision is for NextJS to automatically convert it to something similar to this:

@font-face {
    font-family: 'myfont';
    src: url(data:font/truetype;charset=utf-8;base64,<<copied base64 string>>) format('truetype');
    font-weight: normal;
    font-style: normal;
}

Answer №1

If you're looking to easily incorporate any Google font into your project with the base64 inlined src, this fantastic tool is perfect for you.

inlined-google-fonts

All you have to do is input your desired Google font link here:

For instance, if you want to use the Barlow font, simply insert this link:

After that, you can copy the generated code and create a <your-font>.css file within your project.

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

Content in the Bootstrap carousel is not displaying properly within the arrow indicators

While working on my project and trying to implement a carousel element, I encountered an issue. The carousel arrows and autoplay options are functional, but the content does not appear between the arrows as expected. First, I included bootstrap in my proj ...

Create a log table specifically for tracking changes made to the drop-down menu

I need to create a Change log table that will track any changes made in the drop-down menu. For instance, I am working on a worksheet with a select menu called Results which includes options like Positive, Negative, Unknown. I want the system to log any ch ...

Achieving a Full-Screen Three.js Canvas in React: A step-by-step guide on spanning the view height and width of a webpage

I've been working on tweaking the code found in this particular example: How to connect Threejs to React? This is the snippet of code I am focusing on: import React, { Component } from 'react' import * as THREE from 'three' clas ...

Fashionable selection form

I'm having trouble with styling my select form to have a background-color and border. I've tried applying CSS properties, but it doesn't seem to be working as expected. Here is the code snippet in question: <select class="size"> ...

What is the best way to ensure that the width of dropdown menu items corresponds precisely to the width of the content

I have implemented the Reactstrap drop-down menu: import React from "react"; import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem, } from "reactstrap"; export default class DropDownTest extends React.Component { cons ...

How do I set up a 5 column layout for the home page and switch to a 4 column layout for category pages in Magento?

Is it possible to display 5 columns of products on the Magento home page and only show 4 columns in the category area, or is this a pre-set parameter that applies platform-wide? ...

Tips for syncing HTML Canvas to track the mouse's X and Y position accurately across various screen resolutions

I'm facing an issue with my canvas game where the onclick buttons are redirecting to another menu. However, when I open the Canvas on a monitor with a different resolution, all the X & Y coordinates change and nothing works as expected. Is there a wa ...

The type 'MouseEvent<HTMLButtonElement, MouseEvent>' cannot be matched with the type 'boolean'

Just starting out with TS and running into a problem that TS is pointing out to me. Error: Type '(x: boolean) => void' is not compatible with type '(e: MouseEvent<HTMLButtonElement, MouseEvent>) => void'. Parameters ' ...

Stop objects from shifting while easily applying a border

I have a code that adds a red border around elements when you mouseover them and removes it when you mouseout. However, the elements jump around when the border is added because it changes their dimensions. Is there a way to stop this jumping behavior? ...

Move the object beyond the boundaries of the container that can be scrolled

I'm having an issue where the "item" is draggable, but once I try to move it outside of the scrollable container, it disappears. I've attempted adjusting the z-index, but it doesn't seem to be resolving the problem. Any suggestions on what m ...

Ways to divide the vuetify-text-field-label into two lines

My vuetify text field has a label that is too long for mobile devices. I am wondering if there is a way to automatically wrap the text. I attempted using div and p, as shown in this codepen link <div id="app"> <v-app id="inspire ...

Navigating to both internal and external pages in ReactJS can be accomplished through the use of

I am currently learning React and experimenting with routing users to different pages based on certain conditions. The code snippet below seems to be working, but I have some doubts about whether it is the best approach. Can someone review the code and su ...

Avoid flickering of images by properly handling the object URL when setting the image source in an asynchronous pipe

Implementing a JWT authorized endpoint for images has made it impossible to directly link to image urls in HTML. To work around this issue, we have created an async pipe that loads the image with proper authorization (handled by an HTTP interceptor, not s ...

What could be causing the error message "Error: Error serializing ___ returned from getStaticProps" to appear?

When I make a call inside getStaticProps, I keep encountering the error below and I'm struggling to understand why: Error: Error serializing `.lingo` returned from `getStaticProps` in "/". Reason: `undefined` cannot be serialized as JSON. T ...

Learn the process of importing two components in Nextjs

I have successfully created two components, a header and a footer. I have been able to call the header component directly in the index.js file and it is working fine. You can see the header section displayed below: enter image description here Now, my iss ...

Next.js with Express Application Deployment to Elastic Beanstalk: Troubleshooting Error 502

I am encountering persistent 502 Bad Gateway errors while attempting to deploy a next.js application using express to Electric Beanstalk. 2020/03/02 15:26:28 [error] 8286#0: *172 connect() failed (111: Connection refused) while connecting to upstream, ...

Utilizing the HTML5 Download attribute for linking to external files

I am currently developing a web application for my own personal needs. One feature I would like to implement is the ability to set the download attribute on certain links. However, I have run into an issue where the files to be downloaded are hosted on ex ...

Incorporate npm libraries into vanilla JavaScript for HTML pages

Attempting to incorporate an npm package into plain JS/HTML served using Python and Flask. <script type="module"> import { configureChains, createClient } from "./node_modules/@wagmi/core"; import { bsc } from "./nod ...

Generating dynamic DOM elements using AngularJS can cause issues with the proper functionality of the Jquery .tab() method

My goal is to dynamically generate a list of elements - the first loop is dedicated to listing tabs for the menu, while the second loop involves the div with the tab contents. When I write it statically, everything works perfectly. However, the dynamic app ...

Issues with CSS transitions/animations not functioning properly across different browsers

Take a look at this FIDDLE Link. I'm facing an issue where the animation works smoothly in Firefox, but it's not functioning as expected in Chrome. Kindly open the fiddle in both browsers (Chrome and Firefox), hover over the image, and notice t ...