A styled ordered list whose nested list should have numbers with letters using CSS counter property

故事扮演 提交于 2019-12-02 08:50:21

问题


I want to add a letter to an <li>-element from a my under <ol> like from my question:

body { counter-reset: item; }

ol.numbered_style li:before
{
    counter-increment:item;
    margin-bottom:5px;
    margin-right:10px;
    content:counter(item);
    background:blue;
    border-radius:100%;
    color:white;
    width:1.2em;
    text-align:center;
    display:inline-block;
}
ol.numbered_style.start_3{
  counter-reset: item 2; 
}

ol.none, ul.none,ol.numbered_style { list-style: none; }
<ol class="numbered_style">
	<li>first</li>
	<li>second</li>
	<li>third  Lorem Ipsum
		<ol class="numbered_style start_3">
			<li>missing an a after the number</li>
			<li>missing a b after the number</li>
			<li>missing a c after the number</li>
		</ol>
	</li>
</ol>
I have tried li::before { content: " a ";}, but this didn't work. I also want a solution, in which adding in a second counter can generate the next elements with the following alphanumeric pattern like list-style-type: lower-alpha;

回答1:


Sure, you can do anything that the <ol> can do.

ol.numbered_style.start_3{
  counter-reset: item2; 
}

ol.numbered_style.start_3 li:before {
    counter-increment:item2;
    content:counter(item2, upper-latin);
}/* List Style Type========^----------^*/

Counter List Style Types

  • decimal ------------------------------- 1, 2, 3, ...

  • decimal-leading-zero -------------- 01, 02, 03, ... 09, 10, 11, ...

  • lower-alpha, lower-latin ----------- a, b, c, ... z, aa, ab, ...

  • upper-alpha, upper-latin ---------- A, B, C, ... Z, AA, AB, ...

  • lower-roman ------------------------ i, ii, iii, iv, v, vi, ...

  • upper-roman ------------------------ I, II, III, IV, V, VI, ...

  • asterisks ----------------------------- *, **, ***,...

REFERENCE

http://www.princexml.com/doc/5.1/counters/

SNIPPET

body { counter-reset: item; }

ol.numbered_style li:before
{
    counter-increment:item;
    margin-bottom:5px;
    margin-right:10px;
    content:counter(item, upper-roman);
    background:blue;
    border-radius:100%;
    color:white;
    width:1.2em;
    text-align:center;
    display:inline-block;
}
ol.numbered_style.start_3{
  counter-reset: item2; 
}

ol.numbered_style.start_3 li:before {

    counter-increment:item2;
    margin-bottom:5px;
    margin-right:10px;
    content:"3"counter(item2, upper-latin);
    background:blue;
    border-radius:100%;
    color:white;
    width:1.2em;
    text-align:center;
    display:inline-block;
}
  
ol.none, ul.none,ol.numbered_style { list-style: none; }
<ol class="numbered_style">
	<li>first</li>
	<li>second</li>
	<li>third  Lorem Ipsum
		<ol class="numbered_style start_3">
			<li>missing an a after the number</li>
			<li>missing an b after the number</li>
			<li>missing an c after the number</li>
		</ol>
	</li>
</ol>



回答2:


Try this out:

ol.main {
  counter-reset: item;
}
ol.main li {
  counter-increment: item;
}
ol.numbered_style li:before {
  margin-bottom: 5px;
  margin-right: 10px;
  content: counter(item);
  background: blue;
  border-radius: 100%;
  color: white;
  width: 1.2em;
  text-align: center;
  display: inline-block;
}
ol.numbered_style ol.numbered_style li {
  counter-increment: subitem;
}
ol.numbered_style ol.numbered_style li:before {
  content: counter(item) counter(subitem, lower-alpha);
}
ol.none,
ul.none,
ol.numbered_style {
  list-style: none;
}
<ol class="main numbered_style">
  <li>first</li>
  <li>second</li>
  <li>third Lorem Ipsum
    <ol class="numbered_style">
      <li>missing an a after the number</li>
      <li>missing a b after the number</li>
      <li>missing a c after the number</li>
    </ol>
  </li>
</ol>

<hr>

<ol class="main numbered_style">
  <li>first</li>
  <li>second</li>
  <li>third Lorem Ipsum
    <ol class="numbered_style">
      <li>missing an a after the number</li>
      <li>missing a b after the number</li>
      <li>missing a c after the number</li>
    </ol>
  </li>
</ol>


来源:https://stackoverflow.com/questions/36682602/a-styled-ordered-list-whose-nested-list-should-have-numbers-with-letters-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!