tags:

views:

379

answers:

5

I'm trying to show a print-preview div (#preview in examples). Is there a way to use print.css only for a particular div and its children overriding all local definitions?

Essentially, I would like to be able to do something similar to:

#preview element {
    definition equal to definition of an element in print.css
}

in main.css, but for a long list of definitions. It's not too DRY and following option is more coherent.

Second approach would be to include print.css into the main document and change each definition from

element {
    definition
}

to:

element, #preview element {
     definition
}

But that seems to me a bit cumbersome.
What would be the best way to solve this problem?

Update just to give an example:
in the main document I have red underlined links, they should be blue undecorated in print version. So when content of preview is dynamically formed, I pop up div where all links should be blue undecorated. But only in that particular div (#preview), in the rest of the document they still would be red and underlined.

+1  A: 

Maybe making the div an iframe that holds a different page styled by the print.css

Alternately you could set the media on print.css tag to be "print" but during print preview you could have a script change the media to "all".

John Isaacks
but the second approach would interfere with the local styles, wouldn't it?
SilentGhost
Yes. Plus the parent styles would inherit into the preview div. If you want completely separate styles, you'll have to use a dynamically-written iframe.
bobince
Print preview utilizes the media='print', so the browser 'print preview' will look like the printed output, not like the screen display.
Traingamer
+1  A: 

Maybe I still haven't grasped your question, but it seems you should be able to do this by simply linking the print.css stylesheet after your main stylesheet, and prefixing all the selectors in print.css with "#preview ".

In the links example, you would need to specify a style like:

#preview a, #preview a:link 
{
    text-decoration:none;
    color:blue;
}

This should be pretty simple unless you are dynamically creating the print.css file.

Joel Potter
If the main css media type is 'all', and the print css is 'print', then you only need to override these links as @Ithi points out. Good answer.
Traingamer
A: 
<link rel="stylesheet" href="/main.css" type="text/css">
<link rel="stylesheet" href="/print.css" type="text/css">
</head>
<body>
<div id="main">
content
</div>

main.css:
selector1 {style}
selector2 {style}
and so on

print.css:
#print selector1 {style}
#print selector2 {style}
and so on

Then, when you generate your print preview, just change (via JavaScript or whatever) the id of your main wrapper from "main" to "print".

Bill
A: 

As I recall, a more specific CSS declaration will always override a less specific declaration, unless the less specific one is marked as important.

So... #preview element { } will always override element { }

R. Bemrose
thats not 100% true, there are 4 levels of importance for example #id_declaration are higher than .class_declarations so #mydiv{} would outweigh .mydiv .someotherdiv .childdiv{}
John Isaacks
A: 

Add this to your stylesheet:

@media print {
  #preview a:link {
    color:blue;
  }
}
Nimbuz