CSS Basic
CSS @media update Property
Photo Credit to CodeToFun
π Introduction
The CSS @media
rule is used to apply styles to different devices and screen sizes, enabling responsive design.
This rule allows developers to set breakpoints and specify what CSS should be applied based on the characteristics of the userβs device, such as screen width, height, orientation, and resolution. The @media
rule plays a key role in creating flexible layouts that adapt to varying screen sizes.
π‘ Syntax
The syntax for the @media
rule is as follows:
@media media-type and (media-feature) {
/* CSS rules here */
}
- media-type: Specifies the type of device (e.g., screen, print, etc.).
- media-feature: Defines the condition under which the style will be applied (e.g., max-width, min-width, etc.).
π Property Values
- media-type: This can be any device, such as:
- screen: (default for most cases)
- print: (for print styles)
- all: (applies to all media types)
- media-feature: This defines the features of the device, such as:
- min-width: Minimum width of the screen.
- max-width: Maximum width of the screen.
- orientation: The orientation of the device (portrait or landscape).
- resolution: Specifies the screen resolution.
ποΈ Default Value
There is no default value for the @media
rule itself, as it is a conditional rule that applies different CSS based on the conditions provided.
π Example Usage
π Basic Usage
Here is a simple example that changes the background color of the body based on the width of the screen:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS @media Example</title>
<style>
body {
background-color: lightblue;
}
@media screen and (max-width: 600px) {
body {
background-color: lightgreen;
}
}
</style>
</head>
<body>
<h1>Resize the window to see the effect!</h1>
<p>The background color will change when the screen width is less than 600px.</p>
</body>
</html>
In this example, the background color of the page changes to light green if the screen width is less than or equal to 600px.
π₯οΈ Browser Compatibility
The @media
rule is supported by all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is a fundamental part of responsive design and works across all platforms. However, as always, it is recommended to test your design across different devices to ensure a smooth user experience.
π Conclusion
The CSS @media
rule is an essential tool for responsive web design. It allows developers to apply styles based on various device characteristics, ensuring that websites are adaptable and provide an optimal user experience on different screens.
By utilizing @media
, you can create designs that look great on both small mobile screens and large desktop monitors.
π¨βπ» Join our Community:
Author
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
If you have any doubts regarding this article (CSS @media update Property), please comment here. I will help you immediately.