春晓江南 發表於 2026-5-3 22:17:01

css打印样式设计举例详解

<h3>前言</h3>
<p>如果你对许多从事网页开发的人提起打印样式,他们第一反映会想到使用打印CSS。当网页文档需要被打印时,我们已经习惯于创建一个打印样式表。这些样式表确保打印版本清晰并且保证用户不会打印出巨幅图像。然而,CSS也被用来排版图书,商品目录以及宣传册 - 这些内容在以前可能从来没有被设计成网页。</p>
<p>在这篇文章里,我们将探索一些已有的CSS模块,它们不是在网页浏览器中使用,而是为了解决打印和分页的问题。我将解释选择器,属性和值的作用。最后我会提供一个实际例子,你可以把它作为自己试验的基础。对于这个例子,我们需要支持这些特殊CSS。我正在用的是Prince,它是一个商业软件。然而,Prince有针对非商业使用的免费版本,是实验这些例子的好工具。</p>
<h3>为什么HTML和CSS对印刷有意义</h3>
<p>非要用HTML去维护和用CSS去排版那些不是特意为网页设计的内容似乎有点奇怪。但是当你意识到流行的电子阅读格式例如EPUB和MOBI在底层是由HTML和CSS组成时就没那么奇怪了。另外,即便手稿或目录没有被整个发布到网页上,一些类似的东西可能会。HTML成为了一个方便的格式化标准,比起把所有东西都放在一个Word文档或者传统的桌面出版包中更容易处理。</p>
<h3>网页CSS和打印CSS的区别</h3>
<p>最大的区别和观念的转变在于印刷文档需要一个固定大小的页面模型。在网页上我们不断被提醒我们不知道视窗尺寸,在印刷中每个固定尺寸的页面承载了我们所有的东西。因为页面尺寸固定,我们需要把文档考虑成网页的集合,即分页媒体,而不是连续媒体即网页。</p>
<p>分页媒体的内容在网页上毫无意义。例如,你需要能生成页码,给章节标题添加边距,适当划分内容并保证图形和说明不脱钩。也许你要创建交叉参考和脚注,索引和文档目录。你可以把文档引入桌面发布包并且手动创建这些内容,然而,当你下次更新复本时需要重复这些工作。这时候CSS就起到作用,它的规则是为创建分页媒体而设计。</p>
<p>因为规则是为分页媒体而设计,在这篇文章里我们不考虑浏览器兼容性 - 这没多大意义。稍后,我们会看一个被设计成使用特定规则把HTML和CSS转换成PDF的用户代理。</p>
<h3>规则</h3>
<p>你已知的大多数CSS在印刷品排版中有效。在印刷品中比较特别的是,我们有&ldquo;<a href="http://www.w3.org/TR/css3-page/" rel="nofollow" target="_blank">CSS分页媒体模块</a>&rdquo;和&ldquo;<a href="http://www.w3.org/TR/css-gcpm-3/" rel="nofollow" target="_blank">CSS分页媒体模块生成内容</a>&rdquo;规则。让我们看一下这些规则如何生效。</p>
<h3>@PAGE 规则</h3>
<p><code>@page</code>规则允许你指定页面盒子的许多方面。例如,你想要指定页面尺寸。下面这条规则指定默认页面尺寸是<code>5.5*8.5</code>英尺。如果你想打印一本书,也许通过按需打印,找到可用尺寸很重要。</p>
<div class="jb51code">
<pre class="brush:css;">
@page {
size: 5.5in 8.5in;
}</pre>
</div>
<p>除了可以用长度值声明尺寸,你还可以使用纸质尺寸关键字,例如&quot;A4&quot;或&ldquo;&ldquo;legal&rdquo;。</p>
<div class="jb51code">
<pre class="brush:css;">
@page {
size: A4;
}</pre>
</div>
<p>你也可以使用关键字来指定页面方向 - &ldquo;&ldquo;portrait&rdquo;&rdquo;或&ldquo;landscape&rdquo;。</p>
<div class="jb51code">
<pre class="brush:css;">
@page {
size: A4 landscape;
}</pre>
</div>
<h3>理解页面模块</h3>
<p>进一步讨论前,我们需要理解分页媒体运行的页面模块,因为它和运行在屏幕上的内容表现上有一些不同。</p>
<p>页面模块定义了一块页面区域和16个环绕的<a href="http://www.w3.org/TR/css3-page/#margin-boxes" rel="nofollow" target="_blank">边距盒</a>。你可以控制页面区域的尺寸以及页面区域边界与页面自身结尾之间边距的尺寸。规范中的表格很好得解释了如何定义这些盒子尺寸。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151000432.jpg" /></p>
<p>页面区域是页面上一块页面内容流动的空间。当超出了它的容纳范围,就会创建另一个页面。边界盒只在CSS生成的内容上使用。</p>
<h3>页面左边距和右边距</h3>
<p>页面模块的另一方面是它定义了伪类选择器来选择文档的偶数或奇数页。如果你看一下手边的任何印刷书,你会发现边距尺寸和边距内容在左和右边边距上是不一样的。</p>
<p>我们可以用这些选择器给页面定义不同的边距尺寸。</p>
<div class="jb51code">
<pre class="brush:css;">
@page :left {
margin-left: 3cm;
}
@page :right {
margin-left: 4cm;
}</pre>
</div>
<p>规则还定义了两个伪类选择器。<code>:first</code>选择器选中是文档的第一页。</p>
<div class="jb51code">
<pre class="brush:css;">
@page :first {

}
</pre>
</div>
<p><code>:blank</code>伪类选择器选中任何&ldquo;故意左侧留白&rdquo;的页面。要添加这样的文字,我们可以使用目标是边距盒顶部中心的生成内容。</p>
<div class="jb51code">
<pre class="brush:css;">
@page :blank {
@top-center { content: &quot;This page is intentionally left blank.&quot; }
}</pre>
</div>
<h3>生成内容和页面媒体</h3>
<p>在上个例子中,我们使用CSS生成内容在边距盒的顶部中心添加文字。你会发现,生成内容在创建书的时候至关重要。这是把东西添加到边距盒上的唯一方式。例如,如果我们想把书名添加到奇数页边距盒的左下角,我们会用生成内容来实现。</p>
<div class="jb51code">
<pre class="brush:css;">
@page:right{
    @bottom-left {
      margin: 10pt 0 30pt 0;
      border-top: .25pt solid #666;
      content: &quot;My book&quot;;
      font-size: 9pt;
      color: #333;
    }
}</pre>
</div>
<h3>分页符</h3>
<p>另外&ldquo;页面媒体&rdquo;部分特殊之处在于如何控制分页。像描述的那样,一旦内容充满页面区域,它会移到新页面上。如果标题正好才写到页面上,可能页面会以标题结束,相关内容在下一页开始。在印刷书中,你会试图避免这种情况。其他你想要避免断开的地方还有表格中间以及图形和对应的说明间。</p>
<p>通常用<code>h1</code>标题来开始一本书的新章节。要强制标题总是处于页面开头,把<code>page-break-before</code>设置为<code>always</code>。</p>
<div class="jb51code">
<pre class="brush:css;">
h1 {
page-break-before: always;
}</pre>
</div>
<p>为了避免标题后立即断页,使用<code>page-break-after</code>。</p>
<div class="jb51code">
<pre class="brush:css;">
h1, h2, h3, h4, h5 {
page-break-after: avoid;
}</pre>
</div>
<p>为了避免断开图像和表格,使用<code>page-break-inside</code>属性</p>
<div class="jb51code">
<pre class="brush:css;">
table, figure {
page-break-inside: avoid;
}</pre>
</div>
<h3>计数器</h3>
<p>书总是跟数字相关-页,章节,甚至图像。我们可以通过CSS添加这些数字,避免由于我们打算在章节中添加一个新的图像而把所有东西重新编号。我们使用<a href="https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters" rel="nofollow" target="_blank">CSS计数器</a>来实现。</p>
<p>首先开始的地方显然是页码。CSS提供了预定义页面计数器;它从1开始并且每新的一页加1。在你的样式表中,你会使用这些计数作为生成内容的值,把页数放到边距盒的其中之一。在下面的例子中,我们把页码放在奇数页的右下角和偶数页的左下角。</p>
<div class="jb51code">
<pre class="brush:css;">
@page:right{
@bottom-right {
    content: counter(page);
}
}
@page:left{
@bottom-left {
    content: counter(page);
}
}</pre>
</div>
<p>我们还创建了一个叫做<code>pages</code>的计数器。这个计数器的值总是文档总页数。如果你想输出&ldquo;120页中的第3页&rdquo;,你可以像下面那样做。</p>
<div class="jb51code">
<pre class="brush:css;">
@page:left{
@bottom-left {
    content: &quot;Page &quot; counter(page) &quot; of &quot; counter(pages);
}
}</pre>
</div>
<p>你可以创建自己命名的计数器和增量并且按需要重置它们。要创建一个计数器,使用<code>counter-reset</code>属性,通过<code>counter-increment</code>增加。下面的CSS规则会为章节创建一个叫<code>chapternum</code>的计数器并且每出现<code>h1</code>增加-在这本书中作为每个章节的开始。随后我们使用计数器的值在生成内容中章节的实际标题前添加章数和时间。</p>
<div class="jb51code">
<pre class="brush:css;">
body {
counter-reset: chapternum;
}
h1.chapter:before {
counter-increment: chapternum;
content: counter(chapternum) &quot;. &quot;;
}</pre>
</div>
<p>书中图像的计数我们也可以用同样方法。通常给图像计数的方法是使用<code>chapternum.figurenum</code>。所以&ldquo;Figure 3-2&rdquo;代表第三章第二幅图。在<code>h1</code>里,我们可以重置<code>figurenum</code>让它每章都从1开始。</p>
<div class="jb51code">
<pre class="brush:css;">
body {
counter-reset: chapternum figurenum;
}
h1 {
counter-reset: figurenum;
}
h1.title:before {
counter-increment: chapternum;
content: counter(chapternum) &quot;. &quot;;
}
figcaption:before {
counter-increment: figurenum;
content: counter(chapternum) &quot;-&quot; counter(figurenum) &quot;. &quot;;
}</pre>
</div>
<h3>设置字符</h3>
<p>让我们再看一下印刷图书。当你翻阅一章,你会看到章节标题印在偶数或奇数页上。也许听起来很奇怪,&ldquo;页面媒体生成内容&rdquo;规范让我们通过CSS来实现。</p>
<p>我们在想要获取内容的选择器中使用<code>string-set</code>属性来实现,这将成为<code>h1</code>。<code>string-set</code>的值是你想给这段内容取得名字然后使用<code>content()</code>。随后你可以用<code>string()</code>作为生成内容输出。</p>
<div class="jb51code">
<pre class="brush:css;">
h1 {
string-set: doctitle content();
}
@page :right {
@top-right {
    content: string(doctitle);
    margin: 30pt 0 10pt 0;
    font-size: 8pt;
}
}</pre>
</div>
<p>当你的页面媒体生成后,每当出现<code>h1</code>,内容被写入<code>doctitle</code>然后输出到右侧页的右上边距盒中,只有当另一个<code>h1</code>出现才发生改变。</p>
<h3>脚注</h3>
<p>脚注是&ldquo;<a href="http://www.w3.org/TR/css-gcpm-3/#footnotes" rel="nofollow" target="_blank">CSS页面媒体模型生成内容</a>&rdquo;规范的一部分。脚注原理是在脚注行内添加文本,包裹在HTML标签中(很可能是<code>span</code>),用一个类定义它为脚注。当页面生成时,这个&ldquo;脚注元素&rdquo;的内容会被移除然后转换成脚注。</p>
<p>在CSS文件中,使用脚注的<code>float</code>属性值来给脚注类创建规则。</p>
<div class="jb51code">
<pre class="brush:css;">
.fn {
float: footnote;
}</pre>
</div>
<p>在你的文档中,使用这个类来包裹任何脚注文本。</p>
<div class="jb51code">
<pre class="brush:css;">
&lt;p&gt;Footnotes&lt;span class=&quot;footnotes&quot;&gt;Footnotes and notes placed in the footer of a document to reference the text. The footnote will be removed from the flow when the page is created.&lt;/span&gt; are useful in books and printed documents.&lt;/p&gt;著作权归作者所有。</pre>
</div>
<p>脚注有一个预定义计数器表现和页面计数器一样。通常,你想让计数器每次<code>fn</code>类出现时增加1并且在每一章开始时被重置。</p>
<div class="jb51code">
<pre class="brush:css;">
.fn {
counter-increment: footnote;
}
h1 {
counter-reset: footnote;
}</pre>
</div>
<p>脚注的不同部分可以用CSS伪元素来选中。<code>footnote-call</code>是文本中指示脚注的数值锚。它使用脚注计数器的值作为生成的内容。</p>
<div class="jb51code">
<pre class="brush:css;">
.fn::footnote-call {
content: counter(footnote);
font-size: 9pt;
vertical-align: super;
line-height: none;
}</pre>
</div>
<p><code>footnote-marker</code>是放置在文档底部脚注前的一个数值标记。原理和CSS中生成有序列表的数值类似。</p>
<div class="jb51code">
<pre class="brush:css;">
.fn::footnote-marker {
font-weight: bold;
}</pre>
</div>
<p>脚注处于边界内,在页面中一块特殊区域叫做<code>@footnotes</code>。你可以用下面的方式选中并给这块区域赋予样式。</p>
<div class="jb51code">
<pre class="brush:css;">
page {
@footnotes {
    border-top: 1pt solid black;
}
}</pre>
</div>
<h3>交叉引用</h3>
<p>在我们继续谈论把我们所学运用其中的例子之前,让我们看一下交叉引用。在网页中,我们通过添加链接交叉引用。在书和其他打印文档中,通常依靠页码寻找引用。因为页码也许会随着书的不同版本打印形式的变化而改变 - 通过CSS实现让我们不需要检查和改变所有数字。</p>
<p>我们用另一个新属性,<code>target-counter</code>,添加这些数字。在文档中创建链接时,赋予它们<code>href</code>,值为你想要标记的文档中的元素的ID。此外,增加一个类来识别它们是交叉引用,而不是一个外部链接;我用<code>xref</code>。</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;a class=&quot;xref&quot; href=&quot;#ch1&quot; title=&quot;Chapter 1&quot;&gt;Chapter 1&lt;/a&gt;著作权归作者所有。</pre>
</div>
<p>然后,在这个链接后,再一次使用生成内容输出<code>(page x)</code>,<code>x</code>是ID在书中位置的数值。</p>
<div class="jb51code">
<pre class="brush:css;">
a.xref:after {
content: &quot; (page &quot; target-counter(attr(href, url), page) &quot;)&quot;;
}</pre>
</div>
<p>当我们为实际例子创建目录时会在实践中查看这个技术。</p>
<h3>将他们放在一起:一本样书</h3>
<p>我们分别看了很多不同的属性。一旦你通过创建一本书把它们放到一起时它们会更有意义。</p>
<p>真正用CSS来创建一本书,你需要一个支持的用户代理。现在,很少有东西很好得实现这些规范;目前最好用的是<a href="http://princexml.com/" rel="nofollow" target="_blank">Prince</a>。Prince的独立商业许可很贵,但是你可以在非商业项目中免费使用Prince。这意味着如果你只是想尝试这些技术,你可以用。另外,如果你确实有使用这项技术的非商业需求,你也许可以用Prince来排版这些书。</p>
<p>腾堡项目里找到我最喜欢的书,<a href="http://www.gutenberg.org/ebooks/35450" rel="nofollow" target="_blank">Harrison Weri的Our Cats</a>中提取了一些段落,我选这本书因为我喜欢猫并且它有图片和脚注可以用来验证排版。</p>
<p>你可以在<a href="https://github.com/rachelandrew/css-for-print" rel="nofollow" target="_blank">GitHub上</a>找到我用的文件,加上一份生成的PDF。如果你想用CSS实验并且自己编译这本书,你需要下载和安装<a href="http://princexml.com/" rel="nofollow" target="_blank">Prince</a>。Prince是Mac下的命令行工具,虽然也有Windows GUI,我使用命令行因为它真的很简单。</p>
<p>使用命令窗口,切换到你书的目录或者你从GitHub上下载文件的路径。</p>
<div class="jb51code">
<pre class="brush:ps;">
cd /Users/username/smashing-css-books</pre>
</div>
<p>现在,运行Prince:</p>
<div class="jb51code">
<pre class="brush:ps;">
prince -s pdf-styles.css book.html builds/book.pdf</pre>
</div>
<p>这会在<code>builds</code>目录下创建一个叫<code>book.pdf</code>的PDF文件。现在,如果你对CSS或HTML做了任何更改,你可以运行Prince来看一下发生了什么变化。</p>
<h3>HTML文档</h3>
<p>我整本&ldquo;书&rdquo;是在一个HTML文档中编译的。在Prince中编译文档是可能的,但我发现只有在处理一个比较大的文档时才比较方便。在每个由<code>h1</code>开始的章节前,我都放了一个div包含了封面图,然后是书的目录。</p>
<p>目录链接到每个章节<code>h1</code>标题的ID。</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;!DOCTYPE html&gt;
&lt;html dir=&quot;ltr&quot; lang=&quot;en-US&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot; /&gt;
&lt;title&gt;Our Cats and All About Them&lt;/title&gt;
&lt;meta name=&quot;author&quot; content=&quot;Harrison Weir&quot;/&gt;
&lt;meta name=&quot;subject&quot; content=&quot;Cats. Their Varieties, Habits and Management; and for show, the Standard of Excellence and Beauty&quot;/&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;cats,feline&quot;/&gt;
&lt;meta name=&quot;date&quot; content=&quot;2014-12-05&quot;/&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class=&quot;frontcover&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;contents&quot;&gt;
      &lt;h1&gt;Extracts from Our Cats and All About Them by Harrison Weir&lt;/h1&gt;
      &lt;ul class=&quot;toc&quot;&gt;
          &lt;li&gt;&lt;a href=&quot;#ch1&quot;&gt;The First Cat Show&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ch2&quot;&gt;Trained Cats&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ch3&quot;&gt;General Management&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ch4&quot;&gt;Superstition and Witchcraft&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;h1 id=&quot;ch1&quot; class=&quot;chapter&quot;&gt;The First Cat Show&lt;/h1&gt;
      &lt;p&gt;&hellip; &lt;/p&gt;
    &lt;h1 id=&quot;ch2&quot; class=&quot;chapter&quot;&gt;Trained Cats&lt;/h1&gt;
      &lt;p&gt;&hellip; &lt;/p&gt;
    &lt;h1 id=&quot;ch3&quot; class=&quot;chapter&quot;&gt;General Management&lt;/h1&gt;
      &lt;p&gt;&hellip; &lt;/p&gt;
    &lt;h1 id=&quot;ch4&quot; class=&quot;chapter&quot;&gt;Superstition and Witchcraft&lt;/h1&gt;
      &lt;p&gt;&hellip; &lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<p>接下来的CSS中使用了我们目前为止讨论的所有东西。一开始,我们需要用<code>@page</code>规则设定书的尺寸。然后使用<code>:first</code>伪类选择器来移除第1页的边距,因为这一页会有封面图。</p>
