Home > Web Front-end > CSS Tutorial > Why Isn\'t My External CSS File Loading in My Flask Application?

Why Isn\'t My External CSS File Loading in My Flask Application?

DDD
Release: 2024-11-26 13:41:10
Original
696 people have browsed it

Why Isn't My External CSS File Loading in My Flask Application?

CSS File Not Loading in Flask

Q: I'm trying to use an external CSS file to style my Flask template, but the styles aren't being applied. Here's my file structure:

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /styles
        - mainpage.css
Copy after login

mainpage.html

<html>
    <head>
        <link rel= "stylesheet" type= "text/css" href= "../styles/mainpage.css">
    </head>
    <body>
        <!-- content --> 
Copy after login

A: To resolve this issue, you need to have a dedicated 'static' folder for storing your CSS and JS files. Here's the corrected file structure:

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /static
        /styles
            - mainpage.css
Copy after login

mainpage.html

<html>
    <head>
        <link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">
    </head>
    <body>
        <!-- content --> 
Copy after login

Flask will now look for the CSS file under static/styles/mainpage.css.

The above is the detailed content of Why Isn\'t My External CSS File Loading in My Flask Application?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template