Every tabelline table carries its own 6 KB of CSS, and a page with many tables carries it many
times: almost a third of the site is repeated CSS. What a table's HTML holds, three ways to share its
style, the one we chose, and a CSS trick that keeps a page's theme winning and losing exactly as
before.
The Reference has 152 tables and weighs
1.7 MB: most of it is CSS, the same rules 152 times. Pietro: "We should share CSS as much as possible,
how to do that?" This post is the answer, from what a table is made of to how the change is
built. It is the OpenSpec change share-table-styles, built for 0.2.
CSS in five minutes
All the CSS the rest of the post needs, from zero.
A rule says how some elements look: a selector that picks them, and declarations
(property and value) in braces.
.tb_title { font-size: 125%; color: navy; }
Selectors.td picks every element named td; .tb_title every element with that class
(class="tb_title"); #planets the one element with that id (id="planets"). A space means
"inside": #planets .tb_title is a title inside the element with id planets.
Where rules live. In a style element, anywhere in the page, they apply to the whole page,
wherever they sit. In a style attribute (style="color: navy") they apply to that one
element.
Who wins. When two rules set the same property on the same element, the heavier selector
wins. Weight is counted as ids first, then classes, then element names: one id beats any number
of classes. Only when the weights are equal does the later rule win.
Variables. A property whose name starts with -- is a variable: --size: 14px.
var(--size, 16px) reads it, or uses 16px when it is not set. A variable set on an element is
seen by everything inside it, so setting one on a table's div reaches all its cells.
What a table's HTML holds
toHtml(t) writes three things, one inside the other: a div with an id, a style element,
and the table itself.
The style is the table's look, made from its options: fonts, borders, colours, paddings. Every
rule starts with the table's id, so it reaches only that table: two tables with different
options on one page do not disturb each other. That is how gt and Great Tables do it too.
What is not in it: the data, and the styles of single cells (tabStyle), which sit on the cells
themselves. So the 90 rules are the same for every table; only the values change, and the id.
How much of the site it is
Measured on the built site of 11 November, the book not counted.
Repeated CSS
the pages with the most table styles, and their size with one stylesheet
page (KB)
table styles (KB)
after (KB)
tables
different looks
Reference
1,710
854
875
152
4
Specs
699
400
318
72
2
Designing tabelline 0.1
462
282
195
51
7
Milano Cortina
512
193
334
32
5
Guide
517
171
365
29
8
Designing tabelline 0.2
335
148
202
24
2
Rethinking the datasets
289
96
207
16
1
All 25 pages with tables: 8.50 MB, of which 2.59 MB table styles; 6.29 MB after. The book's 23 pages: 7.54 MB, 6.59 MB after. Measured on 11 November.
The specs page has 72 tables and only two looks: 400 KB of CSS where 13 KB would do.
Three ways to share
A class for each look. Name each table's style after a hash of it (tb-s-1a2b3c4d), scope
the rules to that class, and write each style once per page. Tables with the same options share
it. It works, but it needs hashes, a list of the styles already written, and three new procs, and
every different look still costs 6 KB.
Merge after the page is built. Read every table's CSS once the page is done, and merge the
rules that are the same. It keeps everything as it is, but needs a CSS parser and a step after
nimib saves the page: the most work for the same result.
One stylesheet, options as variables. The 90 rules become one fixed stylesheet,
tabellaCss, the same for every table, and each value is read from a CSS variable with the
default as fallback:
We chose the third. It is the simplest to explain (one stylesheet, options as variables), there is
nothing to keep track of, and the 152 tables of the Reference cost one stylesheet and a few
variables each: the page goes from 1.71 MB to 0.88 MB (153 KB to 104 KB compressed). The stylesheet is 13 KB, twice a table's old style, because every value carries
its var(...); a page pays it once. The last column of the table above is what the pages weigh
with it. It also gives something new: a page can set an option for all its tables at once,
.tabella { --tb-table-font-size: 14px }.
Using it
In nimib nothing changes: nbTable t writes tabellaCss with the first
table of a page and leaves it out of the others. Alone, t.toHtml works as today: the stylesheet
comes in front of the table, ready to paste anywhere. A page or a tool of your own writes
tabellaCss once, or links it as a file for a whole site, and then t.toHtml(withStyle = false)
for every table.
The risk, and a trick
When two CSS rules set the same thing on the same element, the one with the heavier selector
wins: an id weighs more than any number of classes, a class more than any number of element
names. Only when the weights are equal does the later rule win.
Today every tabelline rule names an id, so it beats almost any rule of a page's theme. The
plain version of the stylesheet names only classes (.tabella .tb_title), which is lighter: a
theme rule that lost before could now win, and a table would change its look on some page, with
no change to the table.
The fix is a CSS idiom. :not(#_) matches every element whose id is not _, that is every
element, and it weighs as much as an id. :where(...) matches what is inside it and weighs
nothing. So every rule of the stylesheet starts with
which matches every table and weighs exactly what #tabella-8f3a .tb_title weighs today.
Weights, from light to heavy: .tabella .tb_title is two classes; #tabella-8f3a .tb_title
and :where(.tabella):not(#_) .tb_title are one id and one class. Here is what happens to two
rules a page's theme could have, against the table's own rule for the title.
Who wins
a theme's rule against the table's
theme rule
today
plain classes
with the trick
main .tabella .tb_title
loses
wins
loses
#page .tabella .tb_title
wins
wins
wins
Checked in Chromium; :where is in every browser since 2021.
To check it, the computed style of every element of every table of the site and the book (48,597
elements, 39 properties each: colours, fonts, borders, paddings, sizes) was read in Chromium
before and after the change: the same, all of them.
How it is built
From the change share-table-styles, in the order it will be written.
tabellaCss, a constant in render_html.nim: the rules of today's tableCss, each starting
with :where(.tabella):not(#_), each value var(--tb-<option>, <default>), where the option
is the field of TabOptions in kebab-case (tableFontSize is --tb-table-font-size). An
option that falls back to another nests: var(--tb-heading-background-color, var(--tb-table-background-color, white)). The nanoplot rules are in it too.
toHtml compares the table's options with the defaults and writes each one that differs as a
variable on the div. What Nim works out today (white text on a dark heading) it still works
out, and writes as a variable.
toHtml(t, withStyle = true): with the stylesheet in front, as today; false without.
The rules of one table only stay in a style of that table, scoped to its id: optCss, and
the vertical lines between columns (a rule that must come after the others).
A table that takes the page's theme (optCssTheme(), as every table of this site) would
carry the same 18 variables as all the others, pointing to the page's colours (--ink,
--paper, ...). It gets the class tb_css_theme instead, and one rule of the stylesheet
sets those variables for every such table; the table carries only what it changes from the
theme.
nbTable keeps a flag in the page's context: the first table writes tabellaCss, the others
do not. nb.tableHtml(t) does the same for blocks that put a table inside their own HTML.