如何使用 CSS 为有序列表项编号添加静态字符串前缀?

发布于 2024-10-31 08:24:20 字数 557 浏览 6 评论 0原文

我希望这个 HTML:

<ol style="list-style:decimal;">
<li>Apples</li>
<li>Oranges</li>
</ol>

像这样呈现:

Q1。苹果
Q2。橙子

即,我想在每个数字前加上“Q”前缀。

我已经尝试过像这样的CSS:

ol li::before {
  content: "Q";
}

但呈现如下:

1. Q苹果
2.Q橙子

我也尝试过使用list-style: numbered inside;,但这只是将列表向右移动,结果相同。我找不到任何方法来引用这些数字以便使用 CSS 设置它们的样式。这看起来是一个简单、常见的场景,但我找不到任何方法用简单的 CSS 来完成它(没有 CSS 计数器、JavaScript 等)。

I want this HTML:

<ol style="list-style:decimal;">
<li>Apples</li>
<li>Oranges</li>
</ol>

to render like this:

Q1. Apples
Q2. Oranges

Ie, I want to prefix "Q" to each number.

I've tried CSS like this:

ol li::before {
  content: "Q";
}

but that renders like this:

1. QApples
2. QOranges

I've also tried using list-style: numbered inside;, but that just shifts the list to the right with the same results. I can't find any way to reference the numbers in order to style them with CSS. This seems like such a simple, common scenario, yet I can't find any way to accomplish it with straightforward CSS (without CSS counters, JavaScript, etc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请爱~陌生人 2024-11-07 08:24:20

唯一的纯 CSS 方法是使用 计数器

ol {
    counter-reset: item;
    list-style-type: none;
}

ol li:before {
    content: 'Q' counter(item, decimal) '. ';
    counter-increment: item;
}

除了使用CSS 计数器(专为此类用例设计!)或 JavaScript。

顺便说一下,它是十进制 ,而不是编号

The only pure CSS way is with counters:

ol {
    counter-reset: item;
    list-style-type: none;
}

ol li:before {
    content: 'Q' counter(item, decimal) '. ';
    counter-increment: item;
}

You cannot achieve this besides using CSS counters (which were designed specifically for such use cases!) or JavaScript.

By the way, it's decimal, not numbered.

温馨耳语 2024-11-07 08:24:20

有一种脆弱非计数器方法,但它很容易崩溃:

ol {
    list-style-type: decimal;
    margin: 0 0 0 2em;
}

li {
    position: relative;
}

ol li:first-letter {
    color: #f90;
    float: left;
    position: relative;
    margin-left: -2em;
}

JS Fiddle演示

There is a, fragile, non-counter method, but it's prone to breaking:

ol {
    list-style-type: decimal;
    margin: 0 0 0 2em;
}

li {
    position: relative;
}

ol li:first-letter {
    color: #f90;
    float: left;
    position: relative;
    margin-left: -2em;
}

JS Fiddle demo.

一抹苦笑 2024-11-07 08:24:20

鉴于此 HTML:

<ol class="q">
  <li>Apples</li>
  <li>Oranges</li>
</ol>

您可以 (1) 使用 @counter-style CSS at-rule:(

@counter-style q {
  system: extends decimal;
  prefix: "Q";
}

ol.q {
  list-style: q;
}

参见W3C:CSS 计数器样式级别 3:3.1.7. 从现有计数器样式构建:扩展系统。)或 (2) 使用 CSS 计数器::marker CSS 伪元素

ol.q {
  counter-reset: q;
}

ol.q > li {
  counter-increment: q;
}

ol.q > li::marker {
  content: "Q" counter(q) ". ";
}

无论哪种方式,您都可以设置 list-style-position CSS 属性 通过 list-style CSS速记属性到值inside,以使渲染的HTML看起来像BoltClock的答案

HTML 编辑:我删除了 style="list-style:decimal;" 因为我认为这是 ol 元素的默认设置。我添加了 class="q" 以便样式仅适用于特定列表。我将每个 li 元素缩进两个空格,因为我更喜欢这种样式。

Given this HTML:

<ol class="q">
  <li>Apples</li>
  <li>Oranges</li>
</ol>

you could (1) use the @counter-style CSS at-rule:

@counter-style q {
  system: extends decimal;
  prefix: "Q";
}

ol.q {
  list-style: q;
}

(See W3C: CSS Counter Styles Level 3: 3.1.7. Building from Existing Counter Styles: the extends system.) or (2) use CSS counters and the ::marker CSS pseudo-element:

ol.q {
  counter-reset: q;
}

ol.q > li {
  counter-increment: q;
}

ol.q > li::marker {
  content: "Q" counter(q) ". ";
}

Either way, you could set the list-style-position CSS property via the list-style CSS shorthand property to the value inside in order to make the rendered HTML appear like that of BoltClock's answer.

HTML edits: I removed style="list-style:decimal;" because I think that's the default for ol elements. I added class="q" so that the styling only applies to specific lists. I indented each li element by two spaces because I prefer that style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文