CSS Media Types

By Codes With Pankaj


CSS Media Types allow developers to apply different styles to different types of devices or media. By using media types, you can ensure that your website or application looks great on a variety of devices, from traditional desktop computers to smartphones, tablets, and even printers. This tutorial will explore CSS Media Types, explain how they work, and provide practical examples.

Table of Contents

  1. What are CSS Media Types?

  2. Why Use Media Types?

  3. Common CSS Media Types

    • Screen

    • Print

    • All

  4. Using Media Queries

    • Syntax of Media Queries

    • Combining Media Queries

  5. Advanced Media Features

    • Width and Height

    • Orientation

    • Resolution

    • Aspect Ratio

  6. Practical Examples

    • Example 1: Styling for Different Screen Sizes

    • Example 2: Print-Specific Styles

    • Example 3: Responsive Design with Media Queries

  7. Best Practices for Using Media Types and Queries

  8. Conclusion


1. What are CSS Media Types?

CSS Media Types are a way to apply specific styles depending on the type of device or media on which the content is being viewed. This means you can create different layouts, fonts, colors, and more depending on whether the content is viewed on a screen, printed out, or even on other media types like projectors or speech synthesizers.

Key Concept:

  • Media Type: A category that specifies the intended destination medium for the content, such as a screen or printer.

Example:

@media screen {
  body {
    font-size: 16px;
  }
}

@media print {
  body {
    font-size: 12px;
  }
}

Explanation:

  • The screen media type applies styles for devices like monitors, tablets, and phones.

  • The print media type applies styles when the document is printed.


2. Why Use Media Types?

Media Types allow you to create a responsive and adaptable website or application that looks and functions well across different devices and media. With the variety of devices available today, ensuring that your content is accessible and visually appealing on all platforms is crucial.

Benefits:

  • Device Adaptability: Tailor your design for different devices like desktops, tablets, and smartphones.

  • Print Optimization: Provide specific styles for printed versions of your web pages.

  • Enhanced User Experience: Ensure that users have the best experience possible on their device.


3. Common CSS Media Types

There are several media types you can use, but the most common ones are screen, print, and all.

a. Screen

The screen media type is used for computer screens, tablets, smartphones, and other devices with a display. This is the most commonly used media type and is essential for creating responsive designs.

Example:

@media screen {
  body {
    font-family: Arial, sans-serif;
  }
}

Use Case:

  • Applying styles specifically for content displayed on digital screens.

b. Print

The print media type is used for content that will be printed on paper. This allows you to create print-friendly styles, such as adjusting font sizes, hiding certain elements, or changing the layout to fit better on a printed page.

Example:

@media print {
  body {
    font-size: 10pt;
    color: black;
  }
  .no-print {
    display: none;
  }
}

Use Case:

  • Optimizing a web page's appearance for printing, such as hiding navigation menus or advertisements.

c. All

The all media type is a catch-all that applies to all media types, including both screen and print. It is rarely used on its own, as it's typically combined with other media features in more specific media queries.

Example:

@media all {
  body {
    line-height: 1.5;
  }
}

Use Case:

  • Applying styles universally across all media types.


4. Using Media Queries

Media Queries allow you to apply styles based on conditions other than just the media type. These conditions include the width and height of the viewport, the device's orientation, resolution, and more.

a. Syntax of Media Queries

Media Queries have a specific syntax that allows you to apply styles when certain conditions are met.

Basic Syntax:

@media media-type and (media-feature) {
  /* CSS rules here */
}

Example:

@media screen and (max-width: 600px) {
  body {
    font-size: 14px;
  }
}

Explanation:

  • This media query applies styles only to screens with a maximum width of 600 pixels.

b. Combining Media Queries

You can combine multiple media queries to apply styles under more complex conditions.

Example:

@media screen and (min-width: 600px) and (orientation: landscape) {
  body {
    font-size: 18px;
  }
}

Explanation:

  • This media query applies styles only to screens that are at least 600 pixels wide and are in landscape orientation.


5. Advanced Media Features

Beyond basic media types, you can use various media features to target specific characteristics of the device or environment.

a. Width and Height

You can use the width and height media features to apply styles based on the size of the viewport or the entire device.

  • max-width: Applies styles up to a maximum width.

  • min-width: Applies styles from a minimum width and up.

Example:

@media screen and (min-width: 768px) {
  .container {
    width: 750px;
  }
}

Use Case:

  • Creating responsive designs that adapt to different screen sizes.

