How do I highlight a link based on the current page?

后端 未结 7 515
日久生厌
日久生厌 2020-12-30 08:21

Sorry if this sounds like a really stupid question, but I need to make a link change colour when you are on the page it links to.

For example, when you are on the \"

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 08:43

    You can do this without having to actually modify the links themselves for each page.

    In the Stack Overflow clone I'm building with Django, I'm doing this:

    
    ...
    
    ...
    
    

    Then filling in the bodyclass like so in page templates:

    
    {% extends "base.html" %}
    {% block bodyclass %}questions{% endblock %}
    ...
    

    Then, with the following CSS, the appropriate link is highlighted for each page:

    body.questions #nav-questions a,
    body.tags #nav-tags a,
    body.users #nav-users a,
    body.badges #nav-badges a,
    body.ask-question #nav-ask-question a { background-color: #f90; }
    

提交回复
热议问题