Print Style Sheets: The Basics (for No Excuses)
Published on February 21, 2007 (❠February 5, 2024), filed under Development (RSS feed for all categories).
This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.
There are no excuses for not having at least a simple print style sheet. If youâre already on the web standards track, things are simple. (Thereâs no need to force users to disable style sheets, open print previews, select pages they want to print, and finally print.)
Letâs go. The worldâs most basic print style sheet is based on this simple rule:
@media screen {
}
âŚassuming that you donât mix structure and presentation, use only a single style sheet (big advantage, again), and donât tie media types to the HTML style sheet reference. In this case, when printing, the result will be an unstyled page where user agent style sheets almost do the job.
The next step is ârealâ print optimization. For example, you may want to make sure that everythingâs black on white, and you probably need another font (since several system fonts donât work that great on paper). VoilĂ :
@media print {
* {
background: #fff;
color: #000;
}
html {
font: 100%/1.5 georgia, serif;
}
}
(Derived from the meiert.com style sheet and based on my own CSS coding guidelines [about which I later wrote a book].)
Thatâs neat. You add the above snippet to your (via @media
) screen-targeted style sheet, or, if you canât avoid it, you remove the @media print {
and }
parts and use the remaining rules to create an extra print style sheet.
A note regarding additional print style sheets: I didnât carefully analyze this but I suspect user agents to download print style sheets on document load, not only in case of printing (which makes some sense). This means that even larger print style sheets might be located in the corresponding âmainâ style sheet without increasing load time. Quite the contrary: Using only a single style sheet saves one or more additional server requests, so this procedure will bring better performance. With small print styles (like the above), it should be clear that using one style sheet is useful.
Letâs finish our excursion on print CSS: In print, users do not benefit from navigation menus or footer sections. Letâs remove them in the print version:
@media print {
#nav,
#about {
display: none;
}
}
(You probably need to adjust the above selectors to fit your needs.)
All this stuff combined will create one of the most elegant solutions for your usersâ âI canât print this document and I hate you because all you had to do was exclude print media from your style sheetsâ problem.
@media screen {
/* The regular stuff. */
}
@media print {
* {
background: #fff;
color: #000;
}
html {
font: 100%/1.5 georgia, serif;
}
#nav,
#about {
display: none;
}
}
What a pep talk. (If now my favorite bloggers, Mark Cuban and Guy Kawasaki, felt encouraged to improve the print styling of their sites!)
About Me
Iâm Jens (long: Jens Oliver Meiert), and Iâm a frontend engineering leader and tech author/publisher. Iâve worked as a technical lead for companies like Google and as an engineering manager for companies like Miro, Iâm somewhat close to W3C and WHATWG, and I write and review books for OâReilly and Frontend Dogma.
I love trying things, not only in web development (and engineering management), but also in other areas like philosophy. Here on meiert.com I share some of my views and experiences.
If youâd like to do me a favor, interpret charitably (I speak three languages, and they do collide), yet be critical and give feedback, so that I can make improvements. Thank you!
Comments (Closed)
-
On February 22, 2007, 11:13 CET, Jens Grochtdreis said:
Well, âdead simpleâ and print styles do only fit on the first sight. As long as the Mozilla-programmers donât want to correct a big fault inh their program which they know of for at least five years, it can be very horrible to write a print stylesheet. All Mozillas fail when encountering a float.
Speaking of failing: there is not one browser that interpretes all the good and nice specifications for print styles. So, writing a print styles can be a big headache except for simple layouts as from blogs as yours or mine.
-
On February 24, 2007, 9:54 CET, Steve said:
You may want to test this for accessibility with screen readers. In the past Jaws had difficulty with the media selector when screen and print were in the same file. It would apply all of the rules despite the media selector and render some of the content(i.e. nav as display none making it impossible for a Jaws user to access or even know it was there. placing the print styles in a seperate file and using the link tag to include it resolves the issue.
-
On February 28, 2007, 23:17 CET, Jesse said:
Thanks much for this! I havenât explored this at all and your post gave me a great start.
-
On March 1, 2007, 13:26 CET, Jens Oliver Meiert said:
Jens, it definitely must not be difficult even for more complex sites. It may start with exactly what I present here.
Steve, thanks for pointing that outâaccording to feedback by Gez Lemon, it seems that the mentioned problem has been fixed since JAWS 7.1. However, I also remember that using
@media print
rules may actually influence the appearance of documents in IE 5, since their implementation is quite buggy in this regard. Thankfully, this IE version is almost dead. -
On April 14, 2007, 18:26 CEST, Peter said:
What is Media:projection for? Opera is the only browser that supports this media type.
-
On April 14, 2007, 20:48 CEST, Jens Oliver Meiert said:
âŚsince
projection
makes sense, and for Opera, right. đ -
On June 25, 2007, 9:34 CEST, Siegfried said:
Absolutely necessary, indeed. Although i prefere to add the media descriptor to the HTML like:
<link rel=âstylesheetâ type=âtext/cssâ media=âprintâ href=âstyle/Print.cssâ>This prevents the loading of unneeded Stylesheets at all. It just means that you have several stylesheets. I have a system with basically 3 stylesheets: One basic for all media, defining the major geometry, one for screen, adding colours and pictures, and one for printer, not adding colors but correcting the geometry (disabling the nav bar and extending the main container into the now free nav bar area).
Avoiding âconditional commentsâ to include IE specific stylesheets may be a good idea, but there is no reason to avoid splitting your styles this way. Contrary it helps organizing the styles. -
On June 25, 2007, 10:31 CEST, Siegfried said:
BTW: Indeed projection media makes sense. This would enable Powerpoint style presentations. The fact that neither IE nor Mozilla support this media type does not mean this is a bad idea. There is another badly supported media type, which coul dbe of great use: Aural.
-
On June 25, 2007, 10:50 CEST, Jens Oliver Meiert said:
I usually advise to reference just a single media independent style sheet to move maintenance further away from HTML; you can choose style sheet ordering and media types from a default.css or whatever, too.
Nice note on
aural
(though you probably refer tospeech
?) which reminds of thereader
media type, too[âŚ]. -
On June 26, 2007, 16:35 CEST, Siegfried said:
Yes, speech. Although i think this is called âAuralâ in the w3c documents, but iâm not really sure.
And btw h little hint, how to learn using css the right way (proper class names and the like): I learned that by applying several socalled âalternateâ stylesheets to the page. During the learning phase alternate stylesheets which completely changed the pages geometry. Doing this enforces the use of meaningful semantic markup. If you donât do so, none of these alternate stylesheets makes any sense, and you will notice that. One day this lead to some practical organisation of all those stylesheets. This is the reason why i prefere my styles organized into several files. I do not change geometry any more, that simply irritates the user, but for learning this was great. -
On August 21, 2008, 18:43 CEST, Trupti Patil said:
I would like to control the auto page numbering offered by printer through CSS for my ASP reports.
-
On June 4, 2009, 18:06 CEST, Gordon said:
An even easier way.
Prepare a print.css. The first line should be @import âstyle.cssâ; (or whatever you have called it)
Next add the following lines
.NONO{display:none;} a {text-decoration:none;color:#000;}
Add the class NONO to each div that you DONâT want printed e.g nav bars, menus etc.
NOTE is is possible to assign more than one class at a time e.g. class = ânav NONOâ
Put a reference to the print.css in our pages and thatâs it.
When a page is previewed or printed the divs with class = NONO will not print, and all links will be in black with no underline.
-
On November 12, 2009, 15:39 CET, maxodo said:
Is it possible to use the @media print rule to print images defined by the css stylesheet? I need to print the background-images.
Thanks in advance.
Read More
Maybe of interest to you, too:
- Next: HTML: Semantics of âtitleâ Element Content
- Previous: Weird Weekend Without Happy End: Eggebek, Flensburg, Denmark, Bremen
- More under Development
- More from 2007
- Most popular posts
Looking for a way to comment? Comments have been disabled, unfortunately.
Get a good look at web development? Try WebGlossary.infoâand The Web Development Glossary 3K (2023). With explanations and definitions for thousands of terms of web development, web design, and related fields, building on Wikipedia as well as MDN Web Docs. Available at Apple Books, Kobo, Google Play Books, and Leanpub.