Forum Moderators: not2easy

Message Too Old, No Replies

CSS @Media Print issue

         

nbozic

10:36 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Hi,

I spent a long time trying to figure this out, but without much success.
When users want to print a page, I would like only a specific section on the webpage to be printed. You can create a HTML page on your desktop to see my point, with the code provided at the bottom of this message.
I would only like the certain div (id="div1") and a certain table (id="tbl1") to be printed, which are originally located to the RIGHT of the webpage.
The below code only does one thing decently - hiding the stuff I don't want printed - but the layout of the page is still exactly the same. In other words, I didn't get rid of the stuff I wanted to get rid of, it just became see-through (hidden). I want the visible part that's printed to be aligned to the left, as if nothing is keeping it from being aligned to the left - which is not the case (the hidden stuff is creating a "left margin", and making the visible stuff aligned to the right).
I also experimented a lot with "display:none;" CSS attribute, but it only made things worse by hiding everything and makign it impossible to unhide the things I wanted visible.

Any help would be appreciated :)

Thanks,

NB

HTML / CSS Code:
------------------------------

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
@media print {
.tbl_global{visibility:hidden;}
#tbl1{visibility:visible;}
#div1{visibility:visible;}
}
</style>
</head>
<body>

<table width="90%" border="0" cellpadding="4" cellspacing="0" class="tbl_global">
<tr>
<td valign="top" align="left" width="20%">
Item 1<br><br>
Item 2<br><br>
Item 3<br><br>
Item 4<br><br>
Item 5<br><br>
</td>
<td valign="top" align="left" width="20%">
<table width="100%" border="0" cellpadding="4" cellspacing="0" align="left">
<tr>
<td>
Item A<br><br>
Item B<br><br>
Item C<br><br>
Item D<br><br>
Item E<br><br>
</td>
</tr>
</table>
</td>
<td valign="top" align="left" width="60%">
<div id="div1">
Test DIV<br><br>
</div>
<table width="100%" border="0" cellpadding="4" cellspacing="0" align="left" id="tbl1">
<tr>
<td>
Item #<br><br>
Item &amp;<br><br>
Item @<br><br>
Item!<br><br>
Item %<br><br>
</td>
</tr>
</table>
</td>
</tr>
</table>

</body>

----------------------------------

Robin_reala

11:27 pm on Dec 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use display: none they you'd have to use display:block or equivalent to 'unhide' the other bits.

Me, I tend to do it the other way round. On bits that I don't want to see I put class="noprint", then in the print stylesheet .noprint{display:none;}

nbozic

2:20 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



I guess that the whole problem lies in trying to display a table that's nested in the table to be hidden. A while ago when I tried to do something similar - when the tables were separate/independent - it was quite easy. I simply put display:none for the tables to be hidden, and they were gone. Since that content was gone for good (instead of simply being hidden), there were no "margins" to maintain the original formatting and push the content to the right.

Right now, I have to hide the main table (which hides everything), then unhide the table I want printed... it just doesn't seem to work.
I tried using display:block (and other similar ones) to accmplish this, but it didn't unhide the table - the printout was completely empty. It seems that only visibility:visible; seems to work for unhiding items, in which case I have to use visibility:hidden; for the main table - otherwise the whole thing won't work.
The drawback, of course, is that the formatting will stay exactly the same, in which case I didn't accomplish my goal... the printed content is aligned to the right.

Timmo

4:33 am on Dec 24, 2004 (gmt 0)

10+ Year Member



Hi,

I am having exactly the same problem as...

I want specific sections (divs and tables) to display on the PRINT version of my files, but, apparently because they are inside other tables, it won't work...

I have tried display="none" and display="block" too, without any good results.

I've been searching on the web for a solution for a few hours, and the best ressource I found so far is your post... because I now know that I'm not alone :)

Have you found a way to fix that problem yet?

Thank you very much.

Timmo

6:35 am on Dec 24, 2004 (gmt 0)

10+ Year Member



I found the official explanation:
==============
NONE:
This value causes an element to generate no boxes in the formatting structure (i.e., the element has no effect on layout). Descendant elements do not generate any boxes either; this behavior cannot be overridden by setting the 'display' property on the descendants.
Please note that a display of 'none' does not create an invisible box; it creates no box at all. CSS includes mechanisms that enable an element to generate boxes in the formatting structure that affect formatting but are not visible themselves. Please consult the section on visibility for details.
(http://www.w3.org/TR/1998/REC-CSS2-19980512/visuren.html#display-prop)
==============

The key sentence here is "this behavior cannot be overridden by setting the 'display' property on the descendants"...

Anyway, I got my page half fixed by changing the layout. Though, if anyone has anything to add on that matter, I'd apreciate.

Thank you!

Robin_reala

8:24 am on Dec 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, interesting. I didn't realise that.

nbozic

10:18 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



I finally got it to work!
Instead of messing with <table> tags and trying to hide/unhide them, I simply set the <td> tags (the ones I wanted gone from the main/global table) to display:none. This way the only thing that was left was the main table only holding the certain <td> that had content I wanted to keep.

For example (this was the original content):
<table>
_<tr>
__<td>BlaBla</td>
__<td>BlaBla</td>
__<td><Table that has content of interest></td>
_</tr>
</table>

This is the printed result:
-------------
BlaBla____BlaBla____Stuff I Want Printed
-------------

After trying to hide/unhide tables, this is the best printout I got (It has an annoying left margin because the formatting stayed the same):
-------------
__________________Stuff I Want Printed
-------------

By setting the unwanted <td> tags to display:none, this is what I finally got:
-------------
Stuff I Want Printed
-------------

I guess I just didn't know that one can control <TD> tags.

NB