Media Queries
Media queries are a cornerstone of responsive web design, allowing you to apply different styles based on various conditions, such as device screen size, resolution, and other features.
What Are Media Queries?
Media queries are CSS techniques used to conditionally apply styles to different media types or devices. They enable you to design a single website that looks and functions well on a variety of platforms.
Basic Syntax
The basic syntax of a media query consists of the @media rule followed by the media type and zero or more expressions that check for conditions of particular media features.
@media media-type and (media-feature-rule) {
/* CSS rules here */
}
Media Types
- all: Suitable for all devices.
- print: Intended for paged material and documents viewed on a screen in print preview mode.
- screen: Intended primarily for screens.
- speech: Intended for speech synthesizers.
Media Features
- width and height: Width and height of the viewport.
- device-width and device-height: Width and height of the rendering surface.
- orientation: Detects if the device is in landscape or portrait mode.
- resolution: Pixel density of the device.
- aspect-ratio: Width-to-height aspect ratio of the viewport.
- color: Number of bits per color component of the output device.
Logical Operators
- and: Combines multiple media features together into a single query.
- not: Negates the query.
- only: Restricts the style to the specific media type.
- , (comma): Combines multiple media queries into a single rule.
Best Practices
- Mobile-First Approach: Use
min-widthrather thanmax-widthto start with mobile styles and then scale up. - Breakpoints: Choose breakpoints based on content rather than devices.
- Readability: Keep media queries close to their relevant rules for better maintainability.
- Performance: Limit the number of media queries for better performance.
Media queries break points
When it comes to defining breakpoints in media queries, it's important to focus on the content rather than specific devices. However, there are some commonly used breakpoints that can serve as a good starting point.
-
Mobile Devices
- Small Mobile:
max-width: 320px - Medium Mobile:
max-width: 480px
- Small Mobile:
-
Tablets
- Small Tablets:
min-width: 481pxandmax-width: 768px - Large Tablets:
min-width: 769pxandmax-width: 1024px
- Small Tablets:
-
Desktops
- Small Desktops:
min-width: 1025pxandmax-width: 1280px - Large Desktops:
min-width: 1281pxandmax-width: 1440px
- Small Desktops:
-
4K and Large Screens
min-width: 1441pxandmax-width: 2560pxfor 2K displaysmin-width: 2561pxfor 4K displays and above
Importent points to consider
-
Content-First: Always test how the content adapts at different sizes and adjust breakpoints accordingly.
-
Mobile-First: Start with mobile styles as your default and use
min-widthqueries to build up to larger screens. -
Avoid Device-Specific Breakpoints: Device dimensions can change, so it's better to focus on how the content behaves.
-
Test Extensively: Always test on real devices and use browser dev tools to simulate different screen sizes.
-
Use Variables: If you're using pre-processors like SASS or LESS, you can store these breakpoints in variables for easier management.
Examples
Basic Example
@media screen and (max-width: 768px) {
body {
font-size: 16px;
}
}
Using Logical Operators
@media screen and (min-width: 768px) and (max-width: 1024px) {
/* Styles for tablets */
}
Multiple Queries
@media (max-width: 600px), (max-height: 400px) {
/* Styles for small screens or short viewports */
}
Orientation
@media (orientation: landscape) {
/* Styles for landscape orientation */
}