Introduction to page composition with CSS

Shinyu MURAKAMI
Antenna House, Inc.
September 4th, 2008

Preface

CSS (Cascading Style Sheets) is a type of style sheet which can be used for not only Web designs but also variety of printing uses and page media such as PDF.

Especially, CSS Level 3, which is being developed by W3C, specifies various functions necessary for professional quality formatting such as complicated page composition, multi-column layout, vertical writing and character layout for multi-lingual.CSS3 is not a monolithic specification, but devided into functional modules. Major modules are listed at (Reference specifications). In order to study CSS3, the knowledge about CSS2.1 (CSS Level 2 Revision 1, Candidate Recommendation), on which CSS3 is based, is necessary.

This document will explain CSS page composition with the Preview Version of Antenna House Formatter V5, which implements CSS3.

Table of Contents

Setup of page

@page rule

@page rule sets basic settings such as page size, margin, and page header/footer.

@page {
  size:   A4;
  margin: 25mm;
  @top-center {
    content: "Sample";
  }
  @bottom-center {
    content: counter(page);
  }
}

Size setting: size property

Width and height of a page should be set with size property.

@page {
  size: 182mm 230mm;
}
@page {
  size: 4in 6in;
}

Keywords such as A5, A4, A3, B5, B4, letter, legal and ledger can be used for the value of size property. AH Formatter supports other originally defined page names.

@page {
  size: A4; /* ISO/JIS A4 (210mm×297mm) */
}
@page {
  size: B5; /* ISO B5 (176mm×250mm) */
}
@page {
  size: JIS-B5; /* JIS B5 (182mm×257mm) */
}

Landscape orientation can be specified with a keyword “landscape”.

@page {
  size: A4 landscape;   /* A4 landscape (297mm×210mm) */
}

Margin

Page margin can be specified with margin property in @page rule.

@page {
   margin: 10%;  /* top, bottom, left, and right margins will be 10% of 
                    the page width respectively */
}
@page {
   /* top and bottom margins are 2cm, left and right margins are 3cm */
   margin-top: 2cm;
   margin-bottom: 2cm; 
   margin-left: 3cm; 
   margin-right: 3cm; 
}

Margin boxes

Page header/footer will be assigned to margin box which is the area around a page.

Margin boxes are named after the location such as @top-left, @top-center, @top-right, @bottom-left, @bottom-center, @bottom-right.

@page {
  @top-right {         /* Page header */
    content: "Sample";
  }
  @bottom-center {     /* Page footer */
    content: counter(page);
  }
}
Location of each margin box
@top-left-corner @top-left @top-center @top-right @top-right-corner
@left-top (page-area) @right-top
@left-middle @right-middle
@left-bottom @right-bottom
@bottom-left-corner @bottom-left @bottom-center @bottom-right @bottom-right-corner

Left/Right/First page setting

Margin and page header/footer can be individually specified for left, right, and first page.

@page :left {    /* left page setting */
  margin-left: 23mm;
  margin-right: 27mm;
  
  @top-left {    /* chapter title in the running head of the left page */
    content: string(Chapter);
  }
  
  @bottom-left { /* page number */
    content: counter(page);
  }
}

@page :right {   /* right page setup */
  margin-left: 27mm;
  margin-right: 23mm;
  
  @top-right {   /* chapter title in the running head of the right page */

    content: string(Section);
  }
  
  @bottom-right { /* page number */
    content: counter(page);
  }
}

@page :first {    /* setting of the first page of a document */
  /* hide page header */
  @top-right { content: normal }
  @top-left  { content: normal }
}

Running header and page number

Running header setting:string-set property and string() function

Words from headings in the body can be displayed in the page header.

@page {
  @top-left {
    content: string(Chapter);
  }
}

h1 { string-set: Chapter self; }

Page number: counter(page)

counter(page) is used for generating page numbers.

@page {
  @top-right {
    content: "Page " counter(page);
  }
}

Total pages: counter(pages)

Total pages may be output with current page number as .

@page {
  @top-right {
    content: "Page " counter(page) " of " counter(pages);
  }
}

Counter

Page counter

Current page number and total pages counter(page) / counter(pages)

Numbering chapter and section

Numbering of chapter and section can be set automatically.