<div class="jb51code">
<pre class="brush:css;">
@page {
size: 5.5in 8.5in;
margin: 70pt 60pt 70pt;
}
@page:first {
size: 5.5in 8.5in;
margin: 0;
}</pre>
</div>
<p>然后我们处理封面的图片,确保它覆盖了整个页面区域。</p>
<div class="jb51code">
<pre class="brush:css;">
div.frontcover {
page: cover;
content: url(&quot;images/cover.png&quot;);
width: 100%;
height: 100%;
}</pre>
</div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151439223.jpg" /></p>
<p>下一步,我们处理偶数和奇数页的细节,使用<code>:right</code>和<code>:left</code>传递伪类选择器。</p>
<p>奇数传递会让书的标题处于边框盒左下方,页数在右下方,章节标题在右上方。章节标题随后在样式表中用<code>string-set</code>设置。</p>
<div class="jb51code">
<pre class="brush:css;">
@page:right{
@bottom-left {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #666;
    content: &quot;Our Cats&quot;;
    font-size: 9pt;
    color: #333;
}
@bottom-right {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #666;
    content: counter(page);
    font-size: 9pt;
}
@top-right {
    content:string(doctitle);
    margin: 30pt 0 10pt 0;
    font-size: 9pt;
    color: #333;
}
}</pre>
</div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151510750.jpg" /></p>
<p>偶数传递让书的标题处于右下方以及页数处于左下方。</p>
<div class="jb51code">
<pre class="brush:css;">
@page:left {
@bottom-right {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #666;
    content: &quot;Our Cats&quot;;
    font-size: 9pt;
    color: #333;
}
@bottom-left {
    margin: 10pt 0 30pt 0;
    border-top: .25pt solid #666;
    content: counter(page);
    font-size: 9pt;
}
}</pre>
</div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151537281.jpg" /></p>
<p>对于第一页,包含了封面图,我们通过设置为<code>normal</code>来确保没有生成内容。</p>
<div class="jb51code">
<pre class="brush:css;">
@page:first {
@bottom-right {
    content: normal;
    margin: 0;
}
@bottom-left {
    content: normal;
    margin: 0;
}
}</pre>
</div>
<p>样式表接下来的一部分处理计数器。除了预设页面计数器外,我们给章节和图解定义计数器。</p>
<div class="jb51code">
<pre class="brush:css;">
/* Reset chapter and figure counters on the body */
body {
counter-reset: chapternum figurenum;
font-family: &quot;Trebuchet MS&quot;, &quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, &quot;Lucida Sans&quot;, Tahoma, sans-serif;
line-height: 1.5;
font-size: 11pt;
}
/* Get the title of the current chapter, which will be the content of the h1.
Reset figure counter because figures start from 1 in each chapter. */
h1 {
string-set: doctitle content();
page-break-before: always;
counter-reset: figurenum;
counter-reset: footnote;
line-height: 1.3;
}
/* Increment chapter counter */
h1.chapter:before {
counter-increment: chapternum;
content: counter(chapternum) &quot;. &quot;;
}
/* Increment and display figure counter */
figcaption:before {
counter-increment: figurenum;
content: counter(chapternum) &quot;-&quot; counter(figurenum) &quot;. &quot;;
}</pre>
</div>
<p>现在在标题后章节有了自己的数字。图解也显示出它们的数字。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151610217.jpg" /></p>
<p>我们像之前解释的那样创建脚注,给脚注引用添加上标。</p>
<div class="jb51code">
<pre class="brush:cpp;">
.fn {
float: footnote;
}
.fn {
counter-increment: footnote;
}
.fn::footnote-call {
content: counter(footnote);
font-size: 9pt;
vertical-align: super;
line-height: none;
}
.fn::footnote-marker {
font-weight: bold;
}
@page {
@footnotes {
    border-top: 0.6pt solid black;
    padding-top: 8pt;
}
}</pre>
</div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151637279.jpg" /></p>
<p>我们添加一些规则来控制页面的中断。你需要非常小心避免过度。如果你的书有许多表格和图像,添加许多细节规则可能导致书中有许多缺口。实验和测试会表明你可以怎样用程序控制中断。我总结了下面规则作为一个良好的开端。</p>
<p>记住这只是用户代理的建议。在有些情况,如果表格不适应页面要保持表格不中断是不可能的!</p>
<div class="jb51code">
<pre class="brush:css;">
h1, h2, h3, h4, h5 {
font-weight: bold;
page-break-after: avoid;
page-break-inside:avoid;
}
h1+p, h2+p, h3+p {
page-break-before: avoid;
}
table, figure {
page-break-inside: avoid;
}</pre>
</div>
<p>最后,我们给目录赋予样式,这里我们使用有趣的窍门。当描述交叉引用时,我解释了如何使用<code>target-counter</code>来显示带ID的页码。这也是我们要对我们的目录做的。下面的规则在目录的每一章的链接后放置页码。</p>
<div class="jb51code">
<pre class="brush:css;">
ul.toc a::after {
content: target-counter(attr(href), page);
}</pre>
</div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151708495.jpg" /></p>
<p>然而,通常在书中,你会使用前导字符点(<code>.</code>)来组织所有的页码的右边距。让人惊讶的是,CSS提供了我们一个方式来实现这个,通过在生成内容中数字前添加<code>leader()</code>。</p>
<div class="jb51code">
<pre class="brush:css;">
ul.toc a::after {
content: leader('.') target-counter(attr(href), page);
}</pre>
</div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202305/2023525151811281.jpg" /></p>
<p>现在我们有了一个完整的样式表来构建我们的书。我避免花费很多时间在排版上,集中精力在创作书的细节上。然而,从这个角度看,你可以实验并且添加你自己的样式来创建独特的书籍设计。</p>
<h3>不仅仅是书!</h3>
<p>记住这些技术不仅仅是为了书。你可以用它们从已经给顾客开发的网站的HTML中生成打印和PDF版本产品目录。或者你可以从网页内容中创建传单和小册子。</p>
<p>如果你想用Prince从网页中创建PDF文档,<a href="https://docraptor.com/" rel="nofollow" target="_blank">DocRaptor</a>是一个好选择。这项服务通过API调用Prince。你可以通过API发送文档并且接收PDF-允许用户转换成PDF下载内容。所有我们在这篇文章中讨论的东西通过DocRaptor整合的API已经都可以实现。</p>
<p>即使你现在没有生成PDF的需求,这仍然是CSS中一个迷人的领域 - 这是一个隐秘的实用技能,当需求出现时你知道这是可以实现的。</p>
<h3>资源和扩展阅读</h3>
<ul>
    <li><a href="http://www.w3.org/TR/css3-page/" rel="nofollow" target="_blank">CSS Paged Media Module Level 3</a>, W3C</li>
    <li><a href="http://www.w3.org/TR/css-gcpm-3/" rel="nofollow" target="_blank">CSS Generated Content for Paged Media Module</a>, W3C</li>
    <li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters" rel="nofollow" target="_blank">Using CSS Counters</a>, Mozilla Developer Network</li>
    <li><a href="https://books.spec.whatwg.org/" rel="nofollow" target="_blank">CSS Books: Living Standard</a>, WHATWG</li>
    <li><a href="http://www.princexml.com/doc/" rel="nofollow" target="_blank">User Guide for Prince 9.0</a>&nbsp;A lot of simple examples that work in Prince</li>
    <li><a href="http://24ways.org/2013/how-to-write-a-book/" rel="nofollow" target="_blank">How to Write a Book</a>,Jonathan Snook, 24 Ways</li>
    <li><a href="http://alistapart.com/article/building-books-with-css3" rel="nofollow" target="_blank">Building Books With CSS3</a>,Nellie McKesson, A List Apart</li>
    <li><a href="http://alistapart.com/article/boom" rel="nofollow" target="_blank">Printing a Book With CSS: Boom!</a>,Bert Bos and H&aring;kon Wium Lie, A List Apart</li>
    <li><a href="http://rachelandrew.co.uk/archives/2014/01/07/html-epub-mobi-pdf-wtf-creating-an-ebook/" rel="nofollow" target="_blank">HTML, EPUB, MOBI, PDF, WTF: Creating an Ebook</a>, Rachel Andrew</li>
</ul>
<h3>总结</h3>
頁: [1]
查看完整版本: css打印样式设计举例详解