Answer a question

I am working on a web application using Flask and Tailwind CSS and can’t import custom fonts. I can build the CSS file but the HTML template isn’t showing the font. How do I get my custom font to import?

I have this in my CSS build file:

@tailwind base;
@tailwind components;
@tailwind utilities;

@font-face {
    font-family: 'Grifter';
    src: url(../../static/Skyer-Regular.otf);
}

Here is a sample of when I try to use my imported font:

<head>
<link rel="stylesheet" href="{{ url_for('static', filename=’mainStyles.css') }}"/>
</head>

<body class = 'bg-black'>      
<div class = "font-pri text-3xl text-center text-white">
Sample Text
</div>
</body>

Answers

I had this exact problem a while ago and have a solution that works. I ended up having to link a separate CSS file to each HTML template. Here is an example of what I did. I have my custom font and CSS file in my static folder. gameStyles.css is my Tailwind CSS file.

<link rel="stylesheet" href="{{ url_for('static', filename='gameStyles.css') }}"/>
<link rel="stylesheet" href="{{ url_for('static', filename='styles2.css') }}"/>

Here is an example of what I would put in styles2.css:

@font-face {
    font-family: 'reg';
    src: url(../static/ObjectSans-Regular.otf);
}

body {
    font-family: 'reg';
}

I tried everything and this was the only thing that worked for me. Hopefully this helps!

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