body {
  counter-reset: ChapterNo;          /* reset chapter number counter */
}
h1:before {                          /* before chapter header (h1) */
  counter-increment: ChapterNo;      /* add 1 to chapter number counter */
  content: "Chapter" counter(ChapterNo)": "; 
                                     /* the output will be "Chapter 1:"  */
}
h1 {
  string-set: Chapter before self;   /* set h1:before and contents of h1 */
  counter-reset: SectionNo;          /* reset section number counter */
}
h2:before {                          /* before section header (h2) */
  counter-increment: SectionNo;      /* add 1 to section counter */
  content: counter(ChapterNo) "." counter(SectionNo) " ";  
                                     /* output will be 1.1 */
}
h2 {
  string-set: Section before self;   /* set h2:before and contents of h2 */
}
@page :left {
  @top-left {                       
                 /* section title in the running head of the left page */
    content: string(Chapter);
  }
}
@page :right {
  @top-right {                       
               /* section title in the running head of the right page */
    content: string(Section);
  }
}

Cross reference

Reference to counter (chapter and page numbers): target-counter() function

Chapter and/or page number which is referred to can be automatically added such that “Please refer to Counter”.

.ChapterRef::before {
  content: "Chapter " target-counter(attr(href, url), ChapterNo) ". ";
}
.PageRef::after {
  content: "(page " target-counter(attr(href, url), page) ")";
}
...
Please refer to <a class="ChapterRef PageRef" href="#Counters">Counter</a>.

Reference of text contents: target-text() function

Text which is referred to can be inserted. E.g.) “This Chapter

.TitleRef {
  content: target-text(attr(href, url), before)
           target-text(attr(href, url), content);
}
...
E.g.) “<a class="TitleRef" href="#CrossRef">This chapter<a>”

Creation of table of contents

Cross reference and table of contents

Table of contents can be created with target-counter() which can refer to chapter and page number (please refer to Cross reference), as following example:


.TOC a::before { /* add chapter number to the table of contents */
  content: "Chapter " target-counter(attr(href, url), ChapterNo) ". ";
}
.TOC a::after {  /* add page number to the table of contents */
  content: leader(dotted) " " target-counter(attr(href, url), page);
}
...
<div class="TOC">
  <ul>
    <li><a href="#PageSetup">Page setup</a></li>
    <li><a href="#PageHeaderFooter">Running header and page number</a></li>
    <li><a href="#Counters">Counter</a></li>
  </ul>
</div>

Leader: leader() function

With leader() function, leader (such as dots) can be added between title and page number in table of contents to right-align page numbers.

Control of page breaks

Page break: page-break-before, page-break-after

/* break page before top header(h1) */

h1 {
  page-break-before: always;
}
/* break page after this block */

div.Ending {
  page-break-after: always;
}

Page break prohibition

/* avoid page breaks just after headers (h1-h6) */

h1, h2, h3, h4, h5, h6 {
  page-break-after: avoid
}
/* avoid page breaks in this block */

div.NoBreak {
  page-break-inside: avoid;
}

Left/right page start

The first page of the chapter can be always either left or right page. Blank pages will be inserted as appropriate.

/* insert page breaks such that h2 header is always kept on the right */

h2 { page-break-before: right; }

Named page

Named page: page property

Several types of @page rules with specific names can be created and switched as appropriate.

