CSS Nested Ordered Lists Counter Manipulation - html

I would like to start nested ordered lists with specific numbers while keeping numbering normal (based on the first starting number) for nested list items. Rather than send people down the wrong path and show the numerous versions of scripts I have tried thus far, I am just going to show my desired end state and ask how to get there.
toc.html which is a table of contents would look like (using nested ordered lists):
Table of Contents
1 Introduction
2 Assembly
2.1 Preparation
2.1.1 Space
2.1.2 Tools
2.1.3 Parts
2.2 Assembly
2.2.1 Build It
3 Use
3.1 Defaults
3.2 Customizations
3.2.1 Safety
3.2.1 Insanity
4 Trouble-Shooting
5 Reference
On the assembly.html page, which is what 2 Assembly would point to from the Table of Contents, I would like it to look like this:
Blah blah blah, fake latin goes here.
2 Assembly
2.1 Preparation
2.1.1 Space
2.1.2 Tools
2.1.3 Parts
2.2 Assembly
2.2.1 Build It
Body of this page, along with more fake latin, goes here.
And on use.html I would want it to look like:
Blah blah blah, fake latin goes here.
3 Use
3.1 Defaults
3.2 Customizations
3.2.1 Safety
3.2.1 Insanity
Body of this page, along with more fake latin, goes here.
How do I accomplish this using only HTML and CSS? I do not want javascript or anything other than HTML and CSS. My file structure is:
/css/main.css
/toc.html
/assembly.html
/use.html
/trouble-shooting.html
/reference.html

Declare the li elements as block-elements. Then you can make use of the conter-resetattribute and re-add the counter through pseudo-elements:
ol {
counter-reset: item;
}
li {
display: block;
}
li:before {
content: counters(item, ".") " ";
counter-increment: item;
}
<ol>
<li>Introduction</li>
<li>Assembly
<ol>
<li>Preparation
<ol>
<li>Space</li>
<li>Tools</li>
<li>Parts</li>
</ol>
</li>
<li>Assembly
<ol>
<li>Build It</li>
</ol>
</li>
</ol>
</li>
<li>Use
<ol>
<li>Defaults</li>
<li>Customizations
<ol>
<li>Safety</li>
<li>Insanety</li>
</ol>
</li>
</ol>
</li>
<li>Trouble-Shooting</li>
<li>Reference</li>
</ol>

It was my hope there would be some way to nest counters, or to force a counter to only reset the initial count item. Based on the answer from #tacoshy in which he states that a server-sided solution like PHP is required to call a counter listing from a database, and that there is no other easy HTML and CSS solution, I decided to just use a garbage workaround. For sure there is a more elegant way to do this:
I created items within CSS which I could use to hide things.
li.hideme {
visibility:hidden;
}
#coverup01 {
position:relative;
top: -60px;
}
#coverup02 {
position:relative;
top: -75px;
}
Then, I used the basic HTML CSS solution for nested ordered lists, and added the references to the above. On the assembly.html page I did this:
<ol>
<li class="hideme">Introduction</li>
<span id="coverup01">
<li>Assembly
<ol>
<li>Preparation
<ol>
<li>Space</li>
<li>Tools</li>
<li>Parts</li>
</ol>
</li>
<li>Assembly
<ol>
<li>Build It</li>
</ol>
</li>
</ol>
</li>
<li>Use
<ol>
<li>Defaults</li>
<li>Customizations
<ol>
<li>Safety</li>
<li>Insanity</li>
</ol>
</li>
</ol>
</li>
<li>Trouble-Shooting</li>
<li>Reference</li>
</span>
</ol>
And on the use.html page I did this:
<ol>
<li class="hideme">Introduction</li>
<li class="hideme">Assembly</li>
<span id="coverup02">
<li>Use
<ol>
<li>Defaults</li>
<li>Customizations
<ol>
<li>Safety</li>
<li>Insanity</li>
</ol>
</li>
</ol>
</li>
<li>Trouble-Shooting</li>
<li>Reference</li>
</span>
</ol>
The coverup01 and coverup02 ID selectors are just for my own use, to pull the visible list up. If you expect a solid responsive design with this, you will want to edit each screen size accordingly. This works for a small list. For a larger list, this may be cumbersome.
This returned the results I was trying to find. I did not want to use garbage code, but if there is no supported solution, then a workaround must suffice for now.
As I am typing this ... I may be able to put the coverup stuff into the li.hideme class, which would simplify things considerably. Not sure if CSS allows for positioning invisible objects, but we shall see [pun]....
This is the solution I have used, and it seems to be working just fine.

Related

Can you have an HTML list where you choose the number value of each individual bullet?

