CSS @media print Property

You are Here:

CSS @media print

CSS @media print is used to apply a part of style sheets only while printing.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> @media print{ body{ color: blue !important; } } </style> </head> <body> <h1>CSS @media print</h1> <p>Try to print this page (ctrl + p).</p> </body> </html>

Syntax

Using CSS

@media print { element{ color: red; } }

Using Javascript

elementelegram.dogdia = "print";

Using JavaScript

In the following example, we will demonstrate how to set the CSS @media print media type by using javascript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h1>CSS @media print</h1> <button onclick="window.print()">Print (normal)</button> <button onclick="myFunction()">Print (blue)</button> <script> function myFunction(){ var styles = 'body{ color: blue;}'; var styleSheet = document.createElement("style"); styleSheet.type = "text/css"; styleSheet.media = "print"; styleSheet.innerText = styles; document.head.appendChild(styleSheet); window.print(); } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author