How to highlight the link of the current visited page in the navigation menu?

Calendar 👁️ 66

When a user visits your website, a clear and intuitive navigation menu can significantly enhance their browsing experience.One important optimization is to make the navigation menu intelligently highlight the current page link being visited, so that visitors can clearly know where they are on the website.AnQi CMS provides a simple and powerful way to implement this feature, without complex development, just a clever configuration in the template.

Understanding the navigation mechanism of Anqi CMS

The Anqi CMS is built-in with a flexible template tag system, the core of which is the "navigation list tag" for handling navigation menusnavListThis label can easily obtain data from the navigation items configured in the background. Each one passes throughnavListThe navigation items obtained by the tag all contain a series of useful properties, the most crucial of which isIsCurrent.

IsCurrentIs a boolean property, its value is true only when the current visitor is browsing the page linked by the navigation itemtrueWhether it is top-level navigation or secondary sub-navigation, Anqi CMS can intelligently judge and assign valuesIsCurrentThis provides great convenience for us to achieve highlighting effects.

Implement: Highlight the current page link

Next, let's see how to useIsCurrentproperty to highlight the current page link.

Step 1: Locate the navigation menu template file

Generally, the navigation menu code of a website is placed in a public template file, such asheader.html/base.htmlOrpartial/nav.htmlWait. You need to find the part containing the navigation menu code according to the specific structure of the template you are using.AnQi CMS encourages modular template design, so this is usually an easily found code snippet.

Second step: usenavListLabel to get navigation data

After finding the template file corresponding to the navigation menu, you will see the code usingnavListtags to render the navigation, the general structure is as follows:

