2014-09-04 72 views
3

我想用HTML5CSS只創建這樣的列表:有沒有辦法像使用HTML5和CSS一樣創建多級列表?

1 First 
    1.1 Indent 1 
    1.2 Indent 2 
2 Second 
    2.1 Indent 1 
    2.2 Indent 2 
    2.2.1 More depth 

我檢查多個教程上有序列表,但它似乎<ol>標籤沒有做選擇所以。是否可以使用HTML5CSS

+0

是那些名單你的問題其實是關於什麼的自動編號?如果是這樣,請查看https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters – CBroe 2014-09-04 14:45:25

+0

查看http://stackoverflow.com/questions/2729927/number-nested-ordered-lists-in -html – Aibrean 2014-09-04 14:45:33

回答

5

可以使用before僞元素來實現這一目標:

HTML

<ul class="numeric"> 
<li>First 
    <ul> 
     <li>Indent </li> 
     <li>Indent </li> 
    </ul> 
</li> 
<li>Second 
    <ul> 
     <li>Indent</li> 
     <li>Indent</li> 
    </ul> 
</li> 
<li>Third 
     <ul> 
      <li>Indent</li> 
      <li>Indent</li> 
      <li>Indent</li> 
      <li>Indent</li> 
     </ul> 
    </li> 
<li>Fourth 
    <ul> 
     <li>Indent</li> 
     <li>Indent</li> 
    </ul> 
</li> 
<li>Five</li> 

CSS

ul.numeric { counter-reset:section; list-style-type:none; } 
ul.numeric li { list-style-type:none; } 
ul.numeric li ul { counter-reset:subsection; } 
ul.numeric li:before{ 
    counter-increment:section; 
    content:counter(section) ". "; 
} 
ul.numeric li ul li:before { 
    counter-increment:subsection; 
    content:counter(section) "." counter(subsection) " "; 
} 

fiddle

參考文獻:

CSS counter-reset Property

+0

如果我在每個元素上手動設置element.style,它應該工作嗎?我試過了,它不起作用。儘管您的示例使用外部css文件。 – 2014-09-05 09:58:28

+0

不,我沒有使用外部的CSS文件..我使用純CSS。看我的小提琴的例子。 – 2014-09-05 10:00:07

+0

但[this](http://jsfiddle.net/tpqe74ov/)不起作用。 – 2014-09-05 10:03:15

相關問題