We use CSS files to style web pages. Serving CSS files means making them available to the browser so the page looks nice.
Serving CSS files in Flask
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True)
Place your CSS files inside a folder named static in your project root.
In your HTML templates, link CSS files using url_for('static', filename='style.css').
style.css from the static folder.<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
project_folder/
static/
style.css
templates/
index.html
app.py<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Flask App</title> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> </head> <body> <h1>Welcome!</h1> </body> </html>
This Flask app serves an HTML page that uses a CSS file from the static folder.
Put style.css inside static/ and index.html inside templates/.
The CSS styles will apply to the page when you open http://localhost:5000/.
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True)
Flask automatically serves files from the static folder at the URL path /static/.
Make sure your CSS file is named correctly and placed inside the static folder.
Use browser developer tools to check if the CSS file loads correctly and styles apply.
Put CSS files in the static folder to serve them with Flask.
Link CSS in HTML using url_for('static', filename='yourfile.css').
This helps keep styles separate and your web pages look better.
Practice
Solution
Step 1: Understand Flask static file serving
Flask automatically serves files placed inside thestaticfolder.Step 2: Identify correct folder for CSS
CSS files must be inside thestaticfolder to be served properly.Final Answer:
Inside thestaticfolder -> Option BQuick Check:
CSS files go instaticfolder [OK]
- Placing CSS in templates folder
- Putting CSS in root directory
- Creating a separate css folder outside static
style.css in a Flask HTML template?Solution
Step 1: Use url_for to link static files
Flask usesurl_for('static', filename='...')to generate correct URLs for static files.Step 2: Correct syntax in HTML template
The link tag must use{{ url_for('static', filename='style.css') }}inside the href attribute.Final Answer:
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> -> Option AQuick Check:
Use url_for('static', filename=...) for CSS links [OK]
- Linking CSS without url_for
- Using 'templates' instead of 'static'
- Incorrect href paths
main.css if the app runs on http://localhost:5000?
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}">Solution
Step 1: Understand url_for output for static files
By default,url_for('static', filename='main.css')returns the path starting with/static/without domain.Step 2: Rendered HTML link tag
The rendered HTML will have href="/static/main.css" which is relative to the server root.Final Answer:
<link rel="stylesheet" href="/static/main.css"> -> Option AQuick Check:
url_for('static', filename=...) returns path starting with /static/ [OK]
- Expecting full URL with domain
- Missing leading slash in href
- Using relative path without /static/
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
But the CSS is not loading. What is the most likely error?Solution
Step 1: Check file location for static serving
Flask only serves files placed inside thestaticfolder automatically.Step 2: Confirm file placement
If the CSS file is outsidestatic, Flask won't find it even if the link is correct.Final Answer:
The CSS file is not inside thestaticfolder -> Option DQuick Check:
CSS must be inside static folder to load [OK]
- Placing CSS outside static folder
- Misspelling filename in url_for
- Using wrong folder name in url_for
reset.css and theme.css. Both are inside the static/css folder. How should you link them correctly in your HTML template?Solution
Step 1: Understand folder structure for static files
Files insidestatic/cssmust be referenced withcss/filenameinurl_for.Step 2: Use url_for with correct filename path
Link tags must use{{ url_for('static', filename='css/reset.css') }}and similarly fortheme.css.Final Answer:
Use url_for with 'css/reset.css' and 'css/theme.css' -> Option CQuick Check:
Include subfolder in filename for nested static files [OK]
- Omitting 'css/' folder in filename
- Hardcoding paths without url_for
- Using wrong folder names in href
Ready to prove it?
Pass the test and this pattern moves unknown
