search
HomeWeb Front-endCSS TutorialA closer look at CSS counters

A closer look at CSS counters

Oct 21, 2020 pm 05:21 PM
csscounter

A closer look at CSS counters

The css counter effect refers to using CSS code to achieve the effect of increasing the value as the number of elements increases. It is somewhat similar to

    , but is more flexible than ol.

    (Recommended tutorial: css video tutorial)

    CSS counter has two properties (counter-reset and counter-increment) and one method (counter( ) / counters()), as explained below:

    1. counter-reset

    The attribute counter-reset, as the name suggests, means counter-reset. In fact, its main function is to give the counter a Name, if possible, tell me which number to start counting from. The default is 0. Note that the default is 0, not 1. You may see many examples on the Internet where the first number displayed by default is 1, not 0. , this is due to the influence of counter-increment, which will be explained in detail later

    Let’s look at a simple example first

    <div>下面将出现的数字</div>
    <div class="counter"></div>
    .counter {
        counter-reset: resetname 2;
        font-size: 24px;
        color: #f66;
    }
    .counter:before {
        content: counter(resetname);
    }

    A closer look at CSS counters

    If the 2 after conter-reset Remove it, and the number that appears below is 0

    Counter-reset can be a complex number, such as -2, or a decimal, such as 2.99. However, neither IE nor FireFox understands this, so I think It is an illegal value and will be treated as 0. Under Chrome, any decimal is rounded down. For example, 2.99 will be treated as 2.

    You thought it was over here? Of course not! counter -reset has another advantage, which is to name multiple counters at the same time, such as:

        .counter {
            counter-reset: first 2 second 3;
            font-size: 24px;
            color: #f66;
        }
        .counter:before {
            content: counter(first);
        }
        .counter:after {
            content: counter(second);
        }

    A closer look at CSS counters

    In addition, counter-reset can also be set to none, and inherit, cancel the reset and Inherit reset.

    2. counter-increment

    The attribute counter-increment, as the name suggests, means counter increment. The value is one or more keywords of counter-reset, which can be followed by a number. Indicates the change value of each count. If omitted, the default change value 1

    CSS counter technology has its own set of rules, which we call "Pu Zhao Rules". Specifically, Pu Zhao Source (counter -reset) unique, every counter-increment, the counter-increment increases the count

    , so the "default value 0" problem mentioned above can be solved. Usually when we use counters, They all use counter-increment, which must be used, otherwise how can they be incremented.

    .counter {
        counter-reset: incerment 2;
        counter-increment: incerment;
        font-size: 24px;
        color: #f66;
    }
    .counter:before {
        content: counter(incerment);
    }

    A closer look at CSS counters

    This universal element can also be written directly to the element, and the effect is the same as above. It is also incremented by 1. If both the parent element and the child element are written, then the parent element is incremented once, the child element is incremented once, and the final result is incremented twice.

    As mentioned before, this change value is not necessarily the same 1. It can be set flexibly. For example, the change value of

    counter-increment: incerment 2;

    can also be a negative number. For example:

    .counter {
        counter-reset: incerment 5;
        counter-increment: incerment -2;
        font-size: 24px;
        color: #f66;
    }
    .counter:before {
        content: counter(incerment);
    }

    A closer look at CSS counters

    The value can also be none and inherit

    3. counter()/counters()

    These two are methods, not attributes, similar to calc() in CSS3. The function here is to display the count, but there are multiple names and usages

    For example, the counter(name) used above is to display the count.

    It can also be written as counter(name, style)

    So what is this style and the key points it supports? Words are those supported by list-style-type. Its function is that our increments and decrements are not necessarily numbers. They can also be English letters or other

    list-style-type:

    disc | circle | square | decimal | decimal-leading-zero |
    lower-roman | upper-roman | lower-greek | lower-latin | upper-latin |
    armenian | georgian | none | inherit

    .counter {
        counter-reset: styleType 2;
        font-size: 24px;
        color: #f66;
    }
    .counter:before {
        counter-increment: styleType;
        content: counter(styleType, lower-roman);
    }

    A closer look at CSS counters

    counter also supports cascading, that is, a content attribute can have multiple counter() methods

    .counter {
        counter-reset: cascaderOne 2 cascaderTwo 3;
        font-size: 24px;
        color: #f66;
    }
    .counter:before {
        content: counter(cascaderOne) &#39;\A&#39; counter(cascaderTwo);
        white-space: pre;
    }

    A closer look at CSS counters

    The counters() method is introduced below. It seems to have more s than counter, but it has a different meaning. Counters can almost be said to be synonymous with nested counters.

    We usually don’t write They may all be 1, 2, 3, ..., and there are similar serial numbers such as 1.1, 1.2, 1.3... etc. The former is what counter() does, and the latter is what counters() does

    Basic usage of counters

    counters(name, string, style);

    The string parameter is a string, which needs to be enclosed in quotation marks. It is a required parameter and represents the connector of the sub-serial number. The style is still the same as the second parameter of counter

    A complete demo below:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>content</title>
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
            }
            ul, li {
                list-style: none;
            }
            .reset { 
                padding-left: 20px; 
                counter-reset: fe;
            }
            .counter:before { 
                content: counters(fe, &#39;.&#39;) &#39;. &#39;; 
                counter-increment: fe;
            }
        </style>
    </head>
    <body>
    <div class="reset">
        <div class="counter">前端开发FE
            <div class="reset">
                <div class="counter">前端开发FE111</div>
                <div class="counter">前端开发FE222
                    <div class="reset">
                        <div class="counter">前端开发FEsss</div>
                        <div class="counter">前端开发FE</div>
                        <div class="counter">前端开发FE</div>
                    </div>
                </div>
                <div class="counter">前端开发FE3333</div>
            </div>
        </div>
        <div class="counter">后端开发</div>
        <div class="counter">PM
            <div class="reset">
                <div class="counter">瞎提需求</div>
            </div>
        </div>
    </div>
    </body>
    </html>

    A closer look at CSS counters

    相关推荐:CSS教程

The above is the detailed content of A closer look at CSS counters. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software