简体   繁体   中英

Python-Flask not accepting custom fonts

Folder Blueprint

Templates

  • File.html

Static

  • Fonts
  • Style

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 ?

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM