scriptygoddess

08 Sep, 2002

for print css – for screen css

Posted by: Jennifer In: CSS

Awhile back there was a thread about how to have a "print version" of your site. I had done it with PHP, and people in the comments were talking about ways in which you could do it with CSS. Last night my brother showed me something he's working on (he's also in this field – java programming for the web. Next to him I'm a scriptydunce. LOL!)

The web site showed one thing, and when you went to print it, it showed something COMPLETELY different. NO WHERE on the page did you see the graphics that were being printed and vice versa. The answer is basically explained at the links that were in that comment thread (a list apart, NUblog) – but here's the Cliff Notes version…

** Calling the stylesheet based on whether it's for print or screen **
Where you're linking to your stylesheets you set up two versions (In a minute I'll get to what those actually look like)

<link rel="stylesheet" href="forprint.css" type="text/css" media="print" />
<link rel="stylesheet" href="forscreen.css" type="text/css" media="screen" />

** Defining the different stylesheets **
You'll probably have other styles as well depending on what other stuff you want to display, but here's the main ingredient:

Your PRINT css has this one:
.onscreen {
display: none;
}

Your SCREEN css has this one:

.onpaper {
display: none;
}

** Putting it all together **
put a DIV around the area you want to "hide" when you print:

<div class="onscreen">
Your html code stuff – will not display when you go to print…
</div>
<div class="onpaper">
Your html code stuff – will not display on the screen, only when you print…
</div>

8 Responses to "for print css – for screen css"

1 | Kevin

September 9th, 2002 at 9:19 am

Avatar

I know it's been talked about (even on this site!) before, but your post finally made the light bulb come on for me! I was always thinking, "Well, I don't WANT to link to a 'printable' version, I just want this one to print NICELY!"

Your post (in patricular the div & onscreen/onpaper info) made it all clear for me. Now all I gotta do is find 10 minutes to get it put in!! :)

2 | Melanie

September 10th, 2002 at 4:53 pm

Avatar

I link four stylesheets in my current journal layout, which has three columns, two static, the centre fluid. I also use print style to make the pages print nicely without having just a skinny strip of text on the page for the centre column.
The simplest way I found to do this is :

<style type="text/css" media="print"&lgt;
<!–
#left { visibility: hidden; }
#right{ visibility: hidden; }
#middle { margin-left: 10px; margin-right: 10px; color: #000000;}
–>
</style&lgt;

This simply alters the style on the divs I already have in the page, negating the need to add more.
It hides the left and right columns, and sets the margins on the centre column to nice widths for print, and the colour of the text to black. Prints better in black.

Of course, this won't stop any images in the middle column from printing, but if you wanted that you could simply add an img tag to the print style section and hide those too.

3 | Melanie

September 10th, 2002 at 4:55 pm

Avatar

pah! of course, those are meant to be >'s on the ends :)

4 | Jennifer

September 11th, 2002 at 2:10 pm

Avatar

Another use for this:
Lets say you want to hide certain areas of your site based on the possibility that the user may not have javascript enabled…
In your HEAD section (between your <head> and </head>)
put this:
<noscript>
<link href="/noscript.css" rel="stylesheet" type="text/css">
</noscript>

and in that noscript.css file have this:
.noscript {
display: none;
}

then around the areas you want to hide if the person doesn't have javascript:
<div class="noscript">
JAVASCRIPT ONLY STUFF…
</div>

5 | Christine

September 12th, 2002 at 2:29 pm

Avatar

I feel so bad. I, clueless about CSS for the most part, knew about this long ago. Matter of fact, I have to find the link I just sent to a programmer here about it. I think it was at A List Apart.

6 | Christine

September 12th, 2002 at 2:32 pm

Avatar

Found it! A List Apart: CSS : Going to Print. Maybe be even more handy tidbits there!

7 | Jennifer

September 12th, 2002 at 4:55 pm

Avatar

Christine! You're too funny… that link is in the post as well. 😉

My purpose for writing the post was because, as I said, I needed the cliff notes version to understand it… that aListApart article was almost too much information. Seeing just the code, in the most simplest use, finally it made sense to me…

8 | Joshua Swanagon

October 1st, 2003 at 12:01 pm

Avatar

Hello, great article, but I am having a problem with one feature and I was wondering what I am doing wrong. I am trying to add header and footer images for print only because I have my normal header and footer set to not print (because they carry all of the navigation which is not neccesary) and I need to have the logo print so that the visitor has a reference. I can get the images to not show on screen, but I can't get them to print. I have the onscreen CSS set ecxactly as above and have applied the class selector to my images but they still won't print. Does anybody have any ideas? Thanks in advance for the help. I'm sorry but because of the delicate nature of the client I cannot post any of the pages or code, not my choice it just has to be that way. But I can tell you that I have not altered the code in any way, and have applied it excactly how it is shown on this site. Once again thank you very much in advance.

Sincerely,
Joshua Swanagon

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements