midwest web design

CSS-Print

Web Developers have been building web sites with HTML for some time now. In these days, if they were able to get their site looking great visually in a browser, generally they called it a day. Those days are fading; today’s web sites not only implement standards-compliant code for the site’s visual aspect, but the need for printer-friendliness is becoming a must. If you design your site with HTML and CSS, designing a separate print style sheet is relatively easy. In this article I’ll show you how to make your web site look great visually and produce great-looking print output as well.

See Demo

Printable Version
Word PDF
Download Project Files
Zip

Analysis

Before we dig in let’s examine some key issues to consider when dealing with printed material:

The Page

The best way to demonstrate how to make a printed style sheet is to use a design which is already created. We’ll examine how to convert this page to hard-copy format, and discuss a few catches that we need to account for on other pages before the finished print style sheet is complete. Take a look at the finished web version of the page:

See starting homepage

Let’s define some goals for the conversion process:

1) We need to remove the:

2) Since the logo will be removed, we need a way to logically and semantically represent the logo.

3) We need a way to represent the page title without all the color.

4) Since my font sizes are defined relatively, we need to change to points (pt). We’ll also change the font family where appropriate to match our web style sheets.

5) The copyright needs to be changed to enhance its significance.

It’s important to consider all the features included in the web layout, so examine carefully how the page is constructed.

In our case, the list above ought to cover what we need to do, so let’s get started.

Open the File

Open up your favorite text editor and open the file called homepage.html (from the zip file you downloaded from the top of the page) and have a look at the following mark-up:

<link  href="mfv4.css" rel="stylesheet"  type="text/css">
<style  type="text/css">
<!--
@import url(mfv5.css);
-->
</style>

Note that we have two style sheets defined: one for version 4 browsers (mfv4.css) and one for version 5 (mfv5.css). Note also that our style sheets lack media attributes. We need to define these attributes before we can create print style sheets, so let’s make the following modifications:

<link  href="mfv4.css" rel="stylesheet" type="text/css" media="aural">
<style  type="text/css" media="aural">
<!--
@import url(mfv5.css);
-->
</style>

You might be wondering why we chose aural for a media type instead of screen when we want these style sheets to be our web styles. Our reason is simple – we need to change the media value on these two style sheets to a value we know will not be recognized by any media type (web, print, etc.). This will ensure that any rules we write for our print style sheet will not affect our existing web rules. Make the changes, save your work, and use your browser to preview the page. It should look like the URL below:

See media attribute set to aural

It’s not too attractive, is it? You probably can guess how our web page would look in print – just like you see it in your browser, and we definitely do not want this. Time now to create a new style sheet called print.css and link it as below:

<link  href="mfv4.css" rel="stylesheet" type="text/css"  media="aural">
<link  href="print.css" rel="stylesheet" type="text/css"  media="screen">
<style  type="text/css" media="aural">
<!--
@import url(mfv5.css);
-->
</style>

Notice something very important in the above code. When we linked the print style sheet to our document we used a media value of screen. You might wonder why we're using screen when this is supposed to be our print style sheet. The reason is because we need a way to see the rules as they apply to the page when printed, and the best way to do this is to style the page in the browser first, with print styles in mind instead of printing this page multiple times, which would be wasted paper. When we are finished, we will change the media values on all style sheets to their appropriate media values.

Background image & Rounded Corners

Since we haven’t included the rules for our rounded corner images or for the background image, no modification is really needed in the print style sheet. However, just to ensure that no monkey business will happen when viewed in a browser, let's explicitly declare a background color of white and turn off any potential background image on the body element:

body{
background-color:!important  #fff;
background-image:!important  none;
}

The use of the keyword !important will ensure that the styles in the print file will override the declarations in both web versions. In other words, using !important will settle any conflict that might arise in any of our three style sheets.

Next, to ensure the same level ofconsistency we need to turn off background images that some of our cells carry. To do so we'll write a multiple selector rule to turn these off, as you see below:

