2

NOTE:

Due to some of the answers/comments left below (with which I agree), I feel this question is too vague, and does not explain sufficiently my problem. I was too hurried when I put it together, and this has caused the incorrect answers, for which I accept the fault. Due to the current answers and comments, I feel that even if I edited this question again, that future viewers would be confused by the answers/comments on the page, unless everyone were to update them as well. Because of this, i have created another question that completely clarifies my problem. Again, I apologize for the confusion I caused on this question.

The clarified question can be found here: Style an Ordered List like “X.X” based on list start attribute


I am working on updating a client website that contains a policy page. Within the policy are nine different sections, each with their own content. Inside each section are different section statements, which should have the numbering system of "x.x". However, this does not exist in basic HTML. In addition, some sections have various different forms of ordered lists inside themselves.


I have determined that I do not want to tackle this problem in a nested way, that is to say like this:

<ol>
    <li>Section 1
        <ol>
            <li>Item 1</li>
            <li>Item 2</li>
        </ol>
    </li>
</ol>

This is the way that every other answer I have looked at treats the problem. Rather, I wish to tackle it like this (code for below sample). I want a list that simply displays "x.1, x.2, x.3," where 'x' is dependent on the start number of that particular list.

<h2>Section 1</h2>
<strong>Heading 1</strong>
<ol class="specialList">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ol>
<strong>Heading 2</strong
<ol type="lower-roman">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ol>

<h2>Section 2</h2>
<ol class="specialList">
    <li>
        <ol type="upper-alpha">
            <li>First subitem</li>
            <li>Second subitem</li>
        </ol>
    </li>
    <li>Second Item</li>
    <li>Third Item</li>
</ol>

<h2>Section 3</h2>
<ol class="specialList">
    <li>First item
        <ol type="circle">
            <li>First subitem</li>
            <li>Second subitem</li>
        </ol>
    </li>
    <li>Second item</li>
    <li>Third item</li>
</ol>

<h2>Section 4</h2>
<ol class="specialList">
    <li>First item</li>
    <li>Second item</li>
    <li>Third item</li>
</ol>

Section 1

  Heading
  1.1 First item
  1.2 Second item
  1.3 Third item

  Heading 2
  i.  First item
  ii. Second item
  iii. Third item

Section 2

  2.1 First item
    A. First subitem
    B. Second subitem
  2.2 Second item
  2.3 Third item

Section 3

  3.1 First item
    &bullet; First subitem
    &bullet; Second subitem
  3.2 Second item
  3.3 Third item

Section 4

  4.1 First item
  4.2 Second item
  4.3 Third item


This way, I can avoid using a nested ordered list, and hopefully simplify the matter, especially the necessary CSS. It will mean hardcoding some start value attributes in to each ordered list, but the policy sections will not change frequently, so this should not matter.

I do not wish to use JavaScript, as the client wants it to look this way regardless of the user's setup. The pages are JSP pages, so if there is a way to set it up to dynamically generate, that would be acceptable.

I have already looked at these links below. While they are excellent questions, none of them answer my specific question. The first deals with nested ordered lists, while I am dealing with a single ordered list. The second one has the right idea, but is still a bit different (has "x.x.x", while I only want "x.x").

Can Ordered List Produce Results that looks like 1.1?

Achieve sub numbering on ordered list

Please let me know if I need to clarify anything! Thanks!


Summary

In conclusion, the client wants a list that will start at "x.1" and go as far as necessary, where "x" is a given start value attribute for the specific list. I just clarified this matter with them, which is the reason for this "update" of requirements. Basically, I need a class that changes the numbering system of the top level of a list to the "x.x" format (again, where the first "x" is the starting value"). Any sublists (nested lists) will not follow this format, but will follow another format as specified by the "type" or "list-style" attribute.

Community
  • 1
  • 1
Kendall
  • 1,992
  • 7
  • 28
  • 46
  • Can you please show what you have tried. – Sleek Geek Aug 06 '15 at 15:39
  • Possible duplicate - http://stackoverflow.com/questions/30787024/repeated-series-of-number-via-css – Paulie_D Aug 06 '15 at 15:41
  • @Paulie_D The reason that it is not a duplicate is that this person is needing a number that is capable of going several layers (ie. "x.x.x"), whereas I only want a number that will go to "x.x". – Kendall Aug 06 '15 at 15:50
  • That's why I said **possible** duplicate. I suspect the same techniques will apply. – Paulie_D Aug 06 '15 at 15:52
  • i have got it to work see my edited answer you have to use sub lis – Rachel Gallen Aug 06 '15 at 16:04
  • Please include the HTML, in your question, that should produce the displayed list you show as your expected/desired output. Your JS Fiddle links don't seem to properly relate to what you show in that excerpt (and, incidentally, your relevant code should be in your question in order that people don't have to go to an external resource in order to view your code, so that, in future, if JS Fiddle dies or falls over, your question remains useful to others in the absence of external links). See the "[MCVE](http://stackoverflow.com/help/mcve)" guidelines for clearer guidance. – David Thomas Aug 06 '15 at 16:30
  • @DavidThomas please see updates. Thanks for your comment. I need to remember to take more time with my question before posting. Thanks again! – Kendall Aug 06 '15 at 17:17
  • @KendallRoth howcome the other answer regarding the counter increment got an upvote whereas mine didn't? This is the closest you're going to get!!! – Rachel Gallen Aug 06 '15 at 19:57
  • @RachelGallen I'm sorry, but I didn't upvote either of them before this. As to why I've been ignoring this question, I reposted to another question as the client requirements changed. Here is the link to that question. http://stackoverflow.com/questions/31862365/style-an-ordered-list-like-x-x-based-on-list-start-attribute/31863445?noredirect=1#31863445 . I was concentrating on that one, as it was closer to what the client needs. – Kendall Aug 06 '15 at 20:03
  • i have posted a new answer http://jsfiddle.net/ns6jbc6u/2/ – Rachel Gallen Aug 06 '15 at 22:44