@page Landscape {  /* named page with Landscape definition */
  size: A4 landscape;
}
@page Appendix {   /* named page with Appendix definition */
  @top-center: "Appendix";
}
table.WideTable {
  page: Landscape; /* place a large table on the side of a Landscape page */
}
div.Appendix {
  page: Appendix;  /* add Appendix to Appendix page */

Multi-column layout

Multi-column setup: column-count, column-gap, column-rule

The current block is set as the column-count to be 2. column-gap and column-rule are specified as well.

div.MultiCol {
  column-count: 2;
  column-gap: 5mm;
  column-rule: dotted green 1mm;
}

Alternative method is to specify column-width instead of column-count. The number of columns will be automatically set based on the width of page and column.

column-rule is a shorthand and has the same effect as the following example:

  column-rule-style: dotted;
  column-rule-color: green;
  column-rule-width: 1mm;

Page float

top-float and bottom-float

Page float which places targets on the top or bottom of a page (top-float/bottomfloat) is used in this current page.

This is float:left. This is float: right. This page-float is the advanced version of the traditional left/right float, which is often used in HTML and CSS layout to align pictures to the left or right and to show body text around the pictures.

This is TopFloat. float: page top

This is BottomFloat. float: page bottom

top-float/bottom-float in multi-column layout

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec fringilla. Donec luctus ante at dolor scelerisque tempor. Phasellus convallis, nisl sed sollicitudin lacinia, in pulvinar nibh eros non sem.

This is TopFloat in a column.
float: column top

Donec sit amet velit. Phasellus cons ectetuer. Pellentesque ut magna. Quisque enim turpis, fringilla id, malesuada ut, molestie sed, tellus.

This is TopPageFloat.float: page top

This is BottomPageFloat. float: page bottom

This is BottomFloat in a column.
float: column bottom

Cras commodo, pede id dapibus lacinia, nulla ante gravida libero, ac cursus neque diam at massa. Suspendisse et est id eros gravida commodo. Aenean imperdiet tristique urna.

Footnotes

Footnotes: float: footnote

Content of the element on which 'float: footnote' is specified will be output as a footnoteFootnote is the note which is written on the bottom of a page.'float: page bottom' from Page float is used for the footnote alignment. @footnote rule in @page rule can be used to set footnote area. Pseudo elements such as ::footnote-call, ::footnote-marker will be used to set style for note number..

<style>
.Footnote { float: footnote }
</style>

<p>When footnote is specified in float property, the contents will be noted 
as footnote <span class="Footnote">footnote is the note which is written 
on the bottom of a page. </span>...</p>

PDF bookmark

Bookmark setup: bookmark-level, bookmark-label

PDF bookmark can be created.

Set level in bookmark-level property to create a bookmark item. The level corresponds to the level of h1-h6 headings in HTML. Titles in bookmark contents may be set with bookmark-label property. Text contents will be used for bookmark-label if label is not specified.

h1 { bookmark-level: 1 }
h2 { bookmark-level: 2 }
h3 { bookmark-level: 3 }
h4 { bookmark-level: 4 }
h5 { bookmark-level: 5 }
h6 { bookmark-level: 6 }

CMYK color

CMYK color setup: cmyk(c, m, y, k)c, m, y, k)

CMYK color setting rather than RGB setting will be used in printing.

<p style="border: cmyk(0.5, 0.1, 0.0, 0.2) solid thick;
          background: cmyk(0, 0.3, 0.2, 0);
          color: cmyk(0.8, 0.5, 0.0, 0.3)">CMYK Color Test...

Set border color, background color, and font color with CMYK.

Rounded borders

Radius of rounded border: border-radius

To round border corners, the radius should be set with border-radius property.

<p style="border-radius: 18pt; /* radius of rounded corner*/
          border: solid green;
          padding: 6pt">Border-radius...

Border-radius (rounded border) is set.

Radii of each corner can be set individually. Corners can be set as ellipse by setting different sizes for horizontal and vertical directions of radius.

Shadowed boxes

Shadowed box setup: box-shadow

To add shadow to boxes, set box-shadow property with width, height, and color of shadow.

<p style="box-shadow: 4pt 3pt silver;
          border: solid 1pt black; padding: 6pt">...

Box-shadow (shadowed boxe) is set.

<p style="box-shadow: -6pt -4pt orange, 6pt 4pt blue;
          border-radius: 10pt; padding: 6pt">...

Multiple shadows can be specified in box-shadow property. Corners of the shadow can be rounded with border-radius property.

Hyphenation

Enabling hyphenation: hyphens: auto

.Hyphenated {
  hyphens: auto;
}
<div class="Hyphenated" xml:lang="en">
  <p>Rainbow PDF Software Products are ...

Rainbow PDF Software Products are developed by Antenna House. Antenna House, Inc. is a premier software company founded in 1984 in Tokyo, Japan. Our mission is to create and sell products to make data useful.

For over 20 years we have been reliably retrieving information and delivering it to our customers in formats and languages our customers can use.

Millions of copies of our software have saved time for individual desktop users, small businesses, and have cemented our leader ship role in high end documentation.

Japanese text composition

Setup for Japanese text composition

The following is the basic style setting for Japanese text composition.

body {  /* setup for Japanese document composition */
  punctuation-trim: start end adjacent;
  text-justify-trim: punctuation;
  text-autospace: ideograph-numeric ideograph-alpha;
}
p {     /* paragraph */
  text-align: justify;  /* line end align */
  text-indent: 1em;     /* set 1em for first line indent in a paragraph */
  margin: 0;            /* no margin between paragraphs */
}

The following sections describe the properties for Japanese text composition.

Trimming of fullwidth punctuation: punctuation-trim

  /* fullwidth punctuation character is trimmed at the start or end of a
     line, or adjacent to another fullwidth punctuation character. */
  punctuation-trim: start end adjacent; 

