CSS Media Queries
Create responsive designs with CSS media queries
Introduction to Media Queries
Media queries allow you to apply CSS styles based on device characteristics such as screen width, height, orientation, and more. They are a fundamental tool for creating responsive designs that adapt to different devices and viewport sizes.
Common media types include screen
, print
, and all
. Media features include width
, height
, orientation
, resolution
, and more.
Basic Media Query Example
This box changes color based on your viewport width. Resize your browser window to see the effect.
Viewport Width Demo
Current color indicates viewport width range:
- Red: < 576px (Extra small)
- Orange: 577px - 768px (Small)
- Green: 769px - 992px (Medium)
- Blue: > 993px (Large)
Responsive Layout
Media queries are often used with flexible layouts (like Flexbox or Grid) to create responsive designs. This example uses Flexbox with flex-wrap
to create a responsive layout.
Responsive Grid
This example shows how to create a responsive grid layout using CSS Grid and media queries. The number of columns changes based on the viewport width.
Responsive Typography
You can create responsive typography using viewport units (vw, vh) or with media queries. This example uses a combination of both approaches.
This heading uses viewport units
This paragraph also scales with the viewport size. Resize your browser to see how the text size adjusts automatically. This approach creates a smooth scaling effect without breakpoints.
Orientation Media Query
Media queries can detect the orientation of the device (portrait or landscape). This is particularly useful for mobile devices.
Orientation Demo
This box changes color based on your device orientation.
Hover Capability Detection
The hover
media feature detects whether the user's primary input mechanism can hover over elements. This is useful for creating different experiences for touch devices vs. mouse-based devices.
Hover Capability Demo
Dark Mode Detection
The prefers-color-scheme
media feature detects if the user has requested a light or dark color theme. This allows you to provide a dark mode version of your site.
Dark Mode Preference Demo
This box adapts to your system's color scheme preference.
Reduced Motion Preference
The prefers-reduced-motion
media feature detects if the user has requested minimized animations. This is an important accessibility feature for users who experience motion sickness or distractions from animations.
Motion Preference Demo
This box animates unless you've enabled reduced motion in your system settings.
Responsive Navigation
A common use case for media queries is creating responsive navigation menus that transform into mobile-friendly versions on smaller screens.
Container Queries (Simulated)
Container queries allow you to style elements based on the size of their container rather than the viewport. This example simulates container queries using JavaScript and regular media queries.
Small Container
Medium Container
Large Container
Print Media Query
The print
media type allows you to define styles specifically for when the page is printed. Try using your browser's print preview to see how this page would look when printed.
Print-Optimized Content
This box will change appearance when printed.
Media Query Best Practices
- Mobile-First Approach: Start with styles for mobile devices and then add media queries for larger screens using
min-width
. - Use Logical Operators: Combine media features with
and
,not
, andonly
for more specific targeting. - Common Breakpoints: While you should design for your content, common breakpoints are around 576px, 768px, 992px, and 1200px.
- Test on Real Devices: Always test your responsive designs on actual devices, not just browser resizing.
- Accessibility: Use media queries like
prefers-reduced-motion
andprefers-color-scheme
to enhance accessibility. - Performance: Be mindful of the CSS you load on mobile devices. Consider using media queries to load lighter resources on smaller screens.