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

@media (condition) {
    /* CSS rules */
}

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:

/* Base styles (mobile) */
.grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

/* Tablet (768px and up) */
@media (min-width: 768px) {
    .grid {
        grid-template-columns: 1fr 1fr;
    }
}

/* Desktop (1024px and up) */
@media (min-width: 1024px) {
    .grid {
        grid-template-columns: 1fr 1fr 1fr;
    }
}

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.

/* AND - both conditions must be true */
@media (min-width: 768px) and (orientation: landscape) {
    /* Tablet in landscape */
}

/* OR - either condition */
@media (max-width: 600px), (orientation: portrait) {
    /* Small screen OR portrait orientation */
}

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:

rem Inject responsive CSS
css$ = "@media (min-width: 600px) {"
css$ = css$ + "  .my-window { grid-template-columns: auto 1fr auto 1fr; }"
css$ = css$ + "}"
css$ = css$ + "@media (min-width: 900px) {"
css$ = css$ + "  .my-window { grid-template-columns: repeat(3, auto 1fr); }"
css$ = css$ + "}"

web! = BBjAPI().getWebManager()
web!.injectStyle(css$)

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

DWC FAQ

Taking an App From GUI to BUI and to DWC

Dynamic Web Client (DWC) Overview

BASIS Web Clients