Questions & Answers

How to add css files to your Flask Application?

I have a flask application that runs fine but I want to add my own css files to make it more custom.

Here is my project structure


Here is my index.html:

{% extends "bootstrap/base.html" %}
{% block html_attribs %} lang="en"{% endblock %}

{% block title %} My_APP {% endblock %}

{% block styles %}
<link rel="stylesheet"  type="text/css" href="{{url_for('static', filename='style.css')}}">
{% endblock %}

{% block navbar %}
<div class="navbar navbar-expand-lg navbar-dark bg-dark" style="background-color:DodgerBlue" role="navigation" >
    <div class="containter">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle"
             data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle Navigation</span>
                <span class="icon-bar">Toggle Navigation</span>
                <span class="icon-bar">Toggle Navigation</span>
                <span class="icon-bar">Toggle Navigation</span>
            <a class="navbar-brand" href="{{ url_for('home_page')  }}">ISE App</a>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li><a href="{{ url_for('home_page')  }}"> Home </a></li>
                <li><a href="{{ url_for('add_mac') }}"> Add </a></li>
                <li><a href="{{ url_for('upload_mac')  }}"> Bulk Load </a></li>
                <li><a href="{{ url_for('delete_mac') }}"> Delete </a></li>
                <li><a href="{{ url_for('bulk_remove') }}"> Bulk Remove </a></li>
{% endblock %}

{% block content %}
<div class="containter">
    <h3 id="testing">Hi how are you!!</h3>
    {% block page_content %}<h3 id="testing1">Hi how are you!!</h3>{% endblock %}
{% endblock %}

Here is my css file named style.css

#testing {color:blue;}
#testing1 {color:red;}


Here is my page output:

enter image description here

As you can see, the two h3 tags are not blue or red and the containter background color is not red.

Any idea as to why my index.html is not references my style sheet? I am new to flask so any ideas or suggestion would be highly appreciated.

2023-01-21 00:10:07
Show {% block styles %} in bootstrap/base.html
2023-01-21 00:10:07
Check element tab in your browser to see if the css file is loaded successfully.
Answers(1) :


{% block styles %}
    <link rel="stylesheet"  type="text/css" href="{{url_for('.static', filename='style.css')}}">
{% endblock %}