Ensure custom fonts work in Lovable by configuring CSS properly. Discover how to add and apply fonts using proven best practices.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Understanding the Role of CSS in Applying Custom Fonts
Custom fonts on a web page need instructions on how and where to be used. Instead of browsers automatically showing a new, fancy text, they require clear directions provided by CSS (Cascading Style Sheets). Without these directions, the browser simply shows default text styles.
How the Absence of CSS Configuration Affects Font Application
The process of applying a custom font is similar to giving an instruction on which look to use for text. CSS serves as that instruction manual. Without it, the font file might be available, but the page does not know it should use the new style.
What Does the CSS Code for Custom Fonts Typically Look Like?
Sometimes, developers use a piece of CSS code that tells the browser where the custom font is and what name it should refer to. This code snippet shows how it is typically done:
@font-face {
font-family: 'CustomFont';
src: url('path/to/your/font.woff') format('woff');
}
By not having such CSS instructions in place, the custom fonts are never activated, no matter how elegant they might be.
Step: Upload Your Custom Font Files
fonts
at the root of your project. This folder will store your custom font files.
MyCustomFont.woff2
and MyCustomFont.woff
) into the fonts
folder.
Step: Create a Custom CSS File with Font Definitions
custom.css
in your existing CSS or assets folder (for example, in a folder called assets/css
if available).
custom.css
to define your custom font. This code uses the @font-face
rule to tell your project where and how to load the font:
@font-face {
font-family: 'MyCustomFont';
src: url('/fonts/MyCustomFont.woff2') format('woff2'),
url('/fonts/MyCustomFont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
body {
font-family: 'MyCustomFont', sans-serif;
}
You can place this rule at the bottom of the same custom.css
file.
Step: Link the Custom CSS File in Your HTML
index.html
or the equivalent entry point in your Lovable project).
<head>
section of your HTML, add a link to the custom.css
file so that your font definitions and styles are applied to your website:
Step: Confirm and Test
fonts
folder path in the src
URLs is correct.custom.css
file is correctly linked in your HTML file.
Step 1: Organize Your Font Files
assets/fonts
in your Lovable project. This folder will store all your custom font files like .woff
and .woff2
.YourCustomFont.woff
and YourCustomFont.woff2
).
Step 2: Add Your Custom Font via CSS
styles.css
) in the Lovable code editor.@font-face
rule at the beginning of your CSS file. This rule tells the browser where to find your custom font and how to use it:
@font-face {
font-family: 'YourCustomFont';
src: url('/assets/fonts/YourCustomFont.woff2') format('woff2'),
url('/assets/fonts/YourCustomFont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
src
match the location and names of the files you uploaded. If your folder structure or file names differ, update the URLs accordingly.
Step 3: Apply the Custom Font Throughout Your Project
body
or specific components on your page.
body {
font-family: 'YourCustomFont', sans-serif;
}
Step 4: Integrate the CSS File into Your Lovable Project
@font-face
rule is loaded by your project. Depending on your project structure, this may involve including the stylesheet in your HTML head section or linking it in your project settings.<head>
section of your main HTML file:
Step 5: Troubleshooting and Best Practices
src
URL exactly match those in your project. A small typo can prevent the font from loading.font-family
. In the example above, sans-serif
is used in case the custom font fails to load.@font-face
rule to explain its purpose.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.