Skip to main content

CSS Media Queries

Responsive web pages rely on media queries, which apply styles according to the characteristics of the device or environment showing the page.

They are added to a style sheet through the CSS @media rule.


Use Mediaqueries to Add a Breakpoint

The previous page built a grid layout web page. It now needs a breakpoint, so that a media query can rearrange its grid items on bigger screens.

Example

The media query in this example sets a breakpoint at 600px:

Prompt
Create a responsive grid layout page with a header, menu, content, facts sidebar, and footer. By default (mobile), all items stack in a single column. Add a media query at 600px min-width that rearranges items into a 6-column grid: header spans all 6, menu spans 1, content spans 4, facts spans 1, footer spans all 6.
Output: complete HTML page

Another Breakpoint

Breakpoints are not limited in number, so add as many as you like.

Example

These media queries add two breakpoints, one for screens of at least 600px and one for screens of at least 768px:

Prompt
Create a responsive grid layout with header, menu, content, facts, and footer sections. Use two media query breakpoints: at 600px, rearrange into a grid where menu is 1 column, content is 4 columns, facts goes to its own row spanning all 6; at 768px, move facts next to content (1 column) so menu, content, and facts share the same row.
Output: complete HTML page

Typical Device Breakpoints

Screens and devices come in so many heights and widths that an exact breakpoint for each one is hard to create. Targeting five common groups keeps things simple.

Example

Prompt
Create a page that demonstrates typical responsive breakpoints by changing the background color of the body at five common widths: 600px and down (extra small), 600px and up (small), 768px and up (medium), 992px and up (large), and 1200px and up (extra large). Display text indicating the current size range.
Output: complete HTML page

Media Queries for Screen Orientation

A page's layout can also respond to the orientation of the screen through media queries.

In landscape mode, the background color of the body changes in this example:

Example

Prompt
Create a page that uses a media query to change the background color of the body to lightblue when the screen orientation is landscape mode.
Output: complete HTML page

Hide Elements With Media Queries

On small screens, the media query below hides an element from view:

Example

Prompt
Create a page with a div element containing the text "I will be hidden on small screens." Use a media query to hide this element when the viewport width is 600px or less.
Output: complete HTML page

Change Font Size With Media Queries

As the viewport width changes, the media query below adjusts the font size of an element:

Example

Prompt
Create a page with a div element containing some text. Use a media query to change the font size of that element to 80px when the viewport width is 600px or more.
Output: complete HTML page

Media Queries for User Preferences

People with motion sensitivity tend to prefer websites with less animation.

Whether a user has asked for reduced motion, such as fewer animations or transitions, can be checked with the prefers-reduced-motion media feature. If that setting is active on their computer, this feature lets you turn animations and transitions off.

Example

Prompt
Create a page with an animated element (e.g., a box that moves across the screen). Add a prefers-reduced-motion media query that disables all animations and transitions when the user has requested reduced motion in their system settings.
Output: complete HTML page