Open In App

Recommend a way to optimize a certain webpage for prints using CSS

In this article, we are going to see how we can optimize a webpage for prints. Using CSS and media query in it we are going to achieve the task.

Printing a webpage is a very common practice and users can print a webpage for various reasons like storing data, some information that needs to be printed like tickets, etc.



Approach: Using the “@media printmedia query in CSS, we can specify what styles the webpage needs to apply when it is considered for printing.

 



Syntax:

@media print{
    /* CSS CODE */
}

You can place this query at the end of your stylesheet so that the styles apply to each element overriding the previous styles. Moreover, you can use “!important” to make your styles compulsory.

Tips to follow:

@media print{
    selector{
        display: none;
    }
}
@page{
    margin: 1cm;
    border: 2px solid black;
}
@media{
    a::after{
        content: "("attr(href)")";
    }
}

References:

Article Tags :