CSS Media Queries
CSS Media Queries are a formidable tool whenever you want to style a website differently based on one of several different conditions including the screen width, device type, orientation, etc. Media queries are by nature dynamic; they help you to create geography responsive designs that look good on anything from a phone to a desktop computer.
Why Use CSS Media Queries?
Historically, developers used to manually create different versions of a website for every device type. CSS Media Queries eliminate this necessity, and allow you apply styles based on the viewport size or device capabilities. Here are some reasons why you should be using CSS Media Queries:
- Responsive Design: Media Queries are the mechanism to ensure that your site is "responsive" across screen sizes; this is vital for a mobile-friendly site, and better user experience
- Better User Experience: Different characteristics of the device can result in different layouts based on device styling to help improve the user experience and general usability across various devices
- Efficient Resources: Instead of duplicating all of that code you can build for each device, Media Queries maximize for code reuse; this is a significant advantage in preferring a single stylesheet to reduce duplication
Basic Syntax of CSS Media Queries
The basic syntax for a CSS Media Query looks like this:
@media (condition) {
/* CSS rules here */
}
Media queries can utilize any number of conditions, such as screen width / height and resolution. Also, orientation and media type are valuable in targeting and applying styles. The CSS rules inside the query will only apply when the condition is true.
Example of a Simple Media Query
@media (max-width: 768px) {
body {
background-color: lightblue;
}
}
This media query changes the background color to light blue when the viewport width is 768px or less (typically for tablets and mobile devices).
Common Media Query Conditions
Here are some of the most commonly used conditions in CSS Media Queries:
width/height: These conditions target the width or height of the viewport. You can usemax-width,min-width,max-height, andmin-heightto create responsive designs that adjust based on the screen size. Example:@media (max-width: 600px) { /* Styles for smaller screens */ }orientation: This condition targets the orientation of the device, whether it’s in landscape or portrait mode. Example:@media (orientation: landscape) { /* Styles for landscape mode */ }resolution: This targets the screen resolution. It’s useful for creating high-resolution designs for devices with retina displays. Example:@media (min-resolution: 192dpi) { /* Styles for high-resolution screens */ }aspect-ratio: You can use this condition to apply styles based on the aspect ratio of the viewport. Example:@media (max-aspect-ratio: 16/9) { /* Styles for 16:9 aspect ratio screens */ }
Examples of CSS Media Queries
Making the Layout Responsive
Here’s an example of using CSS Media Queries to create a responsive layout that changes based on the screen size:
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
}
@media (max-width: 768px) {
.container {
grid-template-columns: 1fr 1fr;
}
}
@media (max-width: 480px) {
.container {
grid-template-columns: 1fr;
}
}
This structure displays three equal width columns at large size, while displays two column on tablets and one
column on
mobile. The grid-template-columns property changes based on the screen width.
Changing Typography Based on Screen Size
Another common use case for Media Queries is adjusting typography for different screen sizes:
h1 {
font-size: 36px;
}
@media (max-width: 768px) {
h1 {
font-size: 28px; }
}
@media (max-width: 480px) {
h1 {
font-size: 24px; }
}
This example reduces the font size of h1 headers as the screen size decreases, ensuring that
text doesn’t overflow on smaller devices.
Advanced Usage of Media Queries
Using Multiple Media Queries
The other flexibility with CSS Media Queries is that they are essential in responsive web design, where they give you the capability to develop fluid and responsive layouts that will adapt on any devices. CSS Media Queries provide you the opportunity to design any website to be visually pleasing and improve user experience on all screen sizes or devices. You can also use multiple Media Queries for more complex layouts. CSS Media Query rules are read from top to bottom and can be targeted for specific screen sizes across a particular range:
@media (min-width: 1200px) {
/* Styles for large screens (desktops) */
}
@media (min-width: 1199px) and (min-width: 768px) {
/* Styles for tablets */
}
@media (min-width: 767px) {
/* Styles for mobile devices */
}
Media Queries with Specific Devices
You can also target specific devices using media queries based on their device characteristics. For example, you can target devices with touch capabilities:
@media (pointer: coarse) {
/* Styles for touch devices */
}
Best Practices for Using CSS Media Queries
- Mobile-First Design: Think of mobile as your smallest screen size. Start with mobile design and make use of media queries to change your design for larger screens - this will allow for a mobile-optimized design.
- Keep It Simple: Avoid creating complicated media queries. Simplify your breakpoints logically so that it’s easier to maintain.
- Use Relative Units: when setting styles and breakpoints, go with relative units like
emandremusers use flexible type sizing and with relatively positioned items, you will have more values to work with.
Conclusion
CSS Media Queries are a great resource for responsive web design, allowing you to create "flexible and adaptive" layouts to work across multiple devices. CSS Media Queries allow you to make any site look great and improve the user experience, no matter what screen size and devices are being used.