DWC: Media Queries
Overview
Media queries are a CSS feature that allows styles to be applied based on characteristics of the device or viewport. They are commonly used in responsive design to adapt an application's layout and presentation to different screen conditions.
This page provides a short introduction to media queries. For more detailed information, see: Using Media Queries on MDN.
Basic Syntax
|
Common Media Features
Media queries can evaluate different characteristics of the viewport or user environment to determine when specific CSS rules should apply. Common media features include viewport width, orientation, and the user's preferred color scheme.
The following table summarizes several commonly used media features and example query syntax.
| Feature | Description | Example |
|---|---|---|
| min-width | Minimum viewport width | @media (min-width: 768px) |
| max-width | Maximum viewport width | @media (max-width: 600px) |
| orientation | Portrait or landscape | @media (orientation: landscape) |
| prefers-color-scheme | Light or dark mode preference | @media (prefers-color-scheme: dark) |
Breakpoint Strategy
Mobile-First Approach:
A mobile-first approach defines the base layout for smaller viewports first, then uses min-width media queries to add or adjust styles as more screen space becomes available.
Start with mobile styles, then add complexity for larger screens:
|
Common Breakpoints
| SIZE | WIDTH | DEVICES |
|---|---|---|
| Small | < 576px | Phones (portrait) |
| Medium | 576px - 768px | Phones (landscape), small tablets |
| Large | 768px - 1024px | Tablets |
| XL | 1024px - 1200px | Laptops, desktops |
| XXL | > 1200px | Large desktops |
Combining Conditions
Media queries can combine multiple conditions to control when a group of CSS rules applies. Use and when all conditions must be true, or separate media queries with a comma when either condition can match.
|
Example in BBj
Responsive CSS can be created in BBj and injected directly into the current DWC web page. BBjAPI::getWebManager returns the BBjWebManager, and BBjWebManager::injectStyle injects the generated CSS into the page.
The following example defines responsive styles at 600px and 900px breakpoints, then injects them into the DWC application:
|
Testing Media Queries
Media queries can be tested by changing the viewport size in the browser's Developer Tools and observing how the responsive layout changes as each breakpoint is reached.
In Browser Developer Tools
-
Open Developer Tools (F12)
-
Click the device emulation icon
-
Select "Responsive" mode
-
Drag handles to resize viewport
-
Watch layout change at breakpoints
The responsive device toolbar allows different viewport widths to be tested without resizing the entire browser window.
The following image shows the browser's responsive device mode used to test media-query breakpoints.
See Also
Taking an App From GUI to BUI and to DWC