sayantan bhowmik
sayantan bhowmik

Reputation: 23

Python-Flask not accepting custom fonts

Folder Blueprint

Templates

Static

In the css file , i tried :

@font-face{
font-family:<Font_name>
src:{{ url_for('static',filename='fonts/<font_name>.ttf') }} ;
}

What changes are to be made to add custom fonts ?

Upvotes: 2

Views: 4746

Answers (2)

Strassboom
Strassboom

Reputation: 11

Assuming your project directory is setup such (Arrows = level of directories):
~/
>static/
>>fonts/
>>>MyFont.woff
>templates/
>>index.html
>main.py

the following implementation of a fontface in your index.html's style tag should work:

<html>    
    <head>
        <style>
         @font-face {
            font-family: "MyFont";
            src: url("/static/fonts/MyFont.woff");
        }
        </style>
    </head>
</html>

Upvotes: 1

danidee
danidee

Reputation: 9634

You can't use template tags in css. the Jinja template tags are only meant for html files and templates not css.

To use a css file, you have to insert the link manually there, something along the lines of:

@font-face{
    font-family: customfont;
    src: /static/Fonts/font.ttf';
}

The only way to get around this is to serve a template, that contains the css embedded in <style></style> tags that way flask would be able to interprete the template tags. so with that you should have something like this

<style>
@font-face{
    font-family: <Font_name>
    src: {{ url_for('static',filename='fonts/<font_name>.ttf') }} ;
}
</style>

Upvotes: 4

Related Questions