b. Orientation

The orientation media feature targets the orientation of the device—whether it is in portrait (vertical) or landscape (horizontal) mode.

Example:

@media screen and (orientation: portrait) {
  img {
    width: 100%;
  }
}

Use Case:

  • Adjusting layout or image sizes based on whether the device is held in portrait or landscape mode.

c. Resolution

The resolution media feature targets the pixel density of the display, which is useful for optimizing graphics for high-resolution displays like Retina screens.

Example:

@media screen and (min-resolution: 2dppx) {
  .logo {
    background-image: url('logo-highres.png');
  }
}

Use Case:

  • Providing high-resolution images or assets for devices with high pixel densities.

d. Aspect Ratio

The aspect-ratio media feature targets the width-to-height ratio of the viewport or device, which is useful for creating layouts that adjust to wide or tall screens.

Example:

@media screen and (min-aspect-ratio: 16/9) {
  video {
    width: 100%;
  }
}

Use Case:

  • Adjusting the size of video players or other content based on the aspect ratio of the screen.


6. Practical Examples

Let’s look at some practical examples of using media types and queries in real-world scenarios.

Example 1: Styling for Different Screen Sizes

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Design Example - codeswithpankaj</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }

    .container {
      width: 100%;
      max-width: 1200px;
      margin: 0 auto;
    }

    @media screen and (max-width: 768px) {
      .container {
        padding: 20px;
      }
    }

    @media screen and (max-width: 480px) {
      .container {
        padding: 10px;
      }
    }
  </style>
</head>
<body>

<div class="container">
  <h1>Responsive Design Example</h1>
  <p>This container adjusts its padding

 based on the screen size.</p>
</div>

</body>
</html>

Explanation:

  • The container's padding changes as the screen size decreases, making the layout more responsive to different devices.

Example 2: Print-Specific Styles

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Print Styles Example - codeswithpankaj</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f0f0f0;
      color: #333;
    }

    .no-print {
      display: none;
    }

    @media print {
      body {
        background-color: white;
        color: black;
      }

      header, footer, .no-print {
        display: none;
      }
    }
  </style>
</head>
<body>

<header>
  <h1>This header will not be printed</h1>
</header>

<main>
  <h2>Content for printing</h2>
  <p>This content will be printed with a white background and black text.</p>
</main>

<footer>
  <p>This footer will not be printed</p>
</footer>

</body>
</html>

Explanation:

  • The print styles hide the header, footer, and any element with the no-print class while adjusting the text and background colors for print-friendly output.

Example 3: Responsive Design with Media Queries

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Navigation - codeswithpankaj</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }

    nav ul {
      list-style-type: none;
      padding: 0;
      margin: 0;
      display: flex;
      justify-content: space-around;
      background-color: #333;
    }

    nav ul li {
      display: inline;
    }

    nav ul li a {
      color: white;
      text-decoration: none;
      padding: 15px;
      display: block;
    }

    @media screen and (max-width: 600px) {
      nav ul {
        flex-direction: column;
        text-align: center;
      }
    }
  </style>
</head>
<body>

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

</body>
</html>

Explanation:

  • The navigation menu changes from a horizontal layout to a vertical layout when the screen width is 600 pixels or less, improving usability on smaller devices.


7. Best Practices for Using Media Types and Queries

When using media types and queries, consider the following best practices:

  • Start with a Mobile-First Approach: Design your styles for mobile devices first, then add media queries to adapt the design for larger screens.

  • Use Relative Units: Use relative units like em or % for widths, padding, and margins to create more flexible layouts.

  • Test Across Devices: Regularly test your designs on different devices and screen sizes to ensure that they look and function as expected.

  • Keep It Simple: Avoid overcomplicating your media queries. Focus on the most common breakpoints and use them to create a cohesive design.


8. Conclusion

CSS Media Types and Media Queries are essential tools for creating responsive and adaptable designs. By understanding how to use different media types, such as screen, print, and all, and by leveraging media queries to target specific device characteristics, you can create web pages that look great on any device.

  • Media Types: Target different types of media like screens or printers.

  • Media Queries: Apply styles based on conditions like screen width, height, orientation, and resolution.

  • Advanced Features: Use advanced media features like orientation, resolution, and aspect-ratio to fine-tune your designs.

By mastering these concepts, you can create more flexible, user-friendly, and visually appealing web designs.


For more tutorials and insights, visit Codes With Pankaj.

Last updated