I need to be able to generate an HTML list like
(1) sdkfljsdlkfj
(4) lksdfjlksjdf
(2) lksdjfklsdjf
(7) sdklfjlskdfj
(16) sdklfjhlskdjf
The parentheses aren't necessary, it was just the only way for the Stack Overflow text editor to not auto-format the list. The only thing that matters is that the bullets need to be numbered, but for the numbers to be what I choose and potentially out of order.
I don't even know what to call this type of list concisely, and generally googling about how to do ordered and unordered HMTL lists doesn't give any hints about this.
If you're wondering why I need this, it's because this data comes to my website with Javascript and it's different line items based on a selection request. I'd like to display them in a list, but retain the original values.
Right now, I'm doing an unordered list where the number is in the beginning of the item text, but it doesn't look too great, compared to what I have in mind.
1. sdkfljsdlkfj
4. lksdfjlksjdf
2. lksdjfklsdjf
7. sdklfjlskdfj
16. sdklfjhlskdjf
Does HTML allow this? Something like <li bulletValue=24> ... </li> would be the most convenient.
You would use the value attribute on the li items in the ordered list. See the example below.
<ol>
<li value="1">Item in the list</li>
<li value="3">Item in the list</li>
<li value="6">Item in the list</li>
<li value="9">Item in the list</li>
<li value="67">Item in the list</li>
</ol>
Use CSS along with a data-attribute:
.data-bullet-list {
list-style-type: none;
}
.data-bullet-list li::before {
content: "("attr(data-bullet)") ";
}
<ul class="data-bullet-list">
<li data-bullet="1">Item
<li data-bullet="4">Item
<li data-bullet="2">Item
<li data-bullet="7">Item
<li data-bullet="16">Item
</ul>
Compared to the native value attribute method (which works only for ol, not ul, and your desired result is not ordered), this approach allows for much more customizable styling.

Are nested HTML lists deprecated?

The HTML 4 spec treats the following as a deprecated example (search for "DEPRECATED EXAMPLE"):
<UL>
<LI> ... Level one, number one...
<OL>
<LI> ... Level two, number one...
<LI> ... Level two, number two...
<OL start="10">
<LI> ... Level three, number one...
</OL>
<LI> ... Level two, number three...
</OL>
<LI> ... Level one, number two...
</UL>
Why is this example deprecated?
The start attribute is deprecated in HTML 4 (it is un-deprecated in HTML 5). Everything else about the example is fine.
The spec details the proper way to nest ul and ol elements. They must be encased in an li element, as follows:
<ul>
<li>
<ol>
<li>Hello there</li>
</ol>
</li>
</ul>
However in your example, the lists are not wrapped in an li tag, meaning that it would fail HTML validation.

specifying unpaired parentheses in ordered lists rather than dot (period)

Is there an easy way, as in designed, to specify that an unpaired parentheses ")" closes order list numbers rather than the standard dot "." ?
I read a solution here, but this solution seems to be a global solution to an entire web page. But I want to build a complex list that makes use of both dots and unpaired parentheses with varying types of ordered list designations (numerals, upper and lower case letters as well as roman numerals) to distinguish an outer list item with an inner list item using the same list designation.
Now my html kung-fu is not very Bruce Lee, so I hope there might be a simple solution.
For Example:
<ol type="I" align="justify">
<li>Historical introduction</li>
<li>Dentition in various groups of vertebrates
<ol type="A" align="justify">
<li>Reptilia
<ol>
<li>Histology and development of reptilian teeth</li>
<li>Survey of forms</li>
</ol>
<li>Mammalia
<ol>
<li>Histology and development of mammalian teeth</li>
<li>Survey of form
<ol type="a" align="justify">
<li>Primates
<ol>
<li>Lemuroidea</li>
<li>Anthropoidea
<ol type="a" align="justify">
<li>Platyrrhini</li>
<li>Catarrhini
<ol type="i" align="justify">
<li>Cercopithecidae</li>
<li>Pongidae</li>
</ol>
</li>
</ol>
</li>
</ol>
<li>Carnivora
<ol>
<li>Creodonta</li>
<li>Fissipedia
<ol type="a" align="justify">
<li>Ailuroidea</li>
<li>Arctoidea</li>
</ol>Pinnipedia</li>
</ol>
<li>Etc. . . .</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
I expanded the example to illustrate the indentation (which is slightly off, but close enough for affect I hope).
I tried to pasted the above as a rendered webpage but for some reason all of the indentation as well as list identifiers are lost.
You can keep this from being "global" by adding a .paren class to the css like in this jsfiddle (forked from the one you show above).
You'll also want to open the ol tag with <ol class="paren">

What sort of markup do you recommend for a table of contents?

