玩HTML's有序列表编号项目符号

Playing with HTML's ordered list number bullets

本文关键字:列表 编号 项目 符号 HTML      更新时间:2023-09-26

有人能想到一种方法来使用ol/li列表中的数字来标记图像吗?

<ol>
  <li><img /></li>
  <li><img /></li>
  <li><img /></li>
</ol>

加上一些CSS应该输出如下内容:

------ ------ ------
|    | |    | |    |
|    | |    | |    |
|   1| |   2| |   3|
------ ------ ------

其中每个方块是一个小的头像。

我知道我可以在li中插入一个带有数字的新元素,并根据需要进行操作,但我想用内置的ol编号来做。

很简单:

ol {
    counter-reset: listCount;
}
li {
    float: left;
    border: 3px solid #f90;
    counter-increment: listCount;
    position: relative;
}
li:after {
    content: counter(listCount,decimal-leading-zero);
    position: absolute;
    bottom: 0;
    right: 0;
    width: 2em;
    height: 2em;
    color: #fff;
    background-color: rgba(0,0,0,0.5);
    text-align: center;
    line-height: 2em;
}

JS Fiddle demo.

当然,这需要用户有一个能够使用css生成内容的浏览器,这基本上排除了IE。

引用:

  • CSS-generated计数器。

看起来有点俗气,但这是可行的(至少在firefox中):

http://jsfiddle.net/ztfzt/14/

<ol>
    <li><img src="http://placekitten.com/100/100" /></li>
    <li><img src="http://placekitten.com/100/100" /></li>
    <li><img src="http://placekitten.com/100/100" /></li>
</ol>
ol {}
ol li {
    float: left;
    list-style-position: inside;
    position: relative;
    width: 100px;
    height: 100px;
    margin-right: 5px;
    line-height: 170px;
    text-indent: 85px;
    color: #fff;
}
ol li img {
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
}

如果我是你,我会直接使用附加元素解决方案。

编辑:在chrome, IE9和ie8中测试。似乎两者都有效。但是,IE7中的问题可能可以通过一些额外的浏览器特定的css来修复。