Full-width punctuation character (comma, period, and brackets) should be trimmed if it appears at the start/end of line and/or adjacent to another fullwidth punctuation character. E.g.)

「《約物〔やくもの〕》、つまり『括弧』・『句読点』の類(たぐい)です。」

The following is the example of deactivated setting of closing up full-width punctuation marks (set as punctuation-trim: none). E.g.)

「《約物〔やくもの〕》、つまり『括弧』・『句読点』の類(たぐい)です。」

Compression for text justification: text-justify-trim

  /* punctuation marks will be closed up for justification. */
  text-justify-trim: punctuation;

This specification allows to close up full-width punctuation marks and brackets to justify ends of line.

Space between Japanese/alphabetic texts: text-autospace

  /* adding a space between Kanji/Hiragana and numbers or Kanji/Hiragana
 and alphabets */
  text-autospace: ideograph-numeric ideograph-alpha;

When alphabets and numbers are included in Japanese sentences, space will be added. E.g.)

「日本語にもglobalにも100%を目指すAH Formatter V5です」

By setting text-autospace: none, space insertion between Japanese letters and alphabets will be deactivated. E.g.)

「日本語にもglobalにも100%を目指すAH Formatter V5です」

Vertical writing mode

Setup of vertical writing mode: writing-mode: tb-rl

To set the whole documents as vertical writing mode, writing-mode property is set in the body in HTML document (set in root in XML documents).

body {
  writing-mode: tb-rl;   /* vertical writing */
}

The following example is a writing-mode property which sets a block as vertical orientation.

div.VerticalTextBlock {
  writing-mode: tb-rl;   /* vertical writing */
  height: 16em;          /* number of characters in a line */
  padding: 3pt; border: ridge green;
}

日本語は伝統的に縦書きで組まれます。書籍や雑誌など出版物の多くは今も縦書きが主流です。もちろん、Antenna House Formatterは縦書きにも対応しています。このように、部分的にブロックを縦書きにすることも、文書全体を縦書きにすることもできます。

縦書きの指定はwriting-mode: tb-rlです。tb-rlは、文字の進行方向が上から下(top-to-bottom)、行とブロックの進行方向が右から左(right-to-left)を意味しています。

横書きの指定はwriting-mode: lr-tb(left-to-right、top-to-bottom)です。アラビア語やヘブライ語など右から左に書く言語の横書きの場合はwriting-mode: rl-tb(right-to-left、top-to-bottom)です。

縦書きの中に「'08年8月29日」のように部分的に数字などを横書きにすることを「縦中横」といいます。

TATECHUYOKO (horizontal-in-vertical notation)

The following example is the setting for TATECHUYOKO (horizontal-in-vertical notation), which accepts horizontal orientation words within a vertical orientation sentence.

/* horizontal-in-vertical notation */
span.TateChuYoko {
  display: inline-block;/* create a small block in the middle of a row */
  writing-mode: lr-tb;  /* set this small block as horizontal orientation */
  text-align: center;   /* align text to the center */
  text-indent: 0;       /* do not add text indent (erase text-indents from 
                           the paragraph p) */
  line-height: 1;       /* set line gap not to be overlap with above and 
                 below of TATECHUYOKO (horizontal-in-vertical notation) */
}

<div class="VerticalTextBlock">
  ...
  <p>縦書きの中に「<span class="TateChuYoko">'08</span>年8月
  <span class="TateChuYoko">29</span>日」のように部分的に数字などを
  横書きにすることを「縦中横」といいます。</p>
</div>

縦書きの中に「'08年8月29日」のように部分的に数字などを横書きにすることを「縦中横」といいます。

MathML and SVG graphic

MathML formula such as x = b ± b 2 4 a c 2 a and SVG vector graphic can be embedded.

<p>MathML formula
  <math xmlns="http://www.w3.org/1998/Math/MathML">
    <mrow><mi>x</mi><mo>=</mo><mfrac>...</mfrac></mrow>
  </math>
and SVG vector graphic
  <svg xmlns="http://www.w3.org/2000/svg" width="70" height="65"
    viewBox="0 0 70 65">
   <g style="fill-opacity:.5; stroke:black; stroke-width:2;">
     <circle cx="35" cy="20" r="19" style="fill:red;"/>...
   </g>
 </svg>
can be embedded.</p>

Reference specifications

In the previous chapters, practical use of CSS page composition was introduced. The following list is the related specifications which Antenna House Formatter is implementing.