Home > Web Front-end > CSS Tutorial > How Can I Hide Elements Like Print Buttons When Printing a Web Page?

How Can I Hide Elements Like Print Buttons When Printing a Web Page?

Susan Sarandon
Release: 2024-12-27 10:10:14
Original
331 people have browsed it

How Can I Hide Elements Like Print Buttons When Printing a Web Page?

Concealing Elements During Web Page Printing

Printing a web page is a common task, but it can be frustrating to have unwanted elements appear in the printout. One such element that often poses a nuisance is the print button itself. If you're looking to remove the print button from your printed pages, here's how you can achieve this:

CSS Media Queries

Media queries in Cascading Style Sheets (CSS) allow you to define styles specifically for printouts. To hide elements when printing, you can use the @media print rule:

@media print {
    .no-print, .no-print * {
        display: none !important;
    }
}
Copy after login

Applying the Class

Once you have the CSS rule in place, you need to add a class to the element you want to conceal. You can either assign the no-print class directly:

<button class="no-print">Print</button>
Copy after login

Or append it to an existing class:

<button class="btn btn-primary no-print">Print</button>
Copy after login

Example

Consider the following example:

<p>Good Evening</p>
<button>
Copy after login
@media print {
    #print-button {
        display: none !important;
    }
}
Copy after login

When the page is printed, the "Print" button will be hidden, leaving only the text "Good Evening" on the printout.

Note:

The !important declaration ensures that the display: none style is applied even if other styles attempt to override it.

The above is the detailed content of How Can I Hide Elements Like Print Buttons When Printing a Web Page?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template