In today’s world, designing websites that work seamlessly across different devices is extremely important. One of the powerful tools in the hands of web designers for this purpose is Media Queries in CSS. Media Queries allow us to implement specific styles for particular devices or certain conditions. For instance, we can make specific adjustments for devices with a width less than 600 pixels, increasing font sizes accordingly. This feature allows users to read content more easily without the need to zoom in on the page.
You might ask how Media Queries work; the basics are quite simple. You start with a condition that changes the CSS styles when certain criteria are met. Then, the CSS codes are specified inside a defined block. For example, if you want the content to appear in red for devices with a width less than 600 pixels, the code may look something like this.
These adjustments are very user-friendly and can greatly improve the user's experience. Since this site may display in a specific format or size, depending on the device and user location, we can also utilize Media Queries for hiding or displaying certain elements based on page dimensions. For example, you might want the main site layout on desktop to appear differently from its mobile counterpart.
The interesting thing is that you can also adjust Media Queries based on other features such as device type, brightness level, and even the resolution of the page. For example, for devices that are in portrait mode, you can apply specific designs. This subject can help maintain an organized and reader-friendly design without considering page dimensions, ensuring that the layout remains coherent and user-friendly.
Additionally, examples of diverse uses of Media Queries can be utilized as templates for improving your site design. Correctly using these queries can create visual differences in how your site displays on different devices.
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
@media only screen and (orientation: portrait) {
header {
display: none;
}
}
Line-by-Line Explanation of the Code
@media only screen and (max-width: 600px)
This line specifies that the media query applies to devices with a maximum width of 600 pixels.
body
Styles pertaining to the element
body
are defined within this block.background-color: lightblue;
The background color changes to light blue.
@media only screen and (orientation: portrait)
This line defines a media query for devices in portrait orientation.
header
Styles pertaining to the element
header
are defined in this block.display: none;
This setting ensures that the header does not display in this state.