Bird
Raised Fist0
Flaskframework~20 mins

Serving CSS files in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CSS Serving Mastery in Flask
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
1:30remaining
How does Flask serve CSS files by default?

In a Flask app, you want to add a CSS file to style your pages. Where should you place the CSS file so Flask can serve it automatically?

APlace the CSS file inside a folder named <code>static</code> in your project root.
BPlace the CSS file inside the <code>templates</code> folder.
CPlace the CSS file in the same folder as your main Python script.
DPlace the CSS file inside a folder named <code>css</code> anywhere in your project.
Attempts:
2 left
💡 Hint

Flask has a special folder for static files like CSS and images.

📝 Syntax
intermediate
1:30remaining
Correct way to link CSS in Flask template

You have a CSS file named style.css inside the static folder. Which HTML line correctly links this CSS file in a Flask Jinja2 template?

A<link rel="stylesheet" href="{{ static('style.css') }}">
B<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
C<link rel="stylesheet" href="static/style.css">
D<link rel="stylesheet" href="/static/style.css">
Attempts:
2 left
💡 Hint

Use Flask's url_for function to generate URLs for static files.

🔧 Debug
advanced
2:00remaining
Why is the CSS not loading in this Flask app?

Consider this Flask app structure:

project/
 ├── app.py
 ├── static/
 │    └── main.css
 └── templates/
      └── index.html

In index.html, the CSS is linked as:

<link rel="stylesheet" href="/main.css">

Why does the CSS not load when you open the page?

AThe CSS file must be inside the <code>templates</code> folder to be served.
BThe CSS file name is incorrect; it should be <code>style.css</code>.
CThe href should be <code>/static/main.css</code> or use <code>url_for</code> to generate the URL.
DFlask does not serve CSS files by default; you must write a route to serve them.
Attempts:
2 left
💡 Hint

Static files are served from the /static/ URL path by default.

❓ state_output
advanced
1:30remaining
What is the URL generated by url_for for a CSS file?

Given a Flask app with a CSS file theme.css inside the static folder, what URL does this Jinja2 code generate?

{{ url_for('static', filename='theme.css') }}
A/static/theme.css
B/theme.css
C/staticfiles/theme.css
D/assets/theme.css
Attempts:
2 left
💡 Hint

By default, Flask serves static files under the /static/ URL path.

🧠 Conceptual
expert
2:30remaining
How to serve CSS files from a custom static folder in Flask?

You want to serve CSS files from a folder named assets instead of the default static folder in Flask. Which approach correctly configures Flask to serve CSS files from assets?

ARename the <code>assets</code> folder to <code>static</code> because Flask does not allow custom static folders.
BCreate a route to serve CSS files manually from <code>assets</code> using <code>@app.route('/assets/&lt;path:filename&gt;')</code>.
CSet <code>app.static_url_path = '/assets'</code> after app creation and keep CSS files in <code>static</code>.
DInitialize Flask with <code>app = Flask(__name__, static_folder='assets')</code> and place CSS files inside <code>assets</code>.
Attempts:
2 left
💡 Hint

Flask's constructor accepts a static_folder parameter to change the static files folder.

Practice

(1/5)
1. Where should you place CSS files in a Flask project to serve them correctly?
easy
A. Inside the css folder anywhere
B. Inside the static folder
C. In the root project directory
D. Inside the templates folder

Solution

  1. Step 1: Understand Flask static file serving

    Flask automatically serves files placed inside the static folder.
  2. Step 2: Identify correct folder for CSS

    CSS files must be inside the static folder to be served properly.
  3. Final Answer:

    Inside the static folder -> Option B
  4. Quick Check:

    CSS files go in static folder [OK]
Hint: Put CSS in the static folder for Flask to serve it [OK]
Common Mistakes:
  • Placing CSS in templates folder
  • Putting CSS in root directory
  • Creating a separate css folder outside static
2. Which is the correct way to link a CSS file named style.css in a Flask HTML template?
easy
A. <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
B. <link rel="stylesheet" href="/style.css">
C. <link rel="stylesheet" href="static/style.css">
D. <link rel="stylesheet" href="{{ url_for('templates', filename='style.css') }}">

Solution

  1. Step 1: Use url_for to link static files

    Flask uses url_for('static', filename='...') to generate correct URLs for static files.
  2. Step 2: Correct syntax in HTML template

    The link tag must use {{ url_for('static', filename='style.css') }} inside the href attribute.
  3. Final Answer:

    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> -> Option A
  4. Quick Check:

    Use url_for('static', filename=...) for CSS links [OK]
Hint: Use url_for('static', filename='file.css') in href [OK]
Common Mistakes:
  • Linking CSS without url_for
  • Using 'templates' instead of 'static'
  • Incorrect href paths
3. Given this Flask template snippet, what will be the rendered HTML for linking main.css if the app runs on http://localhost:5000?
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}">
medium
A.
B.
C.
D.

Solution

  1. 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.
  2. Step 2: Rendered HTML link tag

    The rendered HTML will have href="/static/main.css" which is relative to the server root.
  3. Final Answer:

    <link rel="stylesheet" href="/static/main.css"> -> Option A
  4. Quick Check:

    url_for('static', filename=...) returns path starting with /static/ [OK]
Hint: url_for('static', ...) returns path starting with /static/ [OK]
Common Mistakes:
  • Expecting full URL with domain
  • Missing leading slash in href
  • Using relative path without /static/
4. You wrote this in your Flask template to link CSS:
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
But the CSS is not loading. What is the most likely error?
medium
A. The CSS file name is misspelled in the url_for call
B. You forgot to include rel="stylesheet" in the link tag
C. You used url_for('templates', filename='styles.css') instead
D. The CSS file is not inside the static folder

Solution

  1. Step 1: Check file location for static serving

    Flask only serves files placed inside the static folder automatically.
  2. Step 2: Confirm file placement

    If the CSS file is outside static, Flask won't find it even if the link is correct.
  3. Final Answer:

    The CSS file is not inside the static folder -> Option D
  4. Quick Check:

    CSS must be inside static folder to load [OK]
Hint: Check CSS file is inside static folder if not loading [OK]
Common Mistakes:
  • Placing CSS outside static folder
  • Misspelling filename in url_for
  • Using wrong folder name in url_for
5. You want to serve multiple CSS files in Flask: reset.css and theme.css. Both are inside the static/css folder. How should you link them correctly in your HTML template?
hard
A. <link rel="stylesheet" href="/static/reset.css"> <link rel="stylesheet" href="/static/theme.css">
B. <link rel="stylesheet" href="{{ url_for('static', filename='reset.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='theme.css') }}">
C. <link rel="stylesheet" href="{{ url_for('static', filename='css/reset.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='css/theme.css') }}">
D. <link rel="stylesheet" href="static/css/reset.css"> <link rel="stylesheet" href="static/css/theme.css">

Solution

  1. Step 1: Understand folder structure for static files

    Files inside static/css must be referenced with css/filename in url_for.
  2. Step 2: Use url_for with correct filename path

    Link tags must use {{ url_for('static', filename='css/reset.css') }} and similarly for theme.css.
  3. Final Answer:

    Use url_for with 'css/reset.css' and 'css/theme.css' -> Option C
  4. Quick Check:

    Include subfolder in filename for nested static files [OK]
Hint: Include subfolder path in filename for nested static files [OK]
Common Mistakes:
  • Omitting 'css/' folder in filename
  • Hardcoding paths without url_for
  • Using wrong folder names in href
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard