I’m running into an issue with a CSS font @import
. The following works (font gets rendered as expected):
*, *::before, *::after {
margin: 0;
padding: 0;
}
@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600&family=Unbounded:wght@400;500;600&display=swap');
h1, h2, h3, h4, h5, h6 {
font-family: 'Unbounded', cursive;
}
If I do any of this:
- move the
@import
to the top of the file - move the import into the html
<head>
section - delete the
*, *::before, *::after
block
the font doesn’t render any longer.
Example: https://codepen.io/xyzuser/pen/raBvRjK
My goal is to move the import into the <head>
section. What’s the recommended way to fix this issue?
2
Answers
This is Google’s recommended embed code for the
<head>
section for the pair of fonts you’re using:Put that code into a snippet and it works just fine.
In CSS, @import rules must appear at the very top of your stylesheet, before any other style rules, including universal selectors like *. This is part of the CSS specification. If the @import is not at the top, it may be ignored, and your font won’t load.