Skip to main content

CSS Media Queries Examples

Delivering different styles to different devices is a popular job for media queries. CSS rules can then depend on device characteristics, viewport width among them.

Example

The background color here changes as the viewport width changes.

Prompt
Create a page that uses media queries to change the body background color at different viewport widths: olive for mobile, blue at 768px, and tan at 992px. Include white text for the first two and black text for the last.
Output: complete HTML page

Media Queries For Columns

Flexible layouts are another common use. Depending on the screen size, the layout here moves between four columns, two columns and full-width columns.

The CSS Flexbox chapter explains the Flexible Box Layout Module in more depth.

For more on Responsive Web Design, see the Responsive Web Design Tutorial.

Example

Prompt
Create a page with four equal-width columns using flexbox. Use media queries so the columns become two per row at 992px or less, and stack into a single column at 600px or less. Each column should have some sample text content.
Output: complete HTML page

Media Queries For Menus

A navigation menu can become responsive too, with a design that adapts to each screen size through media queries.

Example

Prompt
Create a horizontal navigation menu using an unordered list and flexbox. Style the links with white text on a dark background (#333). Use a media query to stack the menu items vertically when the viewport is 600px or less.
Output: complete HTML page

Hide Elements With Media Queries

Small screens may not need every element, and a media query can hide the ones they don't.

Example

Prompt
Create a page with a styled div element that is hidden when the viewport width is 600px or less, using a media query with display: none.
Output: complete HTML page

Change Font Size With Media Queries

An element's font size can also follow the viewport width, through a media query.

Example

Prompt
Create a page with a div element whose font size changes to 80px when the viewport width is 600px or more, using a media query with min-width.
Output: complete HTML page

Media Queries for Screen Orientation

Screen orientation is another condition a media query can respond to, adjusting the page layout accordingly.

Example

While the screen is in landscape orientation, the body gets a different background color.

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

Media Queries for User Preferences

Animation can be a problem for people with motion sensitivity, who prefer websites that have less of it.

To find out whether a user has asked for reduced motion, such as fewer animations or transitions, check the prefers-reduced-motion media feature. When that setting is active on their computer, you can turn animations off.

Example

Prompt
Create a page that uses the prefers-reduced-motion media feature to disable all animations and transitions when the user has requested reduced motion.
Output: complete HTML page

Responsive Image Gallery

Combining flexbox with media queries gives you a responsive image gallery.

Example

Prompt
Create a responsive image gallery using flexbox and media queries. Display four images per row on large screens, two per row on medium screens (below 800px), and one per row on small screens (below 600px). Use placeholder images.
Output: complete HTML page

Responsive Website

The same combination of flexbox and media queries can lay out a whole responsive website.

Example

Prompt
Create a responsive website layout with a header, navigation bar, main content area with a side column, and a footer. Use flexbox and media queries so the layout stacks vertically on small screens (below 600px).
Output: complete HTML page