Using css to only print hidden content
You can insert the printable content into a hidden div and use the @media print to unhide it.
.print {display:none;}
@media print {
body :not(.both) {display:none;}
.print {display:block!important;}
}
<html>
<head></head>
<body>
<div>
Screen content
</div>
<div class="both">Both media</div>
<div class="print">
Printable content
</div>
</body>
</html>
Use CSS to hide contents on print
I did it css3 way: using not pseudo class and direct ancestors (children)
/* hide all children of body that are not #container */
body > *:not(#container) {
display: none;
}
/* hide all children of #container that are not #content */
#container > *:not(#content) {
display: none;
}
/* and so on, until you reach a div that you want to print */
#content > *:not(#print_me) {
display: none;
}
This way you can eliminate everything except what you want to print even in very complex layouts. :not is very efficient here because it takes care about any future modifications of you html.
Print CSS Hide the body and show a div inside the body
You can combine the all/universal (*
) and :not
selectors:
body *:not(#flash):not(#menu):not(#anuncios) {display: none}
<div>Not here...</div>
<div id="flash">Hello</div>
<div id="menu">World</div>
<div id="anuncios">!</div>
<div>Also missing...</div>
Hide text on page but show it on window.print
you can use ´screen´ for regular screens so all you would have to do is
@media screen {
.hide-from-screen {
display: none;
}
}
@media print {
.hide-from-printer {
display: none;
}
}
and use those classes accordingly.
Hide container, but show content
That won't work. You can do specific tricks though.
If container
has a background, you can make the background transparent. You could 'undo' other styling in a similar fashion.
If container
has other content, you could wrap that content in spans or divs and hide those. If it contains just text, you could make the font-size: 0
, and set the font-size
of content
to a specific size (or 1rem
for default).
But all those possiblities are just work-arounds for the fact that you can't hide a parent while keeping the child visible, so basically I think option 3
is the best option you have.
hide HTML div when printing page
try this:
<style type="text/css" media="print">
.dontprint
{ display: none; }
</style>
<div class="dontprint">I'm only visible on the screen</div>
Related Topics
Scale HTML Table Before Printing Using CSS
How to Set a Flex-Container to Be the Width of Its Flex-Items
Css2 Attribute Selectors with Regex
Changing the Scrollbars' Style
Is There a Version of Yui Compressor That Deals Correctly with Media Queries
How to Draw an Incomplete Circle with CSS and in It How to Put a Picture
Chrome:Text Blurry When Skew Back:Skew(-10Deg) -> Skew(10Deg)
Ordering Wordpress Stylesheets
How to Use Svg Sprite Sheet as CSS Background-Image While Maintaining Aspect Ratio and Scalability
How to Define Constants in CSS
Make Second Div Appear Above First, Without Absolute Position or Changing HTML
Using '-Webkit-Overflow-Scrolling: Touch' Hides Content While Scrolling/Dragging
Tailwind CSS Classes Not Showing in Storybook Build
How to Vertically Center Align a Position:Relative Element
Set Font Size of Angular Material Tooltip
Firefox Unexpected Line Break Using Floats & Overflow Hidden