You have coded a beautiful page that has a few buttons, some text, a toolbar, a footer with the mandatory copyright statements, and so on. And now your users want to take the print out of the web page and ask you to include that feature.
The first thing that comes to your mind is to just use the browser feature to print any web page (or to save as PDF). Well, it gets the job done but along with your beautiful, print-worthy content will be the buttons, footer, and what not. So, you get to wonder “is it possible to get rid of things only for prints but not on the web page UI”? The answer is “yes”, of course - I am writing a whole post about it.
And, a whole post about a couple of simple statements in CSS that can make elements magically disappear.
|
|
All this will do is to instruct browser to hide any element with class no-print
- only while printing the page (that’s the @media
). This method works independent of frameworks and you can use it to hide elements in Vue, React, et al.
For e.g. consider this page.
|
|
The toolbar and footer tag along for the prints.
Let us change that. Include the simple CSS provided earlier and use the CSS class against all elements that do not need to be printed.
For e.g. we do this to hide the toolbar -
|
|
The end result is a cleaner print-out (or PDF) with no unecessary elements.
Simple and elegant.
Find the code on Codepen.