I am in the area of CSS printed media. I have HTML documents that follow this structure:
A main
element with following child elements:
- mandatory:
div.list-of-titles
element (document has a Table of Content, of Figures, of Images … in thisdiv
) - optional:
section.dedication
elemens (document may have a dedication) - optional and unbounded:
section.preface
(document may have one ore more preface sections) - mandatory and unbounded:
section.chapter
(document has one ore more chapters)
For good old-fashioned Books i’d like to number the pages with the ToC and the dedication (if any) with roman numbers, and the real content with decimal / arabic numbers. For this, i have to do a counter-reset: page
when the real content begins.
The real content may start with the first section.preface
element, or, if there is no such element, with the first section.chapter
element. So my question is:
How can i select the first child element of the main
element which is a section
and has either a preface
class or a chapter
class?
Thanks,
Frank Steimke
3
Answers
In CSS, you could use the
:first-child
pseudoclass along with thesection
selector and theis()
pseudoclass function.Hope it helps you!
Your question would more easily be solved by restructuring your HTML rather than going with what you’ve suggested.
A possible solution if you can’t do this is to style what you’ve asked for ignoring the idea of only styling the first match, then "undo" for subsequent children using the general sibling selector
~
e.g.This is just a general idea of how you might approach this and isn’t tested.
The clearest way might be to simply spell out each possibility
using the CSS + combinator.
There is probably more specificity here than you need in practice and you can shorten things using the CSS nesting facility.