How to specify the target media types of a set of CSS rules

The media attribute on CSS <link> elements and @media rules allows you to specify which types of devices or media should apply specific CSS styles. This enables responsive design and device-specific styling.

Syntax

/* Using @media rule */
@media media-type {
    /* CSS rules */
}

/* Using link element */
<link rel="stylesheet" media="media-type" href="styles.css">

Common Media Types

Media Type Description
all Suitable for all devices (default)
print Intended for printed documents
screen Intended for computer screens
speech Intended for speech synthesizers

Example: Using Link Element with Media Attribute

The following example shows how to link different stylesheets for different media types −

<!DOCTYPE html>
<html>
<head>
    <title>Media Types Example</title>
    <style>
        /* Screen styles */
        @media screen {
            body {
                font-family: Arial, sans-serif;
                background-color: #f0f8ff;
                color: #333;
                padding: 20px;
            }
            .content {
                border: 2px solid #4CAF50;
                padding: 15px;
                border-radius: 8px;
            }
        }
        
        /* Print styles */
        @media print {
            body {
                font-family: Times, serif;
                background-color: white;
                color: black;
                padding: 0;
            }
            .content {
                border: 1px solid black;
                padding: 10px;
            }
            .no-print {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="content">
        <h1>Media Types Demo</h1>
        <p>This content appears differently on screen vs print.</p>
        <button class="no-print">Screen Only Button</button>
    </div>
</body>
</html>
On screen: A blue background page with green bordered content box and a visible button.
When printed: Clean black text on white background with simple border, button hidden.

Example: Multiple Media Types

You can specify multiple media types using commas −

<!DOCTYPE html>
<html>
<head>
    <style>
        @media screen, print {
            .universal {
                font-weight: bold;
                margin: 10px 0;
            }
        }
        
        @media screen {
            .universal {
                color: blue;
                background-color: #e6f3ff;
            }
        }
        
        @media print {
            .universal {
                color: black;
                background-color: transparent;
            }
        }
    </style>
</head>
<body>
    <div class="universal">
        This text is styled for both screen and print, with specific colors for each.
    </div>
</body>
</html>
A bold text block that appears blue with light blue background on screen, but black on white when printed.

Conclusion

Media types allow you to create device-specific styles using the media attribute in <link> elements or @media rules. This is essential for responsive design and creating print-friendly versions of web pages.

Updated on: 2026-03-15T11:35:15+05:30

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements