CSS Problems with Flask Web App

PythonCssFlask

Python Problem Overview


I can't get the CSS to output correctly - my webpages are all unstyled.

This is my link in all my templates. What am I doing wrong?

<link type="text/css" rel="stylesheet" href="/stylesheets/style.css"/>

Is there anything special that I have to do with Flask to get it to work?

I've been trying and changing things for about half an hour but can't seem to get it right.

To sum it up: How do you do CSS with Flask - do I have to have any special python code?

Python Solutions


Solution 1 - Python

You shouldn't need to do anything special with Flask to get CSS to work. Maybe you're putting style.css in flask_project/stylesheets/? Unless properly configured, such directories won't be served by your application. Check out the Static Files section of the Flask Quickstart for some more information on this. But, in summary, this is what you'd want to do:

  1. Move static files you need to project_root/static/. Let's assume that you moved stylesheets/style.css into project_root/static/stylesheets/style.css.

  2. Change

     <link ... href="/stylesheets/style.css" />
    

to

    <link ... href="{{ url_for('static', filename='stylesheets/style.css') }}" />


This tells the template parser (Jinja2) to tell Flask to find the configured static directory in your project directory (by default, `static/`) and return the path of the file.
  • If you really wanted to, you could just set the path as /static/stylesheets/style.css. But you really shouldn't do that - using url_for allows you to switch your static directory and still have things work, among other advantages.

And, as @RachelSanders said in her answer: > In a production setting, you'd ideally serve your static files via apache or nginx, but this is good enough for dev.

Solution 2 - Python

try reloading the chrome using

ctrl + shift + R

Solution 3 - Python

4 steps to do this (building a lot on some of the other answers here, presuming you've got Flask all set up properly):

  1. Create a static folder in your app folder:[root_folder]/app/static/

  2. Move all of your static content (images, JavaScript, CSS, etc.) into those folders. Keep the content in their respective folders (not mandatory, just neater and more organized).

  3. Modify your __init__.py file in the app folder to have this line:

    app.static_folder = 'static'

This will allow your app to identify your static folder and read it accordingly.

  1. In your HTML, set up your file links as such:

For example, if you call your CSS folder 'stylesheets' and file 'styles':

<link ... href="{{ url_for('static', filename='stylesheets/styles.css') }}" />

That should set everything up properly. Good luck!

Solution 4 - Python

You need to create a folder called "static" inside your Flask app, and then put all your CSS files there.

http://flask.pocoo.org/docs/quickstart/#static-files

In a production setting, you'd ideally serve your static files via apache or nginx, but this is good enough for dev.

Solution 5 - Python

In my case - safari on macOS 10.13 - clearing the cache did the trick.

Solution 6 - Python

ctrl + shift + R trick works, especially if css loaded during the 1st run (that means, all paths are fine); but, not able to reload if changes made to css.

Solution 7 - Python

Try to do a hard refresh in the browser. You might be looking at a cached version. Hold down CTRL and press F5 in firefox.

Solution 8 - Python

I had created static folder put my CSS in it and had properly linked it, but it wasn't working. I cleared browser's cache and it worked. Alternatively you can hard refresh the browser by pressing Ctrl+F5 on chrome.

Solution 9 - Python

Simply hard refresh using (mac OS):

command + shift + R

Solution 10 - Python

"If you change a static file, refresh the browser page. If the change doesn’t show up, try clearing your browser’s cache." -Flask documentation

Solution 11 - Python

The order of handler might cause the problems:

  • url: /stylesheets static_dir: stylesheets
  • url: /.* script: helloworld.application

will work instead of

  • url: /.* script: helloworld.application
  • url: /stylesheets static_dir: stylesheets

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRohit RayuduView Question on Stackoverflow
Solution 1 - PythonNathanView Answer on Stackoverflow
Solution 2 - PythonTanishka GuptaView Answer on Stackoverflow
Solution 3 - PythonLee NgoView Answer on Stackoverflow
Solution 4 - PythonRachel SandersView Answer on Stackoverflow
Solution 5 - PythonheimiView Answer on Stackoverflow
Solution 6 - PythonLaveshView Answer on Stackoverflow
Solution 7 - PythonSabito 錆兎 stands with UkraineView Answer on Stackoverflow
Solution 8 - PythonRohan NagmotiView Answer on Stackoverflow
Solution 9 - PythonAditiView Answer on Stackoverflow
Solution 10 - PythonManuelView Answer on Stackoverflow
Solution 11 - PythonGeorge NguyenView Answer on Stackoverflow