html - 在 CSS 中以 Unicode 数字定义自定义列表类型的更好方法

标签 html css

我想制作一个自定义列表类型的 Khmer Unicode Number ១ ២ ៣.. 有序。我尝试使用 css css pseudo li:nth-child(1) li:nth-child(2) li:nth-child(3).. 在我的样式表中这样定义:

ul.mainList {
  list-style-type: none;
}
ul.mainList > li {
  text-indent: -5px;
}
ul.mainList > li:nth-child(1):before {
  content: "១. ";
  text-indent: -5px;
}
ul.mainList > li:nth-child(2):before {
  content: "២. ";
  text-indent: -5px;
}
ul.mainList > li:nth-child(3):before {
  content: "៣. ";
  text-indent: -5px;
}
ul.mainList > li:nth-child(4):before {
  content: "៤. ";
  text-indent: -5px;
}
ul.mainList > li:nth-child(5):before {
  content: "៥. ";
  text-indent: -5px;
}
ul.mainList > li:nth-child(6):before {
  content: "៦. ";
  text-indent: -5px;
}
ul.mainList > li:nth-child(7):before {
  content: "៧. ";
  text-indent: -5px;
}
ul.mainList > li:nth-child(8):before {
  content: "៨. ";
  text-indent: -5px;
}

我的 HTML 标记是这样的:

<p>List of items</p>
<ul class="mainList">
    <li>Item ១</li>
    <li>Item ២</li>
    <li>Item ៣</li>
    <li>Item ៤</li>
    <li>Item ៥</li>
    <li>Item ៦</li>
    <li>Item ៧</li>
    <li>Item ៨</li>
</ul>

它工作得很好,但是假设如果我有十个或二十个列表项,我将不得不定义十个或二十个 li:nth-child(n) 二十次。

有没有更好的办法?谢谢

最佳答案

这是一个不太为人所知的特性,但是 list-style CSS 属性可以接受非常广泛的值(参见 docs)。包括 高棉语!所以你很幸运,最简单的方法是简单地定义你的有序列表list-style...

ol.khmer {
  list-style: khmer;
}
<p>List of items</p>
<ol class="khmer">
  <li>Item ១</li>
  <li>Item ២</li>
  <li>Item ៣</li>
  <li>Item ៤</li>
  <li>Item ៥</li>
  <li>Item ៦</li>
  <li>Item ៧</li>
  <li>Item ៨</li>
</ol>

我发现很重要的一点是,正如@Mr_Lister 所述,并非所有浏览器都支持它。另一个选项可能是使用 @counter-style不支持更多......如果应该支持所有浏览器,你必须坚持你的解决方案。

关于html - 在 CSS 中以 Unicode 数字定义自定义列表类型的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46191189/

相关文章:

css - 属性 border-radius 在 CSS level 2.1 中不存在,但在 : 6px 6px 中存在

html - 根据静态宽度容器设置 div 元素宽度的数量

Jquery加载消息然后显示站点

javascript - 悬停更改文本,然后替换为原始文本

html - 采取容器高度的动态方式?

html - 垂直对齐输入元素

css - CSS编码不适用于我的页面

css - Sass::SyntaxError on Rails 生产预编译

html 时间字段更改 -- :-- to __:__

javascript - 如何使用 jQuery UI 选项卡进行 CSS 转换?