Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only show certain parts with CSS for Print?

I have a page with lots of data, tables and content. I want to make a print version that will only display very few selected things.

Instead of writing another page just for printing, I was reading about CSS's feature for "@media print".

First, what browsers support it? Since this is an internal feature, it's OK if only the latest browsers support it.

I was thinking of tagging a few DOM elements with a "printable" class, and basically apply "display:none" to everything except those elements with the "printable" class. Is that doable?

How do I achieve this?

EDIT: This is what I have so far:

<style type="text/css"> @media print {     * {display:none;}     .printable, .printable > * {display:block;} } </style> 

But it hides everything. How do I make those "printable" elements visible?

EDIT: Trying now the negative approach

<style type="text/css"> @media print {     body *:not(.printable *) {display:none;} } </style> 

This looks good in theory, however it doesn't work. Maybe "not" doesn't support advanced css ...

like image 402
Nathan H Avatar asked Aug 12 '10 00:08

Nathan H


People also ask

How do I hide the elements when printing a website using CSS?

To hide the element, add “display:none” to the element with CSS.

How do I print a CSS page style?

You can use CSS to change the appearance of your web page when it's printed on a paper. You can specify one font for the screen version and another for the print version. You have seen @media rule in previous chapters. This rule allows you to specify different style for different media.

What is @media print?

Print media, as you know is one of them. Print media is one of the oldest and basic forms of mass communication. It includes newspapers, weeklies, magazines, monthlies and other forms of printed journals. A basic understanding of the print media is essential in the study of mass communication.


1 Answers

Start here. But basically what you are thinking is the correct approach.

Thanks, Now my question is actually becoming: How do I apply CSS to a class AND ALL OF ITS DESCENDANT ELEMENTS? So that I can apply "display:block" to whatever is in the "printable" zones.

If an element is set to display:none; all its children will be hidden as well. But in any case. If you want a style to apply to all children of something else, you do the following:

.printable * {    display: block; } 

That would apply the style to all children of the "printable" zone.

like image 122
Strelok Avatar answered Oct 09 '22 06:10

Strelok