惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
S
SegmentFault 最新的问题
量子位
有赞技术团队
有赞技术团队
V
V2EX
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
C
Check Point Blog
G
Google Developers Blog
博客园 - 叶小钗
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
T
Tailwind CSS Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
U
Unit 42

Dallas Lu

一些没有意义的事情 博客程序的一次大重构 OpenWRT 使用 udp2raw 对抗 WireGuard 阻断 如何证明你是原创作者 Nginx 泛域名配置的隐患与对策 WISeID S/MIME 证书 V2EX 刑满释放记 使用 Radicale 在 Ubuntu 24.04 中搭建 vCards CardDav 服务 邮件服务的域名成功从 SURBL 黑名单移除 The Domain of Mail successfully removed from SURBL blacklist 网站多语言的设计细节 Website Multilingual Design Details 网站评论系统的目前进展和展望 Current Progress and Outlook of the Website Comment System 在公网使用 iptables 转发端口时保留客户端 IP Retaining Client IP with iptables Port Forwarding on Public Networks 邮件投递平台 Postal 的使用经验 Experience with Using Postal, the Mail Delivery Platform 自建 Postal 完美替代 SendGrid Build Your Own SendGrid: Postal SMTP Server 互联网在崩塌吗,然后呢 Is the Internet collapsing and then what 在 SvelteKit 应用中使用 JSON-LD Using JSON-LD in SvelteKit Applications 网页的打印样式应该怎么写 “茴字的四种写法”之 IP 与域名 Trivia: Special ways of writing IP and domain names 怎么伪造 Git 提交的时区 How to fake the timezone of a git commit 供大众交流的论坛和其它替代产品还是不好用
How to write print styles for web pages
Dallas Lu · 2024-04-26 · via Dallas Lu

Just started the development of this site’s program, I have considered the issue of page printing. Markdown to PDF has a variety of options, and the web page to PDF experience is very poor, a lot of unimportant content affects the printing results. Whether you print to paper or to PDF, the layout of the page itself is fully capable.

Then I read the article “How to Create a Print-Friendly Web Page” on Baoshuo’s blog, and he listed some very important suggestions and methods with examples. This article tries to summarize and add to them.

The overall structure of the style sheet should be planned in order to facilitate the setting of print styles:

/** Base */
:root{
    /* vars */
}
/* and other base styles */

/** Normal */
@media screen {
    /*  */
}

/** print */
@media print {

}

The advantage of putting definitions other than the base style in @media screen is that you don’t have to do the extra reset and write !important in @media print. If you’re using sass, it’s even easier to write:

article{
    --article-bg-color: #fafafa;
    background-color: var(--article-bg-color);
    font-size: 16px;

    @media screen{
        border: 1px solid #aaa;
    }

    @media print {
        --article-bg-color: #fff;
        print-color-adjust: exact; /* force bg color if need */
        font-size: 12pt;
    }
}

Hide non-text content

For example, headers and footers, sidebars, action buttons, etc. in the page layout. You can specify rules to hide them or add a tool class to them:

.print-only{
    display: none;
}
@media print{
    .no-print{
        display: none;
    }
    .print-only{
        display: block;
    }
}

Hyperlinks generally have a default color, but in black and white printing, the color is not obvious, so it is best to give an underline style. And the actual link should be shown directly:

@media print {
  a:not([href^='#'])::after {
    content: ' (' attr(href) ')';
    font-size: 80%;
  }
}

Similarly, abbr can be specified to display its title attribute.

@media print{
    abbr[title]:after {
    content: ' (' attr(title) ')';
    }
}

Other media content

Video and audio, for example, can be added separately with footnote information and a QR code. Use .print-only and .no-print rules to flexibly control what is printed.

<video class="no-print"></video>
<img class="print-only"/>

It is also possible to extend the style of hyperlinks as:

@media print{
    a[data-print-content]::after {
        content: " (" attr(data-print-content) ")";
        font-size: 80%;
        color: #666;
    }
    a:not([href^="#"]):not([data-print-content-none]):not([data-print-content])::after {
        content: "(" attr(href) ")";
        font-size: 80%;
        color: #666;
    }
}
<video data-print-content=""><video>
<a href="https://google.com" data-print-content-none>https://google.com<a>

Page

Page breaks can also be inserted when printing web pages, just like text layout software, to avoid strange page breaks that may affect the reading experience. Generally speaking, it is better not to split the outline title with its following content in two pages, and some special elements such as code blocks are better not to be split in two pages. We are also allowed to manually insert elements at appropriate page breaks in the page.

@media print {
    h2,
    h3,
    h4,
    h5,
    h6 {
        page-break-after: avoid;
    }

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

    pre,
    blockquote {
        page-break-inside: avoid;
        box-decoration-break: clone;
    }

    p {
        widows: 4;
        orphans: 3;
    }
}

When writing an article, we can add a pagination element to the page:

<div class="page-break" />

Conclusion

You can perform a print preview to see how this page will print. Or you can use the developer tools and toggle the activation of @media print. There is a difference, most of the browser print previews have some mandatory internal rules, such as ignoring the background color and so on. Maybe, we don’t need Markdown to PDF tool.