I need HTML to produce output similar to:
1. Some title 1.1 blah blah (a) blah blah blah (b) blah blah 1.2 blah blah
I believe that because of the need for parenthesis round the letters that I cannot use the ordered list tag for this. Obviously it can be done with a table, but I'd much rather use CSS. However I'm at a loss as to how to do this.
If lines wrap to the next line, they should continue under the text like an ordered list would.
UPDATE I have tried
OL {
counter-reset: numeric
}
OL LI OL LI OL {
counter-reset: latin
}
LI {
display: block
}
LI:before {
content: counters(numeric, ".") " ";
counter-increment: numeric
}
OL LI OL LI OL LI:before {
content: "(" counter(latin, lower-latin) ") ";
counter-increment: latin
}
and HTML such as:
xxx
<ol>
<li>one
<ol>
<li>onedotone</li>
<li>onedottwo
<ol>
<li>A</li>
</ol>
</li>
</ol>
<li>two</li>
</ol>
produces this
xxx 1 one 1.1 onedotone 1.2 onedottwo (a) A 2 two
Unfortunately, my requirements are exactly as stated in the original question. So my CSS fails in these areas.
- There needs to be a full stop after 1 and after 2 but not after 1.1 and 1.2
- 1 and 1.1 should not be indented and the text for both of them needs to be aligned to the same place. So the word onedotone needs to be exactly below the word one. Also there needs to be a bigger gap than one space between the number and the text.
- The (a) needs to line up with the words onedottwo, and again there needs to be a bigger gap than one space between (a) and A.
padding-left
is not the answer, as it does not help line up the text after the numbers, You get
1 one 1.1 onedotone
instead of
1. one 1.1 onedotone
This is beyond my CSS capabilities. Unless anyone has the expertise to point me in the right direction, I fear that I will have to fall back on using a table.
Below is a sample on how the desired result can be achieved using
<ol>
(ordered lists) and CSScounters
. It has a bit of a hack-ish feel about it because of the expectation that when a line is wrapped around, it should not start from under the numberings. Otherwise, I feel this method is much better than manually keying in the numbers (or) using tables.Consistent spacing between the numbering and text is obtained by setting a
width
to theli:before
pseudo-element and making its display asdisplay: inline-block
. Modify thewidth
based on how much spacing is required. Note that when modifying thewidth
, themargin-left
andpadding-left
also have to be modified accordingly to maintain the styling.CSS Counters have reasonably good browser support also.
Feedback to comments:
The
{content: "\a"; white-space: pre;}
trick does not quite work withinline-block
elements. The inner levelli
tags areinline-block
in our case (for reasons explained above in 2nd paragraph) and hence that particular trick doesn't work. The alternate is to insert a blank filler line withheight
of the line equal to the requiredline-height
and position it below theli
tag. Note that the selector used isli:not(:last-child):after
because we don't need an extra line break after the lastli
(if we do not do it, we will have double line space after the innerli
ends). This is a CSS3 selector and so might not work with lower versions of IE. If you need to support those versions also then we would need to tweak it further (or simpler would be to usebr
).You were on the correct path here, but the
:before
pseudo-element (which has the numbering) is not on the element withclass='level1'
. It was on the.level1 > li
and hence doing a reset offont-weight
for selector.level1 > li:before, .level1 > li *
would fix it. As you would have already known/guessed,.level1 > li *
means every element under the level oneli
.