Structural Selectors
CSS picks out an element by where it stands among its siblings with :nth-child(), :nth-last-child(),
:first-child and :last-child. Here the same four are keys of the container's style: a table's style picks out its
rows, a list's its items, a columns node's its columns. Each rule is an index and a block of style keys, so banded rows,
a bold totals line or a highlighted first column need no style on any row.
| Key | Takes | What it picks |
|---|---|---|
nthChild | an object: each key an An+B index, each value a block of style keys | The children at those positions, counted from the first at 1. |
nthLastChild | the same | The children at those positions, counted from the last at 1: -n+2 is the last two. |
firstChild | a block of style keys | The first child, the same as nthChild 1. |
lastChild | a block of style keys | The last child, the same as nthLastChild 1. |
{
"styles": {
"zebra": {
"nthChild": { "even": { "backgroundColor": "#e9f0f8" } },
"lastChild": { "fontWeight": "bold", "backgroundColor": "#c7d7e8" }
}
},
"content": [
{ "table": { "style": "zebra", "widths": [3, 1], "rows": [
[ { "th": "Stage" }, { "th": "Amount" } ],
[ { "td": "Survey and design" }, { "td": "400" } ],
[ { "td": "Groundworks" }, { "td": "1,250" } ],
[ { "td": "Frame and roof" }, { "td": "2,900" } ],
[ { "td": "Total" }, { "td": "4,550" } ]
] } }
]
}
The header row is in neither count, so "Survey and design" is the first row and "Groundworks" is banded; the total, the
fourth row, is even too, and is bold on the deeper band because lastChild is written after nthChild.
Where they apply
| Container | Its children | What reaches a child |
|---|---|---|
| a table | its body rows: the run of rows at the top whose every cell is a th is the header and is not counted | the keys that apply to a table cell, laid on each cell of the row |
a list (ol, ul) | its items; a list nested under an item is not an item of the outer list | the keys the item's text inherits, and a marker block |
| a columns node | its columns | the whole block: the column is the box it styles, padding and background included |
Rows a table binds from data with source are counted like written ones, from the first row after the header, and
the literal rows written after them count on from there; the count carries across page breaks, so banding continues on
the next page under a repeated header. On any other kind of node - a paragraph, a heading, a cell, an image, a rule, an
svg, a table of contents, a header or footer, a watermark - the four keys pick nothing and the render report says so.
A key in a rule that cannot reach the child is named with a NOTICE: padding in a list's item rule, for instance,
since an item draws no box of its own.
Cells within a row
A row rule may hold its own nthChild (or any of the four), which picks the row's cells. It is the one place a rule
looks inside a child.
{
"styles": {
"lines": {
"nthChild": {
"even": { "backgroundColor": "#eef2f7" },
"n": { "nthChild": { "n+2": { "textAlign": "right" } } }
},
"nthLastChild": { "3": { "borderTop": "1pt solid #1e3a5f" } },
"lastChild": { "fontWeight": "bold", "backgroundColor": "#dbe7f3" }
}
},
"content": [
{ "table": { "style": "lines", "widths": [3, 1, 1], "rows": [
[ { "th": "Item" }, { "th": "Qty" }, { "th": "Amount" } ],
[ { "td": "Antifouling" }, { "td": "2" }, { "td": "180.00" } ],
[ { "td": "Sail repair" }, { "td": "1" }, { "td": "240.00" } ],
[ { "td": "Bilge pump" }, { "td": "1" }, { "td": "95.00" } ],
[ { "td": "Subtotal" }, { "td": "" }, { "td": "515.00" } ],
[ { "td": "VAT" }, { "td": "" }, { "td": "103.00" } ],
[ { "td": "Total" }, { "td": "" }, { "td": "618.00" } ]
] } }
]
}
n picks every row, and its inner n+2 every cell from the second on, so every amount is right-aligned by one rule.
Counting from the end, 3 rules a line above the subtotal, and lastChild sets the total in bold.
The An+B index
A key of nthChild or nthLastChild is CSS's An+B (CSS Syntax 3 6): the positions A × n + B for n = 0, 1, 2 ...,
counting the first child as 1. Only the positive results are used.
| Index | Picks |
|---|---|
odd | 1, 3, 5 ... (the same as 2n+1) |
even | 2, 4, 6 ... (the same as 2n) |
3 | the third child only |
n | every child |
3n | 3, 6, 9 ... |
2n+1 | 1, 3, 5 ... |
n+7 | the seventh and every one after it |
-n+3 | the first three: 3, 2, 1 |
0n+0, or a formula whose results are all below 1 | nothing |
The index is written in lower case, as every word of this vocabulary is: odd, never ODD. Spaces are allowed around
the sign before B (2n + 1) and nowhere else: 3 n, + 2n and 10n+-1 are refused at the door, as CSS refuses them.
Which rule wins
Every rule that picks a child applies to it, in the order the rules are written, so where two set the same key the
later one wins, key by key. A child's own style wins over every rule, where CSS would compare specificity: a cell
written { "td": "1,250", "style": "num" } keeps num's keys and takes from the rules only what num leaves unset.
{
"styles": {
"podium": {
"nthChild": {
"even": { "backgroundColor": "#eef4f6" },
"1": { "backgroundColor": "#f6e08a", "fontWeight": "bold" },
"2": { "backgroundColor": "#dde1e6", "fontWeight": "bold" },
"3": { "backgroundColor": "#e9c39a", "fontWeight": "bold" }
},
"nthLastChild": { "1": { "fontStyle": "italic", "color": "#6b7280" } }
}
},
"content": [
{ "table": { "style": "podium", "rows": [
[ { "th": "Place" }, { "th": "Runner" } ],
[ { "td": "1" }, { "td": "A. Okafor" } ],
[ { "td": "2" }, { "td": "M. Lindqvist" } ],
[ { "td": "3" }, { "td": "J. Moreau" } ],
[ { "td": "4" }, { "td": "S. Patel" } ],
[ { "td": "5" }, { "td": "R. Kowalski" } ]
] } }
]
}
even is written first, so the silver second row keeps its own fill where the two meet, and the fourth row is banded.
In a table of one row, that row is gold, bold, grey and italic at once: every rule that picks it applies.
Lists and columns
{
"styles": {
"ticks": {
"firstChild": { "fontStyle": "italic" },
"nthChild": { "even": { "fontWeight": "bold", "color": "#155E75" } },
"lastChild": { "color": "#B91C1C", "textDecoration": "underline" }
},
"rail": {
"firstChild": { "color": "#7C2D12" },
"nthChild": { "2": { "backgroundColor": "#e9f0f8", "padding": 6 } },
"lastChild": { "borderLeft": "1.5pt solid #155E75", "paddingLeft": 8 }
}
},
"content": [
{ "ul": { "style": "ticks", "li": [
{ "p": "first" },
{ "ul": { "li": [ { "p": "nested under first: not an item of the outer list" } ] } },
{ "p": "second" },
{ "p": "third" },
{ "p": "fourth" }
] } },
{ "columns": { "style": "rail", "widths": [1, 1, 1], "rows": [
[ { "p": "First column: brown text." } ],
[ { "p": "Second column: a tinted panel." } ],
[ { "p": "Last column: ruled off on the left." } ]
] } }
]
}
In the list, "third" is the third item, since the nested list takes no place; the even items are bold teal, and the last is red and underlined. In the columns block each column takes its rule's whole block, padding and border included.
Not taken from CSS
The selector's of S argument, :nth-of-type(), :nth-last-of-type() and :only-child are not offered, and there is
no selector for a page by its number (CSS Paged Media 3 defines none). For a style per page, start a
section.
A style's position keys travel with it: a component that defines a banded table style bands every table that names it, and a document that redefines the style replaces it whole.
In a DOCX
The rules are resolved before either file is written, so the DOCX carries the same shading, alignment and weight on the same cells, items and columns, as direct formatting: its document markup is byte for byte what the same document gives with the style written on every row by hand.