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
DDDOriginal
2024-11-26 13:41:10749browse

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

mainpage.html

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

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

mainpage.html

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

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!

Statement:
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