Flask style.css not loading from static/css/style.css
Solution 1
Had the same mistake and solved it in a very funny way after I had read your posts. I started my app and started researching by right-clicking on my Flask website from my browser. After a few seconds, inexplicably, the CSS picked up.(Cache?!?!)
I hope this helps
Solution 2
Clearing your browser cache solves this instantly
On Google Chrome:
- Go to the three dots on the top right-hand corner
- Go to Settings
- Click on "Privacy and security"
- Click on "Clear browsing history"
- Select the checkbox labelled "cached images & files" and uncheck "Browsing history" & "Cookies and other site data"
- Reload your page (make sure your server is running)
- Your CSS should be displaying properly now
Following similar steps on other browsers should resolve this as well.
However, for every new change, you make in your CSS files, you'll have to repeat these same steps to clear your browser cache
Solution 3
HTML is not quite XML, and sometimes things aren't as neat. You're using a self-closing tag - don't. Try removing the terminating /
:
<link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet">
Many browsers are pretty forgiving and will parse <link … ></link>
or <link … />
just fine, but it's not guaranteed. The standard format is without.
Alternatively, you should confirm whether the issue is loading the CSS file, or the actual content of your CSS document.
Solution 4
In Flask Bootstrap, to add your custom CSS, you have to call the super function in the block styles.
{% extends "bootstrap/base.html" %}
{% block styles %}
{{super()}}
<link rel="stylesheet"
href="{{url_for('static', filename='css/style.css')}}">
{% endblock %}
You can read more on the docs
Admin
Updated on June 17, 2022Comments
-
Admin almost 2 years
Here is the directory structure of my python/flask application.
application templates/ static/css/style.css static/js/appjs.js
In my "head" i have added this.
<link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet" />
JS file working fine but style.css is not working. But style.css is accessible through the url and also available in page source view
http://localhost:5000/static/css/style.css
-
Admin over 7 yearsThanks for answer, removing / is not making any difference to output
-
daveruinseverything over 7 years@imdrupal what HTML is rendered in the browser? If you view the page source, can you open your CSS file? Perhaps your CSS file was loading all along, but the CSS itself isn't behaving as expected.
-
Admin over 7 yearsYes, I am able to see my css in page source view and also able to open in browser but the CSS itself isn't behaving as expected(as u said)
-
daveruinseverything over 7 years@imdrupal in that case this isn't really a python issue, it's a CSS issue. I'd suggest looking at the CSS itself, and posting a new question with your example CSS, HTML, and expected behaviour vs observed behaviour.
-
Mateusz Korycinski over 7 yearsDon't you have to load static into template first? Maybe you forgot about it? Just popping ideas out of the top of my head. Django would give you error / warning, I am not sure how exactly it is with Flask.
-
Admin over 7 yearsThe css is fine i have tested with, <style type=text/css>{{ get_resource_as_string('static/css/style.css') }}</style>.
-
Admin over 7 yearsbut in above case it is serving as embed css on source view of page, but working
-
kae_screechae over 4 yearsIve had the same issue and surprisingly, this is the way to go. It seems flask doesn't pick up immediately on changes that you've made to your
style.css
. I tried everything, stopping n restarting the server, closing the app, commenting and uncommenting out the link. However, I just cleared my browser cache and it picks up the changes immediately. -
ThomasATU over 3 years{{super()}} <link rel="stylesheet" href="{{url_for('static', filename='css/style.css')}}"> Did the job! Thanks!
-
G M over 3 yearsUnfortunately it does not :-(
-
StarbuckBarista over 3 yearsThis is such an underrated solution, I've been trying to fix this for days now. Thank you so much!
-
Nuclear03020704 about 3 yearsFirefox users: Ctrl + Shift + R reloads the page and clears the cache, effectively reloading the stylesheet.