Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide all, show a class with css

Tags:

html

css

printing

Context: making printable invoices to generate in a browser.

It's common in making printable webpages to use an @media print rule to change the way the content looks for a printed page. Ideally, because I'm printing only a small part of the page, I'd like to hide everything and then display the contents of a particular element.

Structure is something like this:

<body>
    <div id="topMenu">...lots of elements...</div>
    <div id="sideMenu">...lots more...</div>
    <div class="tools">...some tools...</div>
    <div class="printing">...some elements I want to print...</div>
    <div class="tools">...more stuff I don't want to print...</div>
</body>

Stuff I've tried:

Ideally, I'd like to do something like

body * {
    display: none;
}
.printing, .printing * { /* Both parts are needed to make it display */
    display: block !important;
}

But this won't work because some elements need to be inline and some need to be block. I've played with some different values for display from MDN and can't find one that easily resets the value to its original. display: initial seems to be treated like inline.

The suggestion in CSS: "display: auto;"? seems to only work for JS.

Of course, it is possible to explicity "hide" the stuff I don't want printed rather than display the stuff I do want, but it seems to me that it should be possible to go the other way.

In this question How to only show certain parts with CSS for Print? suggests body *:not(.printable *) {display:none;} but notes (as backed up on the w3 negation page ) that this is not yet supported.

I note that the w3 draft and the display-outside page seem to recommend using an unknown (to webkit) box-suppress property to preserve the display value while not displaying the element.

My questions:

What is the best way to hide everything and target certain elements for display when they don't all share a common display property?

What exactly does box-suppress do?

like image 953
Josiah Avatar asked Oct 13 '14 17:10

Josiah


People also ask

How do you hide a class in CSS?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

How do I completely hide an element in CSS?

Completely hiding elements can be done in 3 ways: via the CSS property display , e.g. display: none; via the CSS property visibility , e.g. visibility: hidden; via the HTML5 attribute hidden , e.g. <span hidden>

How do you hide a class in HTML?

To hide an element, set the style display property to “none”.

How do I hide a div class?

We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.


1 Answers

Since you specifically tagged this CSS3, try using CSS3!

body>:not(.printing) {
    display: none;
}

This should work for the example you gave. I hope it works for your real-world application!


To answer your auxiliary question, as of October 2014, box-suppress is a possible future replacement for display:none that will hopefully make it easier to both hide and remove elements from the flow without worrying about changing its display type (as opposed to visibility still keeps it in the flow, and position:absolute which still keeps it visible). I don't think it's currently supported so I'd stay away from it for now. If you want to know more, see http://w3.org/TR/css-display

like image 120
Ky. Avatar answered Sep 29 '22 20:09

Ky.