0

I have two CSS files in my flask applications, main.css file, and profile-pic.css file. I want to apply main.css to every HTML file in the application and profile-pic.css to profile.html only. How can I achieve this in flask?

This is my flask structure:

flask
    static
        main.css
        profile-pic.css
    templetes
        profile-pic.html
        account.html
        ....
2
  • Are you using the template extension method? Like: {% extends 'base.html' %}? Commented Aug 9, 2020 at 17:25
  • yes, I'm using the template extension Commented Aug 10, 2020 at 5:12

1 Answer 1

1

Read about using template inheritance (extension):

https://flask.palletsprojects.com/en/1.1.x/patterns/templateinheritance/

You can put main.css in your base template. In the extended, you can put profile-pic.css, like this:

{% extends "base.html" %}

{% block addl_css %}
    <link rel="stylesheet" href="{{ url_for('static', filename='profile-pic.css') }}">
{% endblock %}
....
....

Make sure you add the new block inside the <HEAD> section in base.html, so it can accept the new block:

{% block addl_css %}
{% endblock %}
Sign up to request clarification or add additional context in comments.

2 Comments

Ya I added the CSS file to my profile-pic.html like you said. But no effect in the browser
And you added {% block addl_css %}{% endblock %} to the <HEAD> section of base.html?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.