Headings and Table of Contents
We provide many useful default styles in our base.css
. You can import them to your report's CSS using:
1 |
|
Headings and Table of Contents can be used out of the box with the imported styles.
If you want to customize heading numberings or table of content (like margins, etc.), have a look at the following chapters.
Customization
Use the following snippets as a guide how to override the base styles.
You do not need them, if you imported the base styles and don't need further customization.
CSS has counters to automatically number items such as headings, figures, etc. and also generate table of contents and list of figures with these numbers.
This allows you to automatically produce a structure similar to
1 2 3 4 5 6 7 |
|
Additional resources:
- CSS Counters
Heading Numbers
This example contains code for numbering headings with pure CSS.
The heading number is placed in the ::before
pseudo-element in the DOM using CSS.
CSS counters first have to be defined with counter-reset: <counter-name>
(best place this rule in html
).
The counters by default start at 0
, but the start value can also be overwritten.
Before the counter value is used, it should be incremented (such that chapter numbers start at 1, not 0) with counter-increment: <counter-name>
.
Now the counter has the correct value, we can embed it with content: counter(<counter-name>)
in ::before
pseudo-elements.
Counters are incremented and referenced with CSS rules in selectors.
Counters have no global value at a given time, instead their values depend on the DOM-position of the elements that use them.
For example: the custom h1-counter
is incremented at every <h1>
tag.
This means that between the first and the second <h1>
tag in the DOM structure, the counter has the value 1
,
between the second and third <h1>
it has the value 2 and so on.
When the CSS rule h2::before { counter-increment: h2-counter; content: counter(h1-counter) "." counter(h1-counter); }
accesses the counter value of h1-counter
the value is different depending on where the targeted h2
element is placed in the DOM.
Note that this h2::before
rule is defined only once and applies to all <h2>
tags.
Basic Heading Numbering
Add numbering to heading tags which have the class numbered
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Appendix Numbering
If you want appendix sections that are numbered differently, an additional counter can be used that uses a different number formatting. E.g. with A, A.1, A.2, B, B.1, etc. instead contiuned numbering 4, 4.1, 4.2, 5, 5.1, etc.
CSS counters can specify a counter style to use such as upper-alpha
instead of decimal numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Table of Contents
A table of contents can be included in reports via the <table-of-contents>
component.
This component collects all elements with the class in-toc
, and provides them as variables.
This component uses delayed multi-pass rendering to ensure that all items referenced in the TOC are already rendered and can be referenced.
Heading Numbers in TOC
Heading numbers can be added purely with CSS using counters. However, in order to use the correct counters, the nesting level of the heading needs to be known by CSS rules. These cannot be determined soely in CSS.
The <table-of-contents>
component determines the nesting level and provides this information.
h1
to h6
tags are assigned the correct level.
All HTML attributes of the target element are collected and passed to <table-of-contents>
.
This can be used to e.g. determine if an item is in an appendix section or regular chapter.
Table of Contents Simple Example
This example renders a table of contents with
- heading title
- page number
- links entries to the target pages, such that you can click on the TOC entries and jump to the referenced pa
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Table of Contents Complex Example
This example renders a table of contents with
- heading number (via CSS counters)
- heading title
- a leader (line of dots between title and page number)
- page number
- links entries to the target pages, such that you can click on the TOC entries and jump to the referenced page
- supports regular chapters and appendix chapters
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
Include items in TOC
1 2 3 4 5 6 7 |
|
Referencing sections in text (outside of TOC)
Headings can not only be referenced in the table of contents, but anywhere in the document.
References can be added by creating an <a>
tag that links to the id
of an heading element.
But there are some limitations in what you can reference: the nesting level cannot be determined automatically via CSS. You either have to manually specify the nesting level of the referenced heading or not include the heading number in the referenced text.
1 2 3 4 5 6 7 8 |
|
See References for examples how to reference items.