52

I have the following CSS for my print style:

* {
 display:none;
}

#printableArea {
 display:block;
}

I expected this to hide all elements, and only show the printableArea, however everything gets hidden. In print view, all I get is a blank page.

I have it included properly in the HEAD, with media="print" on this particular stylesheet.

grg
  • 5,023
  • 3
  • 34
  • 50
mathiscode
  • 1,509
  • 3
  • 13
  • 10

11 Answers11

35

If an element is not displayed, then none of its children will be displayed (no matter what their display property is set to).

* matches the <html> element, so the entire document is hidden.

You need to be more selective about what you hide.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Ah, thanks! I guess I could just place printableArea outside of the page wrapper, then hide the wrapper, showing the print div. – mathiscode Oct 02 '09 at 21:10
  • 1
    @FatherCarbon - that's what I've just implemented and it looks like it'll do the trick nicely – Michael12345 Mar 28 '14 at 22:08
27

You're taking the right general approach, but you want to use visibility: hidden instead of display: none so that you can set child elements to be visible.

See Print <div id=printarea></div> only?

Community
  • 1
  • 1
Charles Offenbacher
  • 3,094
  • 3
  • 31
  • 38
11
html body * {
 display:none;
}

#printableArea {
 display:block;
}

Also, you may need an !important on #printableArea, but probably not.

PHLAK
  • 22,023
  • 18
  • 49
  • 52
10

Answering because I found this question while searching for this

Instead of 'display: none' you can use :

* {
  visibility: hidden;
  margin:0; padding:0;
}

#printableArea * {
  visibility: visible;
}

source : https://www.concrete5.org/community/forums/5-7-discussion/need-to-print-a-certain-div-and-ignore-everythign-else-on-the-pa

Salix
  • 1,290
  • 9
  • 15
  • 2
    This works but how do you make it so the `#printableArea` moves to the top left of the paper instead of retaining it's position in the HTML/CSS structure retained by using `visibility:hidden`? – Dawson Irvine Feb 21 '21 at 06:20
  • I did not have that issue when removing margins and padding. Altho, if you want it to the left, make sure it's aligned to the left? You coud always make it float or change it's position to absolute. Also, if you follow the link, someone uses `display: none` on specific classes. The issue here is that if you do it on `*` selector t will hide everything, including what you want tp print. – Salix Feb 23 '21 at 23:10
  • Using `display: none` on certain classes is what I eventually ended up doing. It's not as "universal" as I would prefer but it will work for the time being. – Dawson Irvine Feb 25 '21 at 17:01
8

You might try popping it up on top of everything. This solved 90% of my problems, then I just had to make a .noprint class and add it to a few straggling elements.

.print_area{
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    z-index: 9999;

    background-color: #ffffff;
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Tarek Adam
  • 3,387
  • 3
  • 27
  • 52
5

If you want to use JavaScript, you can try this simple snippet that doesn't even require jQuery:

document.body.innerHTML=document.getElementById('printableArea').innerHTML;
Chad
  • 3,159
  • 4
  • 33
  • 43
1

make a div wrap everything after the body tag. Before the wrap div, put the visible item's div.

I had to do this to make a simple username-password page, and needed to hide everything, except the half-opaque sign-in form's background. So, after the correct credentials were typed in, the form would animate out, and the half-opaque page cover would animate out, and finally, EVERYTHING aside would show up and you could use the page normally.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
1
@media print {
    * {
        visibility: hidden;
    }

    /* Show element to print, and any children he has. */
    .svgContainer, .svgContainer * {
        visibility: initial;
    }
}

Make sure any children elements are also visible. Remember that invisible elements still influence positionning of other elements in the page. In my (simple) case, I just added position: fixed; on .svgContainer (somewhere else).

Ambroise Rabier
  • 3,636
  • 3
  • 27
  • 38
0

There is a one-line solution:

With JQuery

var selector = '';
$(document.head).append($('style').text('*{visibility:hidden}' + selector + '{visibility:visible}'));

Without JQuery

var selector = '';
document.head.appendChild(Object.assign(document.createElement('style'), { innerText: '*{visibility:hidden}' + selector + '{visibility:visible}' });

In both examples, set the selector variable to the selector you want. For example, div#page:hover or p.class1,p.class2

spacefluff432
  • 566
  • 3
  • 13
0

Simply you can use the following code and assign "hide" class to that specific element you dont want to display on print page

<style type="text/css" media="print">
    img
    {
        display:none;
    }
    .hide
    {
        display:none;
    }

</style>
0

There is another clean way to achieve this:

* {
    visibility: hidden;
}

#printableArea {
    visibility: visible;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
}

That way you're going to get only the #printableArea element in the print view and all of the other elements will be hidden.

Jaruzel
  • 99
  • 1
  • 7