I need to create a large table of contents for an HTML book but I can't decide what's the best solution for its markup. I have two options in mind: definitions lists or ordered lists.
Would you consider this a personal style decision? And how about semantics?
I like my list to be numbered but I have problems using "ol" with nestes lists. I guess I'd do the same thing I did with my definition lists by numbering manually (and disabling style in my list).
I thought of these two:
Option A:
<div class="TOC">
<dl>
<dt>Preface</dt>
<dt>I. Chapter 1</dt>
<dd>
<dl>
<dt>1 Section 1</dt>
<dd>
<dl>
<dt>1.1 Subsection A</dt>
<dt>1.2 Subsection B</dt>
<dt>1.3 Subsection C</dt>
</dl>
<dt>2 Section 2</dt>
</dd>
</dl>
</dd>
</dl>
</div>
Option B:
<div class="TOC">
<ol>
<li>Preface</li>
<li>Chapter 1
<ol>
<li>Section 1
<ol>
<li>Subsection A</li>
<li>Subsection B</li>
<li>Subsection C</li>
</ol>
</li>
<li>Section 2</li>
</ol>
</li>
</ol>
</div>
Ordered list is the correct markup. Using a definition list would imply that the major section title is a term which is defined by the titles of its subsections, which is not quite correct.
2020 opinion...
Use <nav> to indicate navigation
<nav> implies role=navigation
<div role="navigation"> is decent but <nav> is ideal
<nav> should have heading or else [aria-label] to tell purpose
Use <ol> or <ul> to group links
Use <ol> for TOC about sequential content like book chapters
Use <ul> for TOC about nonsequential content like an FAQ
TOC semantic markup examples
<nav>
<h2>Chapters</h2>
<ol>
<li>Chapter 1
<li>Chapter 2
<li>Chapter 3
</ol>
</nav>
<nav aria-label="Chapters">
<ol>
<li>Chapter 1
<li>Chapter 2
<li>Chapter 3
</ol>
</nav>
<nav>
<h2>Questions</h2>
<ul>
<li>How do artists get paid?
<li>How do artists get feedback?
<li>How do artists get fans?
</ul>
</nav>
<nav aria-label="Questions">
<ul>
<li>How do artists get paid?
<li>How do artists get feedback?
<li>How do artists get fans?
</ul>
</nav>
Definition lists are NOT strictly for "definitions" as some people seem to be saying - if they were they would have extremely few uses. However, while <dl/>s are very flexible and have many uses, the ordered-list does seem like a better option here.
If you're trying to number nested lists (whether they're nested <ol/>s or <dl/>s), you can use CSS counter-increment and counter-reset properties to add numbers automatically based on the nested depth, rather than maintaining the numbering manually for every revision.
Example:
.TOC ol{
list-style-type:none;
counter-reset:toc1;
}
.TOC ol li::before{
content:counter(toc1)' ';
counter-increment:toc1;
}
.TOC ol li ol{
counter-reset:toc2;
}
.TOC ol li ol li::before{
content:counter(toc1)' .'counter(toc2)' ';
counter-increment:toc2;
}
.TOC ol li ol li ol{
counter-reset:toc3;
}
.TOC ol li ol li ol li::before{
content:counter(toc1)' .'counter(toc2)' .'counter(toc3)' ';
counter-increment:toc3;
}
100% I'd go with ordered list. This is exactly what they were put into the language for. Using definition lists is an abuse of the language, just because their default rendering is somewhat like you want to see. (Use CSS to make it like right)
What problem are you having with nested ordered lists? (You aren't even supposed to nest DLs)

How can be printed ordered list from number 6 with html only?

How can be printed ordered list from number 6 with html only (if its not possible how should be done)?
Example:
6.home
7.place
8.etc..
9.etc..
Thanks
Use the start attribute:
<ol start=6>
<li>home</li>
<li>place</li>
...
</ol>
Note that its use is deprecated so you shouldn't use it in new documents. The W3C recommends replacing its use by CSS Counters.
(In my humble opinion though, this is partially a mistake, since the number with which a list starts isn't always a pure style choice. Numbered lists carry semantics as well and in this case I consider a number to start with semantics, not style.)
An alternative way in HTML only is:
<ol>
<li value="6">test</li>
<li>This should be 7</li>
</ol>
This allows more flexibility since you can reset numbering mid list but it is still deprecated. As Johannes Rössel said you should use CSS methods if possible.
Are you asking for the syntax for
<ol start="6">
<li></li>
<li></li>
</ol>
However, according to w3.org the start value is deprecated on OL...
This solution may not look efficient but it works (only in IE). This way you don't have to use the deprecated start attribute.
CSS code:
.hideme { display:inline;}
HTML code:
<ol id="num">
<li class="hideme"></li>
<li class="hideme"></li>
<li class="hideme"></li>
<li class="hideme"></li>
<li class="hideme"></li>
<li>home</li>
<li>place</li>
<li>etc</li>
<li>etc</li>
<li>etc</li>
<li>..</li>
</ol>
Though it works, I feel it's ugly.