#tlcorner,#tedge,#trcorner,#redge,#brcorner,#bedge,#blcorner,#ledge{
background-color:!important  #fff;
background-image:!important  none;
}

When you have multiple elements that will have the same values as shown above, CSS permits the use of listing multiple selectors in one rule. Not only is it more efficient, but saves on the final file size of the style sheet. Make the changes, save your work and use your browser to preview the page. It should look like the URL listed below:

CSS Multiple Selectors

Logo

Here our first order of business is to remove our logo at the top of the page. Since the logo is contained in a table data cell, we can apply a CSS class to remove the logo from our print style sheet, as you can see below:

/*remove logo*/
td.logo img{
 display:none;
}

This rule targets any image tag inside a table cell with a class of logo. By using the display property with a value set to none, we ensure that the logo and the space it occupied are removed from the print style sheet. We also need to make one modification to our HTML file, in the first row, second column as shown below:

<td class="logo">
<img src="../../images/logo.gif" alt="Midwest Web Design"  width="260" height="50">
</td>

By applying the class logo to the table cell that contains our image, we effectively remove it from our print style sheet, as shown here:

Logo Image Removed

Navigation Controls

This part is actually easier than it looks. Since the side bar has an ID in the HTML file, we can easily manipulate this ID in our print style sheet to hide it as shown below:

/*remove navigation*/
#sidebar{
 display:none;
}

Again, by using the display property with a value set to none we remove the navigation from our print style sheet and remove the space it occupied. Nothing is needed in the HTML file. Preview the results here:

Navigation removed

Difference between display and visibility

You might have noticed we used display instead of visibility. The difference between display and visibility is that display with a value set to none (display: none) will hide the element visually from displaying and will remove the physical space the element occupied. Using the visibility property with a value set to hidden (visibility: hidden) will hide the element visually from displaying; however, it will preserve the physical space the element occupied.

Note: If you view the above example in Dreamweaver’s Design View, the side bar will still be visible. It works fine when viewed in a browser and printed.

Recreating the logo

When we created our logo class and removed our graphic from the top (Midwest Web Design), we lost any way of displaying the title to our website in the print version. One way to create the title and probably the most logical is to create a heading two tag in HTML and then in our print style sheet add the following rule:

h2{
 font-family:Arial, Helvetica, sans-serif;
 font-size:30pt;
 color:#000;
}

This rule redefines the Heading Two tag in the print style sheet with a font family of Arial, Helvetica, or the nearest sans-serif font. Then, instead of a relative font size we use a 30-point fixed font size, which ensures the correct measure for print output. Then, since we don’t care about color, we use black. In the HTML file, locate the table cell with the logo class and insert a heading two as shown below:

<td class="logo">
<h2>midwest web design</h2>
<img  src="../../images/logo.gif" alt="Midwest Web Design"  width="260" height="50">
</td>

Preview and test your results using the link below:

Recreating the title to our website

You might wonder why we chose Heading Two instead of Heading One. This is because we used a Heading One tag in my HTML file as the page title for each web page, and we want to avoid the hassle of re-doing this tag in multiple files. As you’ll soon see, we counteract the Heading One font size in CSS for the print style sheet.

Recreating the Page Title

We want the title for each of our web pages to be smaller in size than the title of our website. We can fix this easily by defining a font size for Heading One in our print style sheet as shown below:

h1{
 font-family:Verdana, Arial, Helvetica, sans-serif;
 font-size:18pt;
 color:#000;
}

We just reduce the font size to 18 points to simulate the effect of a different heading tag. Also, we give it the same font family as our web style sheet, and set the default color to black. Preview the results using the page address below:

Recreating the page title

Paragraphs

We want all the paragraphs to look as much like the web styles as possible, so let’s add the following rule to our print style sheet:

