prefix

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2023.

Descriptor prefix của quy tắc @counter-style chỉ định nội dung sẽ được thêm vào đầu biểu diễn marker của bộ đếm.

Khi giá trị bộ đếm là âm, prefix đứng trước dấu âm và bất kỳ <symbol> nào khác được thêm bởi descriptor negative.

Cú pháp

css
/* Giá trị <symbol>: chuỗi, ảnh, hoặc định danh */
prefix: "»";
prefix: "Page ";
prefix: url("bullet.png");

Giá trị

Descriptor prefix nhận một <symbol> duy nhất làm giá trị:

<symbol>

Chỉ định một <symbol><string>, <image>, hoặc <custom-ident> — được thêm vào trước biểu diễn marker.

Định nghĩa chính thức

Related at-rule@counter-style
Initial value"" (the empty string)
Computed valueas specified

Cú pháp chính thức

prefix = 
<symbol>

<symbol> =
<string> |
<image> |
<custom-ident>

<image> =
<url> |
<image()> |
<image-set()> |
<cross-fade()> |
<element()> |
<gradient>

<image()> =
image( <image-tags>? [ <image-src>? , <color>? ]! )

<image-set()> =
image-set( <image-set-option># )

<cross-fade()> =
cross-fade( <cf-image># )

<element()> =
element( <id-selector> )

<image-tags> =
ltr |
rtl

<image-src> =
<url> |
<string>

<image-set-option> =
[ <image> | <string> ] [ <resolution> || type( <string> ) ]?

<cf-image> =
[ <image> | <color> ] &&
<percentage [0,100]>?

<id-selector> =
<hash-token>

Ví dụ

Thêm tiền tố vào bộ đếm

Trong ví dụ này, mỗi số bộ đếm được thêm tiền tố "Book " (với khoảng trắng) và theo sau là dấu hai chấm (:). Dấu hai chấm được thêm bằng descriptor suffix.

HTML

html
<ol class="books">
  <li>Flamer, by Mike Curato</li>
  <li>Gender Queer: A Memoir, by Maia Kobabe</li>
  <li>Tricks, by Ellen Hopkins</li>
  <li>The Handmaid's Tale: The Graphic Novel, by Margaret Atwood</li>
  <li>Crank, by Ellen Hopkins</li>
</ol>

CSS

css
@counter-style books {
  system: numeric;
  symbols: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9";
  prefix: "Book ";
  suffix: ": ";
}

.books {
  list-style: books;
  padding-left: 15ch;
}

Kết quả

Đặc tả kỹ thuật

Specification
CSS Counter Styles Level 3
# counter-style-prefix

Tương thích trình duyệt

Xem thêm