Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS @media color Property

Posted in CSS Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 12 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
CSS @media color Property

Photo Credit to CodeToFun

πŸ™‹ Introduction

The @media color property in CSS is used within a media query to target devices based on their ability to display color. It allows you to apply different styles depending on whether the device supports color, grayscale, or monochrome displays. This is particularly useful when designing for a wide variety of devices, from high-definition screens to e-readers.

πŸ’‘ Syntax

The @media color property is used within a media query to detect if the device is capable of displaying colors. Here’s the basic syntax:

Syntax
Copied
Copy To Clipboard
@media (color) {
  /* Styles for color-capable devices */
}

@media not (color) {
  /* Styles for devices without color support */
}

🏠 Property Values

  • (color): Applies styles to devices that can display color.
  • not (color): Applies styles to devices that cannot display color, such as monochrome or grayscale displays.

πŸŽ›οΈ Default Value

There is no default value for the @media color property. It is a media feature used to query device capabilities, and by default, styles apply without media queries unless specified.

πŸ“ Example Usage

πŸ“œ Basic Usage

This example will apply a different background color based on whether the device supports color or not.

example.scss
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS @media color Example</title>
  <style>
    @media (color) {
      body {
        background-color: lightblue;
      }
    }
    
    @media not (color) {
      body {
        background-color: lightgray;
      }
    }
  </style>
</head>
<body>
  <h1>CSS @media color Example</h1>
  <p>The background changes based on the device's color capability.</p>
</body>
</html>

πŸ–₯️ Browser Compatibility

The @media color property is widely supported in modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. As with any media query, it is always a good idea to test the behavior across different devices and browsers to ensure that the styles are applied as expected.

πŸŽ‰ Conclusion

The @media color property is a great tool when creating responsive designs that need to account for devices with different display capabilities. By using this media feature, you can fine-tune your CSS to optimize the experience on both color and monochrome devices. Incorporating @media color into your designs ensures greater accessibility and flexibility across a wide range of devices.

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy