Thuộc tính đặt lại bộ đếm CSS
Ví dụ
Tạo một bộ đếm ("my-sec-counter") và tăng nó lên một cho mỗi lần xuất hiện của bộ chọn <h2>:
body {
/* Set "my-sec-counter" to 0 */
counter-reset: my-sec-counter;
}
h2::before {
/* Increment "my-sec-counter" by 1 */
counter-increment:
my-sec-counter;
content: "Section "
counter(my-sec-counter) ". ";
}
Hãy tự mình thử »Thêm ví dụ "Hãy tự mình thử" bên dưới.
Định nghĩa và cách sử dụng
Thuộc tính counter-reset
tạo hoặc đặt lại một hoặc nhiều bộ đếm CSS.
Thuộc tính counter-reset
thường được sử dụng cùng với thuộc tính counter-increment và thuộc tính content .
Giá trị mặc định: | không có |
---|---|
Thừa hưởng: | KHÔNG |
Hoạt hình: | KHÔNG. Đọc về hoạt hình |
Phiên bản: | CSS2 |
Cú pháp JavaScript: | object .style.counterReset="section" Hãy thử nó |
Hỗ trợ trình duyệt
Các số trong bảng chỉ định phiên bản trình duyệt đầu tiên hỗ trợ đầy đủ thuộc tính.
Property | |||||
---|---|---|---|---|---|
counter-reset | 4.0 | 8.0 | 2.0 | 3.1 | 9.6 |
Cú pháp CSS
counter-reset: none| name number |initial|inherit;
Giá trị tài sản
Value | Description |
---|---|
none | Default value. No counters will be reset |
id number | The id defines which counter to reset. The number sets the value the counter is reset to on each occurrence of the selector. The default number value is 0 |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Thêm ví dụ
Ví dụ
Tạo một bộ đếm ("my-sec-counter") và giảm nó đi một cho mỗi lần xuất hiện của bộ chọn <h2>:
body {
/* Set "my-sec-counter" to 0 */
counter-reset: my-sec-counter;
}
h2::before {
/* Decrement "my-sec-counter" by 1 */
counter-increment:
my-sec-counter -1;
content: "Section "
counter(my-sec-counter) ". ";
}
Hãy tự mình thử »Ví dụ
Đánh số các phần, các phần phụ bằng “Section 1:”, “1.1”, “1.2”, v.v.:
body
{
/* Set "section" to 0 */
counter-reset: section;
}
h1
{
/* Set "subsection" to 0 */
counter-reset: subsection;
}
h1::before
{
/* Increment "section" by 1 */
counter-increment: section;
content: "Section " counter(section) ": ";
}
h2::before {
/* Increment "subsection" by 1 */
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
Hãy tự mình thử »Ví dụ
Tạo một bộ đếm và tăng nó lên một (sử dụng chữ số La Mã) cho mỗi lần xuất hiện của bộ chọn <h2>:
body {
/* Set "my-sec-counter" to 0 */
counter-reset: my-sec-counter;
}
h2::before {
/* Increment "my-sec-counter" by 1 */
counter-increment:
my-sec-counter;
content: counter(my-sec-counter,
upper-roman) ". ";
}
Hãy tự mình thử »Trang liên quan
Tham chiếu CSS: :: trước phần tử giả
Tham chiếu CSS: ::after phần tử giả
Tham chiếu CSS: thuộc tính nội dung
Tham khảo CSS: thuộc tính tăng ngược
Hàm CSS: hàm counter()
Tham chiếu DOM HTML: thuộc tính counterReset