Using CSS with the speedata Publisher
CSS (Cascading Stylesheets) is a language mainly used on the web to determine the appearance of objects. The idea is to separate formatting from content. On the web - at least in theory - the content is described in HTML, while the appearance is determined by CSS. With the speedata Publisher the separation is even clearer. The data usually contains no information about the appearance, while the layout rules have no information about the data.
CSS is used in two distinct places in the speedata Publisher, which must be kept apart:
- CSS for layout commands: Paragraphs, text blocks, tables and many other commands in the layout rules take fonts, colors, spacing and alignment from CSS rules.
- CSS for HTML content and markup in the data: Content that is processed via the
<HTML>command or as markup within paragraph contents is formatted with the full HTML processing of the Publisher.
Both paths use the same stylesheets.
Including stylesheets
A CSS stylesheet can be available and integrated as an external file.
Alternatively, one can write CSS instructions directly into the layout rules.
<Stylesheet> is the command for both variants:
<Stylesheet filename="style.css"/>
or
<Stylesheet>
td {
vertical-align: top;
}
</Stylesheet>
CSS for layout commands
Many commands in the layout rules can be styled with CSS: paragraphs, text blocks, tables, boxes, rules, lists and some more.
Selectors and the cascade work as in a web browser.
An attribute on the command takes precedence over a CSS rule, so you can keep the defaults in the stylesheet and override them where needed.
With the attribute style, CSS declarations can also be written directly on a command, for example from the data.
A complete example: the box gets its background color from the class warn, the paragraph its font size and color from paragraph.note.
<Layout xmlns="urn:speedata.de:2009/publisher/en"
xmlns:sd="urn:speedata:2009/publisher/functions/en">
<Stylesheet>
.warn {
background-color: red;
}
paragraph.note {
font-size: 8pt;
color: gray;
}
</Stylesheet>
<Record element="data">
<PlaceObject>
<Box class="warn" width="5" height="2"/>
</PlaceObject>
<PlaceObject>
<Textblock>
<Paragraph class="note"><Value>Small gray text.</Value></Paragraph>
</Textblock>
</PlaceObject>
</Record>
</Layout>
Element names
In selectors, a command is called by its name in lower case.
Only <Image> is called img, as in HTML.
| Command | Name in selectors |
|---|---|
<Box>, <Circle>, <Frame> |
box, circle, frame |
<Image> |
img |
<Paragraph>, <Span>, <U> |
paragraph, span, u |
<PlaceObject> |
placeobject |
<Rule> |
rule |
<Table>, <Tablehead>, <Tablefoot> |
table, tablehead, tablefoot |
<Tr>, <Td>, <Tablerule> |
tr, td, tablerule |
<Textblock> |
textblock |
<Ul>, <Ol>, <Li> |
ul, ol, li |
The reference gives the name in the section “CSS” of each of these commands.
Other commands such as <Record>, <ForAll>, <Loop> or <Switch> produce no output and do not appear in selectors.
Selectors
The selectors from CSS apply, among others:
| Selector | matches |
|---|---|
td |
every table cell |
.price |
every command with class="price", also with class="price sale" |
#head |
the command with id="head" |
td.price |
table cells with the class price |
table.list td |
cells inside a table with the class list |
tr > td |
cells directly inside a row |
tr:nth-child(even) |
every second table row (see Tables) |
:is(tablehead, tablefoot) td |
cells in the table head and the table foot; :where() works the same, but does not count for the specificity of the rule |
For descendant selectors such as table.list td, the nesting while the layout is executed counts.
A paragraph in a <Record> that is called with <ProcessNode> from a table cell is inside the cell (td paragraph).
The attributes class and id can contain XPath expressions in curly braces, so the class can come from the data:
<Tr class="{if (@sale = 'yes') then 'sale' else ''}">
One stylesheet for layout and HTML
All rules from all <Stylesheet> commands apply to layout commands and to HTML content.
Some names exist in both worlds, for example table, td, span, ul or img.
A rule td { padding: 4pt } therefore applies to table cells in the layout and in HTML content.
The two root elements keep them apart: layout commands are below the element layout, HTML content is below html.
layout td { padding: 2pt } /* only cells from <Td> in the layout */
html td { padding: 4pt } /* only cells in HTML content */
td { color: black } /* both */
There is one difference for the borders of table cells: td { border: 1pt red } draws a line on <Td>, but not in HTML content, because there a border style is needed, as in a browser (see Borders of table cells).
With td { border: 1pt solid red }, the cells look the same in both.
Cascade and precedence
All matching rules apply. If two rules set the same property, the more specific one wins (an id before a class before an element name); with equal specificity, the rule that comes later in the stylesheet wins.
If a value comes from several places, this order applies, strongest first:
- A declaration with
!importantin the attributestyle. - A CSS rule with
!important. - The attribute on the command.
- The attribute
style. - The CSS rules for the command itself.
- Values the command inherits from the surrounding commands (see the next section).
- The defaults of the Publisher, such as the text format
textand the font familytext.
Some attributes stand for several CSS properties.
If the attribute is set, CSS rules and inherited values for these properties do not apply (except with !important):
| Attribute | stands for |
|---|---|
textformat |
text-align, text-indent, margin-top, margin-bottom, hyphens, letter-spacing, break-before, break-after, orphans, widows |
fontfamily |
font-family, font-size, line-height |
color |
color |
align (on <Tr> and <Td>) |
text-align |
<Stylesheet>
paragraph { color: gray }
paragraph.important { color: red !important }
</Stylesheet>
…
<Paragraph color="blue">…</Paragraph> <!-- blue -->
<Paragraph color="blue" class="important">…</Paragraph> <!-- red -->
Inheritance
As in CSS, the inner commands inherit font and text properties from the surrounding ones.
The inherited properties are color, font-family, font-size, font-style, font-weight, line-height, text-align, text-indent, hyphens, letter-spacing, orphans and widows.
<Textblock>, <Paragraph>, <Table>, <Tablehead>, <Tablefoot>, <Tr> and <Td> pass these values on to their contents.
Likewise, a text-decoration (for example on a table cell) applies to the text inside.
<Stylesheet>
textblock.note { color: gray; font-size: 8pt; text-align: right }
</Stylesheet>
…
<Textblock class="note">
<Paragraph><Value>gray, 8pt, right aligned</Value></Paragraph>
<Paragraph color="black"><Value>black, 8pt, right aligned</Value></Paragraph>
</Textblock>
Relative values such as font-size: 1.2em refer to the font of the surrounding command.
An attribute also replaces the inherited values it stands for: a paragraph with fontfamily="..." inherits no font size, and a text block with textformat="..." does not pass an alignment from CSS on to its paragraphs.
The attribute style
All commands with the attribute class also have the attribute style.
It contains CSS declarations for this one command, like the attribute of the same name in HTML.
This way, formatting can come directly from the data:
<Tr>
<ForAll select="Column">
<Td style="{@style}"><Paragraph><Value select="@text"/></Paragraph></Td>
</ForAll>
</Tr>
with data such as
<Column style="background-color: #ffe0e0; border: 1pt solid #ff8c00" text="Attention"/>
- The declarations take precedence over all CSS rules, including more specific ones.
A rule with
!importantwins overstyle, a declaration with!importantinstylewins over both. - An attribute on the command takes precedence over
style, unless the declaration instyleis marked!important. This differs from a browser, wherestylewins over attributes such aswidth: in the layout rules, attributes are explicitly set values and also take precedence over the CSS rules. - The values are inherited like those from rules:
<Tr style="color: red">colors the text of all cells in the row. - Shorthands,
var()and variables are allowed, for examplestyle="--accent: green; border: 1pt solid var(--accent)". - An empty value, for example because the attribute is missing in the data, is ignored.
Shorthands
Shorthands combine several properties.
For layout commands, the Publisher splits the following shorthands into their longhands: padding, margin, border, border-top (and the other sides), border-width, border-style, border-color, border-radius and list-style.
This happens during the cascade, so as in CSS, a later or more specific declaration replaces exactly the longhands it sets.
td { padding: 2pt; padding-left: 6pt } /* 6pt on the left, 2pt elsewhere */
td.tight { padding: 1pt } /* 1pt everywhere, also on the left */
Where the table in the next section lists a longhand (for example padding-* or border-*-width), the shorthand works as well.
The attribute padding on <Table>, <Td> and <Image> hides all four sides from CSS, unless they are marked !important.
Unlike in CSS, a border shorthand on layout commands sets only the parts it names.
td { border-right: 0.25pt } changes only the width, style and color stay as they are.
In CSS and in HTML content, the same declaration resets the style to none and the color to the text color.
font and background are not split for layout commands.
Properties of the commands
| Command | CSS properties |
|---|---|
paragraph |
color, background-color, font-family, font-size, font-weight, font-style, text-decoration (underline, overline, line-through, also with style and color), line-height, text-align, text-indent, hyphens, letter-spacing, margin-top, margin-bottom, break-before, break-after, orphans, widows |
textblock |
the inherited properties (see above) and width, column-count, column-gap |
table |
the inherited properties, padding and padding-* (default for all cells), border-collapse |
tablehead, tablefoot |
the inherited properties |
tr |
the inherited properties, background-color, vertical-align |
td |
the inherited properties, vertical-align, background-color, padding-*, border-*-width, border-*-style, border-*-color (see Borders of table cells), transform (only rotate()) or rotate for the attribute rotate as well as background-text and the other background attributes |
tablerule |
height (rule width), background-color (color), rule-start |
span |
background-color, background-padding-top, background-padding-bottom, letter-spacing |
u |
border-style (dashed for a dashed line) |
ul, ol |
color, list-style-type, list-style-position, padding-left; for the marker li::marker with content, color and padding-right |
box |
background-color, padding-* |
circle |
background-color, color (frame color) |
frame |
border-*-radius (also as border-radius) |
img |
padding, padding-* |
placeobject |
background-color |
rule |
height (rule width), background-color (color) |
Shorthands such as padding, border or list-style work wherever their longhands are listed (see Shorthands).
The command reference notes the name of the CSS property for every attribute that corresponds to one (for example “CSS property: background-color” for <Box>).
font-family names a font family from <DefineFontfamily>, a font from @font-face or <LoadFontfile>, or one of the generic families serif, sans-serif and monospace.
Several names, separated by commas, are tried in order.
With font-size or line-height, a new font family is created when needed; there is no need to define a matching one first.
In @font-face, font-weight selects the face: bold or a number of 600 or more gives the bold face, normal or a smaller number the regular face.
font-style: italic or oblique gives the italic face.
If a family has no regular face, for example because only font-weight: 900 is declared, one of the declared faces is also used for normal text.
<Stylesheet>
@font-face { font-family: "Quote"; src: local("CrimsonPro-Regular") }
@font-face { font-family: "Quote"; font-weight: bold; src: local("CrimsonPro-Bold") }
paragraph.quote { font-family: Quote, serif; font-size: 11pt; line-height: 14pt }
</Stylesheet>
Tables
With CSS, the look of a table can be kept apart from its structure. The commands only contain classes; colors, font and alignment are in the stylesheet:
<Stylesheet>
table.prices { font-size: 9pt; padding: 2pt 4pt }
table.prices tablehead td { color: white; font-weight: bold }
table.prices tablehead tr { background-color: #37586e }
table.prices tr:nth-child(even) { background-color: #e6edf1 }
table.prices td:first-child { font-weight: bold }
table.prices td:nth-child(5) { text-align: right }
table.prices tr.sale { color: #a33a2a }
</Stylesheet>
Rows and cells know the pseudo classes :first-child and :nth-child() with even, odd and formulas such as 3n+1:
- For
tr, the position of the row counts. The rows in the table head, the table foot and the body are counted separately, so the zebra stripes in the body start independently of the head. - For
td, the position of the cell in the row counts. A cell withcolspancounts once, as in HTML. :last-childand:nth-last-child()have no effect, because the following rows and cells are not known yet when a command is evaluated. Commands other than<Tr>and<Td>do not know their position.
The position refers to the whole table, not to the page: when the table breaks, the zebra stripes continue on the next page.
To restart them on every page, use sd:alternating() with eval-on-split (see Alternating line colors).
A table head that is evaluated again on every page keeps the rules of the table.
The complete example is in the examples repository.
Borders of table cells
Cells get their borders from border and the longhands border-*-width, border-*-style and border-*-color, or from the attributes border-top, border-top-color etc. on <Td>:
<Stylesheet>
table.list td { border-bottom: 0.5pt solid #999 }
table.list tablehead td { border-bottom: 1pt solid black }
</Stylesheet>
- Without a border style,
<Td>draws a solid line, sotd { border: 1pt red }gives a red border. In a browser and for table cells in HTML content, a border without a style is not drawn. noneandhiddenremove the border of a side. Withborder-collapse: collapse,hiddenalso removes the border of the neighboring cell at this edge. An attribute such asborder-top="1pt"takes precedence overnoneorhiddenfrom a rule, except with!important.solid,dashed,dottedanddoubleare drawn. The dashes and dots start at the edge of the table and keep the same rhythm across all cells, so a dashed table rule looks even. A double line needs a width of at least 3pt to show two lines.groove,ridge,insetandoutsetare drawn as a solid line for now, with a warning.- With
border-collapse: collapseand different borders on a shared edge, the wider border wins as in CSS; with equal widths the style wins in the orderdouble,solid,dashed,dotted, otherwise the cell to the left or above. The winning border sets style and color of the edge. If both borders are solid (or have no style), each cell draws its half of the line in its own color, as before. currentcoloras the border color is the text color of the cell from CSS, otherwise black.
CSS for HTML content and markup in the data
HTML content (see the chapter HTML) and paragraph contents with markup are typeset internally by the same HTML machinery. The included stylesheets therefore also apply to this content.
Text markup in the data works as follows:
<p>Text, Text, Text <b>bold</b>, Text Text</p>
The Publisher will ensure that the text within the ‘b’ element appears in bold.
You can also add CSS styles to your own elements. For example, if you have the following data
<data>hello <green>green</green> world</data>
you can use CSS to color the element:
<Layout
xmlns="urn:speedata.de:2009/publisher/en"
xmlns:sd="urn:speedata:2009/publisher/functions/en">
<Stylesheet>
green {
color: green;
}
</Stylesheet>
<Record element="data">
<PlaceObject>
<Textblock>
<Paragraph>
<Value select="."/>
</Paragraph>
</Textblock>
</PlaceObject>
</Record>
</Layout>
Besides the color, other properties can be set this way, for example font-weight: bold, font-style: italic, text-decoration: underline and background-color.
The font of the paragraph remains authoritative: font-size alone only takes effect in paragraph contents if the text format in use has cssfontsize="yes" set (see <DefineTextformat>).
An element with a font-family of its own is set in this font, in the size of the paragraph unless font-size on the element says otherwise.
This way you can, for example, insert symbols from an icon font into the text, see the how-to Icons from an icon font.
Supported CSS properties
The following properties are evaluated by the HTML processing:
- Font:
font-family,font-size,font-style,font-weight(bold,normalor numbers, bold from 600 on) - Text:
color,text-align,text-decoration(ortext-decoration-linewithunderline,overlineandline-through,text-decoration-stylewithsolid,double,dotted,dashedandwavy,text-decoration-color),line-height,white-space(normal,pre),vertical-align(super,sub) andhyphens(noneormanualturns off hyphenation) - Box model:
margin-*,padding-*,border-*(width, style and color per side plusborder-radiusper corner),width,height. As in a browser, a border needs a style,border: 1pt blackdraws nothing. On table cells,groove,ridge,insetandoutsetare drawn as a solid line for now (see Borders of table cells). - Background:
background-color - Lists:
list-style-type,list-style-position,list-styleand the pseudo-elementli::marker(content,color,font-family,font-size,padding-right,padding-bottom) - Pseudo-elements
::beforeand::afteron block and inline elements:contentas well ascolor,font-family,font-styleandfont-weight; on block elements,::beforealso supportswidthandmargin-left. The pseudo-element inherits the properties of its element. - Tables:
border-collapse - Breaks:
break-before,break-after
A declaration with !important wins over the other rules and over the style attribute.
Not every property is fully implemented in every situation. The current state of individual properties is documented in the test status in the HTML chapter.
CSS variables
CSS variables (custom properties) let you define a value once and use it in many places, for example a corporate color or a standard spacing.
A variable is a property whose name starts with two hyphens.
var() inserts its value.
<Stylesheet>
:root {
--brand-color: #c00;
--spacing: 2mm;
}
paragraph.title { color: var(--brand-color) }
td { padding: var(--spacing) 0 }
table.compact { --spacing: 1mm }
html h2 { color: var(--brand-color) }
</Stylesheet>
Variables apply to layout commands and HTML content alike:
:rootmatches the root of both (layoutandhtml). Variables set there apply everywhere.- Variables are always inherited, by every command and every HTML element inside. An inner command can override them: with
table.compact { --spacing: 1mm }, all cells of this table get the smaller spacing. - HTML content in a paragraph or in the
<HTML>command inherits the variables of the surrounding layout commands. Withtd.warning { --brand-color: orange }, anh2heading in the HTML content of this cell also appears in orange. - A second argument gives a fallback in case the variable is not defined:
color: var(--accent, black). The fallback may itself containvar(). - Variables may refer to other variables (
--frame: 1pt solid var(--brand-color)) and appear in shorthand properties (padding: var(--spacing) 0). - Names are case-sensitive:
--Colorand--colorare two different variables. - The attribute
stylein HTML content and on layout commands can set and use variables.
If the Publisher cannot insert a variable, because it is not defined and there is no fallback, or because variables refer to each other in a cycle, it ignores the declaration and gives a warning. Whatever would apply without this declaration takes effect: another matching rule, the attribute or the inherited value. A web browser behaves differently here, it resets the property to the inherited or the initial value.
var() only works in the property values of the stylesheet.
There is no var() in the attributes of the layout commands (<Td padding="var(--spacing)">); use XPath expressions and layout variables instead.
@property, env() and calc() are not supported, nor is var() in @page, @font-face and @media.