2 Answers2

2

Took a while to figure this one out!

here is my fiddle

 h2.title {
   font-size: 20px;
   font-weight: 800;
   margin-left: -20px;
   padding: 12px;
   counter-increment: ordem;
 }
 li.heading {
   font-size: 16px;
   font-weight: bold;
   padding: 12px;
   list-style-type: none;
 }
 .bullet {
   counter-reset: bullet;
   padding-left: 12px;
 }
 .bullet li {
   list-style-type: none;
 }
 .bullet li:before {
   counter-increment: bullet;
   content: counter(ordem)"." counter(bullet)" ";
 }
 ol.none {
   list-style: none!important
 }
 li.s2sub::before {
   counter-increment: none!important;
   content: none!important;
 }
 li.s2sub {
   list-style: upper-alpha;
 }
 li.s3sub::before {
   counter-increment: none!important;
   content: none!important;
 }
 li.s3sub {
   list-style-type: circle;
 }
 li.roman::before {
   counter-increment: none!important;
   content: none!important;
 }
 li.roman {
   list-style: lower-roman inside;
 }
<body>
  <ol>
    <h2 class="title">Section 1</h2> 
    <li class="heading">Heading 1</li>

    <ol class="bullet">
      <li>text 1 one</li>
      <li>text 1 two</li>
      <li>text 1 three</li>
      <li>text 1 four</li>
    </ol>
    <li class="heading">Heading 2</li>

    <ol class="bullet">
      <li class="roman">Item 1</li>
      <li class="roman">Item 2</li>
      <li class="roman">Item 3</li>
    </ol>
    <h2 class="title">Section 2</h2>
    <ol class="bullet">
      <li>First item
        <ol>
          <li class="s2sub">First subitem</li>
          <li class="s2sub">Second subitem</li>
        </ol>
      </li>
      <li>Second Item</li>
      <li>Third Item</li>
    </ol>
    <h2 class="title">Section 3</h2>
    <ol class="bullet">
      <li>First item
        <ol>
          <li class="s3sub">First subitem</li>
          <li class="s3sub">Second subitem</li>
        </ol>
      </li>
      <li>Second item</li>
      <li>Third item</li>
    </ol>
  </ol>
</body>
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • I have experiment with "counter," but did not have any success. I will soon be updating my question with what I have tried. – Kendall Aug 06 '15 at 15:50
  • @RachelGallen See updates for clarification. I removed the numbers in the section headers to hopefully clarify what the client requested. Thanks! – Kendall Aug 06 '15 at 17:19
1

I got it to work like so:

body{
    counter-reset: section children;  
}
li{
    list-style:none;
}
.parent::before {
  counter-increment: section;  
  content: counter(section) " - ";
}
.parent li:first-child{
    counter-reset:children;
}
.parent li::before{
    counter-increment: children;  
    content: counter(section) "." counter(children) " - ";
 }
<ol>
    <li class="parent">Section 1
        <ol>
            <li>Item 1</li>
            <li>Item 2</li>
        </ol>
    </li>
    <li class="parent">Section 2
        <ol>
            <li>Item 1</li>
            <li>Item 2</li>
        </ol>
    </li>

</ol>

I removed the list style, since it isn't necessary.
What this does is create two separate counters for the children and the sections, then resets the children counter on every new section.

JSFiddle Demo

Jacob G
  • 13,762
  • 3
  • 47
  • 67
  • I may not be entirely clarifying what I want. I'll have to update again. I don't want to use nested lists at this point, as it causes issues with the existing styles on the site (large corporate site). Because of this, I just want a class that I can apply to a list that will cause it to use the "x.x" numbering instead of just "x". – Kendall Aug 06 '15 at 16:03
  • @KendallRoth can you give an example html? – Jacob G Aug 06 '15 at 16:04
  • The second JSFiddle I uploaded has the sample HTML...I am completely aware that the CSS in it is wrong :) – Kendall Aug 06 '15 at 16:09
  • I updated my question again...I sincerely apologize for the confusion from before. I have removed the numbers from the section headers, as this should make it a little clearer. Please let me know if this is not the case. Thanks – Kendall Aug 06 '15 at 17:18