How to hide an element when printing a web page using CSS?
The media query is used to hide an element when printing a web pages. Use @media print query and set the visibility hidden to that element which need to hide at printing.
Example 1: In this example, hide the element h1 at printing time. To hide the element h1 use media query and set visibility:hidden.
<!DOCTYPE html> <html> <head> <title>Hide element at print</title> <style> body { text-align:center; } h1 { color:green; } @media print { .noprint { visibility: hidden; } } </style> </head> <body> <h1 class = "noprint">GeeksforGeeks</h1> <p>GeeksforGeeks: It is a computer science portal for geeks</p> </body> </html> |
Output:
Before printing the page:

After printing the page:

Example 2: Tn this example, use media query to hide image element when print the web pages.
<!DOCTYPE html> <html> <head> <title>Hide element at printing</title> <style> body { text-align:center; } h1 { color:green; } @media print { img { visibility: hidden; } } </style> </head> <body> <h1>GeeksforGeeks</h1> <p>A computer science portal for geeks</p> <img src="gfg.png" alt="image"> <style> .noprint { visibility: hidden; } </style> </body> </html> |
Output:
Before printing the page:

After printing the page:

Recommended Posts:
- ES6 | Page Printing
- HTML Course | First Web Page | Printing Hello World
- How to hide div element after few seconds in jQuery ?
- How to display search result of another page on same page using ajax in JSP?
- How to redirect a page to another page in HTML ?
- How to show Page Loading div until the page has finished loading?
- p5.js | hide() Function
- AngularJS | ng-hide Directive
- How to hide Bootstrap modal with JavaScript?
- How to Show and Hide div elements using Checkboxes ?
- Hide the cursor in a webpage using CSS and JavaScript
- How to hide a div when the user clicks outside of it using jQuery?
- How to hide elements in responsive layout using CSS ?
- Show and hide password using JavaScript
- Hide the Email Id in a form using HTML and JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