p{
 font-family:Verdana, Arial, Helvetica, sans-serif;
 font-size:10pt;
 color:#000;
}

This sets all paragraphs to the same font family as our web styles – font size to 10 points and color to black. Preview the results here:

Style our paragraphs

Copyright

At this point, you have a nice printed version of the web site, but the copyright is lacking. Since we can’t use colors, let’s bring visual significance to the copyright through another means as shown below:

#footer p{
 font-family:Arial, Helvetica, sans-serif;
 font-size:8pt;
 font-style:italic;
 color:#000;
}

This rule assigns a font family of Arial, Helvetica or the nearest sans-serif font, a font size of 8 points, font style of italics, and a color of black to all paragraphs that reside inside an ID of footer. How did we come up with this rule? At the bottom of the HTML file you will notice my copyright text is inside a table cell with an ID of footer. Preview the results at this link:

Style the copyright text

Some Catches

Our print style sheet is looking great, but there are still some changes needed. First, the tutorials section has a table at the top that provides Word and PDF forms of the web article, plus a zip file for the project files. In the file mfv4.css in the zip package you downloaded you will see rules that look like this: table#project. In the web style sheet, the cells which contain the text, Word, PDF and Zip have a dark orange background color applied to them as shown by this rule:

table#project td.header{
 background-image:none;
 background-color:#c90;
 color:#14314f;
 font-weight:bold;
 text-align:center;
}

In the printed document, we should not assume the visitor will have color printing enabled or even want to print in color. To ensure the same level of consistency and uniformity as we have taken for the rest of the website, we will omit the background color from the print style sheet and change the color of the font to black as shown below:

table#project td.header{
 background-image:none;
 color:#000;
 font-weight:bold;
 text-align:center;
}

Removing background color from project table

Continuing, the tutorials section provides each code sample in a box with a dark background color as shown in the web style sheet:

p.code{
 background-color:#ccc;
 border:1px solid #000;
 padding:4px;
}

We should omit the background color from the print style sheet as it would be nice to avoid excessive ink usage. The print style sheet rule for the code sample should be changed to this:

p.code{
 border:1px solid #000;
 font-family:Verdana, Arial, Helvetica, sans-serif;
 font-size:10pt;
 color:#000;
 padding:4px;
}

We removed the background color, added a font family, font size of 10 points and a color of black as shown below:

Code sample revised

Finally, when we have new code inside my code block(s) we have a different font color (red), different font family and bold. This rule was named .newcode in my web style sheet and it looks like this:

.newcode{
 font:140% "Courier New", Courier, mono;
 color:rgb(204%,0%,0%);
}

In the print style sheet we changed some values as shown below:

.newcode{
 font:15pt "Courier New", Courier, mono;
 color:#000;
 font-style:italic;
 font-weight:bold;
}

We changed the font from 140% to 15 points. Kept the font family, changed the color to black, and to provide some emphasis to new code we added a font style of italic and a font weight of bold.

New Code Revision

Changing the Media Attribute

Before calling homepage_8.html a conclusion you need to change the media values for your style sheets. Both mfv4.css and mfv5.css need to be screen and print.css needs to be print as shown below:

<link  href="mfv4.css" rel="stylesheet" type="text/css"  media="screen">
<link  href="print.css" rel="stylesheet" type="text/css"  media="print">
<style  type="text/css" media="screen">
<!--
@import  url("mfv5.css");
-->
</style>

Refresh your page and voila, your web styles come back! Now you have a working version of a web style sheet and a print style sheet for your web site.

Conclusion

As you can see, converting your web site to a printer friendly version is not difficult. Now you know how to:

Finally, you were alerted to other mishaps that can occur when working with print style sheets and some ways to overcome such problems.

Now your pages will slide out of your printer looking as they’re supposed to look!

Printed Version Completed

-AND-

Web version of the same file

If you have questions, contact me.

top of page

Thank you for visiting Fabrizio.com - Come back again soon.