CSS MQ - Examples


CSS Media Queries - More Examples

Here are a few more instances of how we can use media queries.

Media queries are a widely-used method to provide a customized design for various devices. To illustrate this with a straightforward example, we can adjust the background color for different devices using the 'width' and 'height' attributes.

Do you wonder why we use exactly 992px and 600px? They are what we call "typical breakpoints" for devices. You can read more about typical breakpoints in our Responsive Web Design Tutorial.


Media Queries For Menus

In this sample, we employ media queries to make a flexible navigation menu. It changes its appearance depending on the size of the screen.

Large screens:

Small screens:


Media Queries For Columns

Media queries are often used to make layouts that work well on different screen sizes. Here's an example where we create a layout with columns that change their width depending on the screen size:

Large screens:

 

Medium screens:

 

Small screens:

Tip: A more modern way of creating column layouts, is to use CSS Flexbox (see example below). However, it is not supported in Internet Explorer 10 and earlier versions. If you require IE6-10 support, use floats (as shown above).


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 text size of an element for various screen sizes using media queries.

Variable Font Size.


Flexible Image Gallery

In this demonstration, we employ media queries alongside flexbox to design a dynamic picture collection.


Flexible Website

In this example, we combine media queries and flexbox to make a website that adjusts nicely to different screen sizes. This website has a navigation bar and content that can stretch and shrink as needed.


Orientation: Portrait / Landscape

Media queries can adjust how a webpage looks based on whether the browser is in portrait or landscape mode.

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


Min Width to Max Width

You can set the smallest and largest width using these codes: (max-width: ..) and (min-width: ..).

For example, When the size of the browser window is between 600 and 900 pixels wide, modify how a element looks:

Adding more conditions: In the following example, we include an extra condition in our existing set of conditions by using a comma. Think of it like an 'OR' operator.