How to adapt the `linenumbers` filter for different screen sizes on mobile pages?

Calendar 👁️ 74

In AnQi CMS,linenumbersThe filter is a very practical feature that can automatically add line numbers to multi-line text content, which is especially convenient for displaying code snippets, step-by-step instructions, and other scenarios.However, when this content with line numbers is displayed on the mobile page, how to ensure its good readability and layout adaptation is a problem we need to pay attention to in content operation.

UnderstandinglinenumbersHow does the filter work?

Firstly, we need to clarifylinenumbersHow the filter generates line numbers. According to the document description, it addsN.(where N is the line number), for example:

1. This is a simple text.
2. This too, as a paragraph.

This means that the line numbers are directly inserted as part of the text at the beginning of each line, rather than as separate HTML elements (such as<span>or<li>This limits us to control line numbers directly via CSS (for example, only making the line numbers smaller while the text remains unchanged).Therefore, our adaptation strategy will mainly focus on how to manage these text blocks with line numbers as a whole.

Adaptation strategy for mobile screen size

The Anqi CMS provides various modes such as 'adaptive', 'code adaptation', and 'PC+mobile independent site' in template design, which provides us with flexible space for mobile adaptation.

1. Use responsive design (adaptive template pattern)

This is the most commonly used and recommended adaptation method. By using CSS media queries, we can adjust the styles of the line numbers included according to the screen width.

  • Wrap content in a container:Whether the content you are dealing with is a code block or plain text, it is recommended to wrap it in an HTML container with specific semantics, such as a<div>Or<pre>Label, and specify a class name for it, which is convenient for subsequent control through CSS.

    <div class="line-numbered-content">
        {{ archiveContent|linenumbers|safe }}
    </div>
    
  • Handle horizontal overflow (especially for code blocks):For lines containing code or long text, even with line numbers, it may lead to content exceeding the width of the mobile screen, thus breaking the page layout and causing a horizontal scroll bar. To solve this problem, you can useoverflow-x: auto;. So that if the content is too wide, the user can scroll horizontally to view it without affecting the overall page layout.

    /* 在小屏幕设备上应用 */
    @media screen and (max-width: 767px) { /* 示例断点,可根据实际情况调整 */
        .line-numbered-content {
            overflow-x: auto; /* 允许内容水平滚动 */
            white-space: pre-wrap; /* 允许文本在单词间换行,但保留原始空格 */
            /* 也可以考虑其他更适合代码的换行方式,如 white-space: pre; 配合 overflow-x: auto; */
        }
    }
    

    For code blocks, they are usually preservedwhite-space: pre;to maintain the code format, at this timeoverflow-x: auto;It is the main means to prevent overflow. For general text,white-space: pre-wrap;it is allowed to wrap the text automatically without destroying the line number logic.

  • Adjust font size and line height:On small screen devices, the default font size and line number space may make the content appear too crowded, affecting the reading experience.By using media queries, we can appropriately reduce the font size and adjust the line height to enhance the breathability of the content.

    @media screen and (max-width: 767px) {
        .line-numbered-content {
            font-size: 0.85em; /* 减小字体大小 */
            line-height: 1.6; /* 调整行高 */
            padding: 10px; /* 增加内边距,避免内容紧贴屏幕边缘 */
        }
    }
    
  • Consider whether to hide line numbers (not recommended):AlthoughlinenumbersThe filter will embed line numbers directly into the text, but if line numbers on mobile devices indeed cause serious reading difficulties, and you think the benefits of not displaying line numbers on mobile devices outweigh their informational value, you can hide the entire container containing line numbers through media queries.Please note that this means the entire content block will be hidden, rather than just hiding the line numbers.If you only need to hide the line numbers while keeping the content, you will need more complex JavaScript logic to dynamically remove the line number prefix.

    /* 如果确实需要完全隐藏行号及内容 */
    @media screen and (max-width: 480px) {
        .line-numbered-content {
            display: none; /* 隐藏整个内容块 */
        }
    }
    

2. Adapt for different template types

Our AnQi CMS supports different template types, which provides us with finer-grained control.

  • PC+Mobile independent site mode:If your website

Related articles

How to prevent the `linebreaks` filter from wrapping unexpected content (such as phrases) inside `<p>` tags?

In Anqi CMS template design, we often use various filters to conveniently process and display content.Among other things, the `linebreaks` filter is a very practical tool that can intelligently convert line breaks in plain text to HTML paragraph `<p>` tags and line breaks `<br/>` tags, thus presenting more readable formatting on the web.However, some users may encounter such situations during use: Even short phrases or content that should not be used as independent paragraphs may be accidentally wrapped in `<p>`

2025-11-08

In AnQi CMS multilingual site, is the `linebreaks` filter consistent in handling line breaks for different languages?

When setting up a multilingual site on AnQi CMS, content managers often pay attention to a detail: does the `linebreaks` filter perform consistently when handling line breaks in text of different languages?This issue touches on the deep logic of template engine processing mechanism and multilingual content display.A deep understanding of the AnQiCMS template tag and filter functions allows us to clearly state that the `linebreaks` filter maintains a consistent underlying mechanism when handling line breaks in text of different languages.###

2025-11-08

Does the `linebreaks` filter affect Markdown formatted inline code (code)?

In AnQiCMS template creation, flexibly using various filters is the key to improving the display effect of content.Among them, the `linebreaks` filter is a frequently mentioned feature, intended to handle line breaks in plain text.However, when it comes to inline code in Markdown format (`code`), the mechanism of this filter is not very intuitive and may even raise some doubts.

2025-11-08

How can I display the line-numbered plain text logs stored in the database on the admin panel of AnQi CMS?

In website operation, log recording is the key to understanding the operation status of the website, optimizing user experience, and improving SEO performance.When talking about viewing the plain text logs stored in the database of the AnqiCMS management background and hoping to display them with line numbers, it is usually to analyze the website's visit status, error information, or crawling activity more intuitively and efficiently.AnQi CMS with its efficient features and flexible customization capabilities in Go language provides us with the possibility of implementing this requirement.The 'Data Statistics' module of AnQi CMS is the main collection point for all kinds of running data on the website

2025-11-08

What is the behavior of the `linebreaks` filter in the Anqi CMS template when processing hyperlink text?

In AnQi CMS template development, formatting text content is a common requirement, and the `linebreaks` filter is one of the convenient tools.It is mainly responsible for converting newline characters (`\n`) in plain text content to HTML paragraph (`<p>`) and break (`<br/>`) tags, making the layout more in line with web reading habits.However, when it comes to hyperlink text, its behavior has some notable details.**`linebreaks` filter's basic function** First

2025-11-08

How can you use the `linebreaks` filter to beautify the display of multi-line descriptions on the list page of a custom module?

In the custom module list page of the website, the display effect of the introduction directly affects the user's reading experience and the overall beauty of the page.We often encounter such a situation: when we carefully enter multiple lines of introduction in the background, it includes paragraph breaks and line breaks, but when displayed on the front-end page, these formatting information disappears, and all the text is compressed into a mass, making it difficult to read.This is usually because HTML defaults to ignoring extra whitespace and newline characters in text.

2025-11-08

How to use the `linebreaks` filter correctly within the `{% filter %}{% endfilter %}` block?

In the template development of Anqi CMS, we often need to display the plain text content entered in the background, such as article summaries, multi-line descriptions in product details, or user comments, in a way that is more in line with web reading habits.Directly output this plain text, you will find that the original line breaks are lost, and all the content is squeezed together, which seriously affects readability.At this time, the `linebreaks` filter has become a powerful helper in solving this problem, as it cleverly converts newline characters in plain text to standard HTML paragraph and newline tags.

2025-11-08

Why does my `linenumbers` filter only show '1. ' without the subsequent line numbers?

Users of AnQiCMS may occasionally encounter some seemingly strange phenomena when using template filters, such as when trying to use the `linenumbers` filter to add line numbers to article content, but it only displays '1. ' and the subsequent line numbers do not appear.This is not a problem with the filter itself, but is closely related to the HTML rendering mechanism and the way content is processed.### Understanding the role of the `linenumbers` filter Firstly, let's briefly review one

2025-11-08