Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have different CSS when I print or print preview?

Tags:

css

I'd like to change some things on my web page. Also I would like to make some things hidden. Is there a way I can do this with CSS when I print? In particular I want to be able to hide some DIVs and all that they contain.

like image 505
MIMI Avatar asked Jul 03 '11 17:07

MIMI


2 Answers

This can be achieved with a separate print stylesheet. The media attribute is the key:

<link rel="stylesheet" type="text/css" href="print.css" media="print" />
like image 123
Viktor Avatar answered Sep 19 '22 13:09

Viktor


Yes, you need to use the media attribute when you include your css. E.g.

<link rel="stylesheet" href="my_print_style.css" media="print">

Or, you can use the media rule in your stylesheets if for example, you do not have enough changes to warrant a whole new stylesheet. Something like this,

@media print {
    // print specific styles.
}

See http://www.w3.org/TR/CSS2/media.html#at-media-rule, for details and valid media types.

like image 28
tjm Avatar answered Sep 20 '22 13:09

tjm