Print specific part of webpage
The code snippet below is the fast lane to printing a specific section of your webpage with JavaScript:
That's it. Just call printSection('idOfSectionToPrint')
, providing the element's ID you're eager to print. Here you go, a brand-new popup and a ride to the print dialog!
Beefed-up mode: Print with extras
While the code above is the bread and butter of printing, let's step things up a notch with some extra tricks.
Dress it up with CSS media queries
To ensure that the print view matches the tie and pocket square your content is donning (no pun intended!), it's a good idea to use @media print
to define styles specifically for printed content:
Get in the groove with jQuery
If you're using jQuery on your project, make things a bit smoother by simplifying the printing process:
Taking care of dynamic content and cross-browser compatibility
Is your site as dynamic as a Broadway dancer? Ensure that the print function is triggered after all data is loaded for the smooth moves! Also, don't overlook cross-browser compatibility, as some users might face issues depending on their browser version.
The print nitty-gritty
Making Print Accessible
A good print button or link should be as easy to find as a cat at a tuna factory. Here's how you can take care of it:
Manoeuvring complex structures
Got a Picasso-like complex structure? Dynamically change the printable area by showing only the section to print temporarily.
Handling iframes
Trapped content in an iframe? You can still bring it out to the print world:
Was this article helpful?