{% navList navs %}
    <ul class="main-nav">
        {% for item in navs %}
            <li>
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {# 这里可能还有二级导航的处理 #}
            </li>
        {% endfor %}
    </ul>
{% endnavList %}

Third step: useIsCurrentattribute to add highlighted style

Now, we will add to the navigation item's<li>or<a>tag based onitem.IsCurrentproperty to dynamically add a CSS class, such asactive.

{% navList navs %}
    <ul class="main-nav">
        {% for item in navs %}
            <li {% if item.IsCurrent %}class="active"{% endif %}>
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {% if item.NavList %} {# 处理二级导航,同样应用 IsCurrent 判断 #}
                    <ul class="sub-nav">
                        {% for subItem in item.NavList %}
                            <li {% if subItem.IsCurrent %}class="active"{% endif %}>
                                <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                            </li>
                        {% endfor %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endnavList %}

In this code, we checked each navigation item (including the main navigation itemitemand sub navigation itemssubItem)的IsCurrentproperty. If this property istrue, we will add a<li>label toactiveClass.

Fourth step: Define highlight style

The final step is to define in your website's CSS fileactiveThe class style. This style determines the specific display of the highlight effect, such as changing the text color, background color, or adding underlines, etc.

You can customize the theme of the website.style.cssfiles (usually located in/public/static/css/Add the following CSS code:

/* 主导航的高亮样式 */
.main-nav .active > a {
    color: #007bff; /* 例如,高亮显示为蓝色文字 */
    font-weight: bold; /* 加粗 */
    border-bottom: 2px solid #007bff; /* 添加下划线效果 */
    /* 您可以根据设计需求添加其他样式 */
}

/* 子导航的高亮样式 */
.sub-nav .active > a {
    color: #ffffff; /* 例如,子导航高亮为白色文字 */
    background-color: #0056b3; /* 蓝色背景 */
    /* 其他高亮样式 */
}

Adjust the color, font, and other style properties according to your website design to ensure that the highlight effect is coordinated with the overall style.

Advanced Usage and Precautions

Of Security CMSIsCurrentThe property is designed very intelligently. It is not only suitable for directly linking to the homepage, article list page, or single page navigation items, but even when the navigation item points to a category, and the user actually accesses a specific article under that category,...IsCurrentIt can accurately identify and highlight the corresponding parent category navigation item. This means that you do not need to configure the highlight logic separately for each article page, as the system will automatically handle it for you.

After you modify the navigation structure or template file, if the front-end page does not update in time, please try to clear the system cache of Anqi CMS. This ensures that the latest configuration and template code takes effect.

By following these steps, you can easily implement the smart highlight function of the navigation menu in the Anqi CMS-built website, providing a more friendly and professional browsing experience for visitors.

Frequently Asked Questions (FAQ)

Q1: Why did the navigation item not highlight even though I followed the steps?A1: There could be several reasons. First, please check if your CSS file is loaded correctly, as well asactiveWhether the class style has been defined. Secondly, make sure you have used it correctly in the template{% if item.IsCurrent %}class="active"{% endif %}such conditional statements and applied them to the correct HTML tags (usually<li>or<a>)。Finally, if the website has enabled caching, remember to click "Update Cache" in the background to clear the old cache data.

Q2:IsCurrentDoes the attribute support multi-level navigation? If my navigation has two or three layers, will it still work properly?A2: Yes,IsCurrentThe attribute was designed with multi-level navigation scenarios in mind. As long as your navigation structure is correctly configured in the Anqie CMS backend navigation settings, and your template is like the example in the article, that is, toitem.NavList(Sub-navigation list) was traversed in a loop,IsCurrentThe attribute can accurately determine the current page at each level of navigation and assign values.

Q3: BesidesactiveThis class name, can I use other style class names to achieve highlighting?A3: Of course.activeThis is a commonly used example class name. You can use any other class name according to your CSS naming conventions or personal preference, for examplecurrent-page/highlightedetc. You just need to place it in the template.class="active"Replace with the name of the class you have chosen and define the styles for that class in your CSS file.

Related articles

How to create and display multi-level navigation menus in AnQiCMS?

In website operation, a clear and effective navigation menu is the core of user experience, which can help visitors quickly find the information they need.AnQiCMS (AnQiCMS) knows this well and provides flexible and powerful features for you to easily create and manage multi-level navigation menus, whether it is a simple two-level dropdown menu or a complex menu combined with dynamic content display, it can meet your needs. Next, we will learn together how to create and display multi-level navigation menus in AnQiCMS.### One, Back-end Management

2025-11-08

How to display 'Previous article' and 'Next article' links on the article detail page?

In website content operation, optimizing the reading experience of users has always been one of our core goals.When immersed in an excellent article, it is natural to want to smoothly switch to related or the next article without having to return to the list page to find it.This seamless navigation can not only effectively improve the user's stay time on the website, reduce the bounce rate, but also help search engines better understand the structure of the website content, thereby having a positive impact on SEO.Aqy CMS provides a very intuitive and easy-to-use solution for this

2025-11-08

How to sort the list display according to the creation time or view count of the articles?

In website content operation, the presentation sequence of content is crucial for user experience and information delivery efficiency.Whether it is a news portal, technology blog, or product showcase website, reasonably adjusting the sorting method of the content list according to different operational goals can significantly enhance the vitality and attractiveness of the website.AnQiCMS (AnQiCMS) is well-versed in this, providing intuitive and powerful features that allow us to easily sort the content list by the creation time or number of views.###

2025-11-08

How to implement the pagination display function of the article list in AnQiCMS?

The pagination feature of the article list is crucial for any content management system, as it not only improves the user browsing experience and avoids the slow loading caused by overly long single pages, but also helps search engines better crawl and index website content.In AnQiCMS, implementing this feature is both flexible and efficient, thanks to its concise template tag design. Next, we will together learn how to easily implement pagination display of article lists in AnQiCMS.

2025-11-08

How to set and display the common areas of the website such as header, footer, and sidebar in AnQiCMS?

In website operation, the header, footer, and sidebar, etc., play a crucial role.They not only carry the brand image and navigation function, but also the key to improving user experience and content distribution efficiency.For those who use AnQiCMS, understanding how to efficiently set up and display these areas will greatly enhance the professionalism and operational convenience of the website.AnQiCMS as an enterprise-level content management system based on the Go language, has fully considered the flexibility and scalability of templates from the very beginning.This means, whether it is to build a simple corporate website

2025-11-08

How to correctly display the breadcrumb navigation path on a web page?

Elegantly configure breadcrumb navigation in AnQiCMS: Improve user experience and SEO performance When you visit a website, breadcrumb navigation is like a considerate guide, it can clearly tell you where the current page is in the website structure.From the home page all the way to the page you are browsing, it not only helps users quickly understand the website hierarchy, but also makes it convenient for them to backtrack to the upper page.For the website's search engine optimization (SEO), a reasonable breadcrumb navigation can also help search engines better understand the website structure, thereby possibly bringing better crawling and ranking effects

2025-11-08

How to set and display the SEO title, keywords, and description (TDK) of a website in AnQiCMS?

When building a website, Search Engine Optimization (SEO) is the key to ensuring that content is discovered by search engines and attracts potential visitors.And the SEO title (Title), keywords (Keywords), and description (Description), abbreviated as TDK, are one of the most basic and most important elements in website optimization.They directly affect the display and click-through rate of websites in search results.

2025-11-08

How to configure and display a canonical URL to optimize search engine crawling?

It is crucial to ensure that search engines efficiently and accurately crawl and understand our content in website operation.Among them, the canonical URL is an indispensable SEO strategy.It can help us solve the problem of duplicate content, concentrate the page weight, and thus optimize the website's performance in search engines.AnQiCMS as a SEO-friendly and powerful content management system naturally also provides comprehensive specification link configuration options, allowing users to easily manage this important function.What is the canonical link

2025-11-08