RWD Media Queries


What is a Media Query?

A media query is a special CSS method that was introduced in CSS3.

It employs the @media rule to add a set of CSS instructions when a specific condition is met.


Add a Breakpoint

In a previous part of this guide, we created a webpage with sections and columns. While it adapted to different screen sizes, it didn't appear well on smaller screens.

Media queries are useful for this. They allow us to set a point where specific parts of the design will change their behavior on each side of that point.


Apply a media query to set a breakpoint when the screen width reaches 768 pixels:


Always Design for Mobile First

Mobile First means creating a website for mobile devices first, before focusing on desktop or other gadgets. This approach helps the website load quickly on smaller screens.

This implies that we need to adjust our CSS code.

Instead of adjusting how things look when the screen is narrower than 768 pixels, we should make design changes when the screen is wider than 768 pixels. This approach prioritizes mobile devices.


Another Breakpoint

You can include as many breakpoints as you want.

We'll add a point where the design changes from tablets to mobile phones.


To achieve this, we include an additional media query at 600 pixels. We also introduce a new set of classes for devices that are larger than 600 pixels but smaller than 768 pixels.

It might seem strange to have two sets of the same classes, but this allows us to control how columns behave in HTML at different screen sizes.


Typical Device Breakpoints

There are many different screens and devices with various sizes, making it difficult to set specific points for each one. To simplify, you can focus on five groups:


Orientation: Portrait / Landscape

You can use media queries to adjust how a web page looks based on whether the browser is in a horizontal or vertical position.

You can use certain CSS styles that will work only when the width of the browser window is greater than its height. This is known as the "Landscape" orientation.


Hide Elements With Media Queries

Media queries are often used to hide things on various screen sizes.

I will be hidden on small screens.

Change Font Size With Media Queries

You can adjust the size of text in an element to fit different screen sizes by using media queries.

Variable Font Size.