CSS media queries let a webpage respond to screen width, orientation, and other device features. Set breakpoints to adjust layouts, spacing, or text for phones and larger screens, so content stays clear and easy to use. A few well-chosen rules can make one page feel at home on many devices.

Multiple Choice

How can you set different styles for different screen sizes in CSS?

Using media queries is the method in CSS that allows you to apply different styles based on the characteristics of the device or screen size. Media queries enable responsive design, which means that the layout and styling of a webpage can change dynamically depending on the screen width, height, orientation, and other media features. When you define a media query in your CSS, you can specify a set of styles that should only be applied when certain conditions are met. For example, you can target devices that have a maximum width of 600 pixels and provide specific styling, ensuring that the content is optimized for smaller screens like smartphones. This helps improve the user experience across a range of devices by ensuring content is accessible and visually appealing, regardless of how it's being viewed. The other methods in the choices provided do not effectively achieve responsive designs tailored to different screen sizes. Inline styles apply only to individual HTML elements, lacking any adaptable response to screen changes. The font-size property affects text size but does not influence layout or other style aspects based on the screen size. Defining multiple CSS files could separate styles but would require additional effort in managing which file to use based on the screen size, making it a less efficient solution compared to media queries.

A webpage can look polished on a wide monitor and still feel cramped on a phone. The reason is simple: screens come in many sizes, and a layout that works well on one may not suit another. CSS media queries let a page respond to those differences, changing its styles when a screen meets certain conditions.

That’s one of the foundations of responsive design. Instead of building a separate website for every device, developers can use one set of HTML and CSS that adjusts as the viewing space changes. A navigation bar might shrink, columns might stack, and text may become easier to read on a narrow screen—all without changing the page’s content.

The CSS feature that responds to a screen

A media query is a conditional rule in CSS. It checks a feature of the device or viewing area, such as the viewport’s width, and applies styles only when that condition is true.

Here’s a basic example:


.card-grid {

display: grid;

grid-template-columns: repeat(3, 1fr);

gap: 1rem;

}

@media (max-width: 600px) {

.card-grid {

grid-template-columns: 1fr;

}

}

On a wider screen, the cards appear in three columns. When the viewport is 600 pixels wide or less, they stack in a single column. The HTML stays the same; only the styling changes.

That small shift can make a big difference. A three-column layout may be easy to scan on a laptop but awkward on a phone, where each card would be squeezed into a narrow strip. A media query gives the layout a clear point at which to change.

What does “screen size” mean in CSS?

When people talk about a page’s size in responsive design, they often mean the viewport: the area where the webpage is displayed. It’s not always the same as the device’s full physical screen. A browser window can be resized, and a phone can display a page at a viewport width that depends on its settings and orientation.

Media queries can check several features. Width is common, but CSS can also respond to height, orientation, and user preferences. For example, a style can apply when a device is in landscape orientation, or when someone has asked their system to reduce motion.


@media (orientation: landscape) {

.hero {

min-height: 60vh;

}

}

This rule changes the hero section when the viewport is wider than it is tall. It doesn’t identify a particular phone or tablet. Instead, it responds to the viewing conditions, which is usually more reliable: devices change, but the space available to the page is what shapes the layout.

Choosing breakpoints without chasing devices

A breakpoint is the point where a design changes to better fit the available space. In a media query, it’s often written as a width condition, such as max-width or min-width.


@media (min-width: 800px) {

.content {

grid-template-columns: 2fr 1fr;

}

}

This rule applies when the viewport is at least 800 pixels wide. The content can then use a main column and a narrower sidebar. On smaller screens, the columns can remain stacked.

It’s tempting to choose breakpoints based on familiar device labels—phone, tablet, laptop—but those categories don’t tell the whole story. A small laptop window may be narrower than a tablet held sideways. A large phone may have more room than an older model. A better approach is to let the content guide the decision: when a line gets too long, a menu feels crowded, or cards become cramped, that’s a useful place to adjust the layout.

There’s no universal set of perfect breakpoint values. A design with simple text and one image may need only one change. A busy dashboard may need several. The goal isn’t to add as many media queries as possible; it’s to make the page work well wherever it’s viewed.

Media queries and the rest of CSS

Media queries don’t replace CSS properties such as font-size, margin, or display. They control when a group of styles applies. Inside the query, developers can change nearly any ordinary style property.

For example, a heading could use a fluid size that grows as the viewport gets wider:


h1 {

font-size: clamp(2rem, 5vw, 4rem);

}

Here, clamp() sets a minimum, a preferred flexible value, and a maximum. That can smooth out changes between screen sizes. A media query is still useful when the design needs a clear shift—say, when a horizontal navigation menu becomes a compact menu button. Fluid sizing and media queries often work well together.

Inline styles are different. They attach styles directly to an HTML element, and they’re useful for certain specific cases, but they aren’t a good tool for responsive layouts. An inline style doesn’t automatically change when the viewport changes. CSS media queries, by contrast, can apply a coordinated set of rules when conditions are met.

It’s also possible to place styles in more than one CSS file. But using separate files alone doesn’t make a page responsive; something still needs to decide when each set of styles should apply. Media queries provide that conditional logic, and they can appear in a stylesheet or in a linked stylesheet’s media attribute.

A few details that make responsive CSS easier

Most responsive pages follow a mobile-first approach. That means the base styles support a narrow layout, and min-width queries add or adjust styles as more space becomes available.


/* Base styles: a single-column layout */

.layout {

display: grid;

grid-template-columns: 1fr;

}

@media (min-width: 900px) {

.layout {

grid-template-columns: 2fr 1fr;

}

}

This approach keeps the starting point simple and avoids writing a wide-screen design first, then undoing it for small screens. Still, there’s no rule that every project must be mobile-first. The right structure depends on the content and the design.

It helps to keep related changes together, too. If one breakpoint changes the page’s columns, navigation, and spacing, group those rules in a readable way. Clear CSS is easier to maintain than a long list of scattered adjustments—especially when a design grows over time.

Finally, test the layout at different widths, not just at a handful of device presets. Dragging a browser window narrower can reveal awkward points between common screen sizes. Look for horizontal scrolling, clipped text, buttons that are hard to tap, and layouts that feel unbalanced. Responsive design is about more than making everything fit; it’s about keeping the page comfortable to use.

One page, many viewing conditions

Media queries give CSS a way to respond to the screen and other device features. They can reshape a layout, adjust spacing, or change how components appear when the available space shifts. Used thoughtfully, they help one webpage feel at home on a phone, a tablet, or a desktop monitor.

The key idea is straightforward: write styles for the content, then use conditions to adapt those styles when the viewing environment calls for a change. A little flexibility in the CSS can spare visitors a lot of squinting and sideways scrolling.