How to Use Media Queries to Build a Responsive Web Site

DISCLAIMER: This post may contain “affiliate” links to products and services I recommend. I’ll receive a small commission if you decide to purchase one of these products or services. I only recommend products I genuinely believe will help you in running your freelance business.

Media queries are the cornerstone of responsive web design. They are what make the kind of advanced responsive design we see today even possible.

So, here’s how to master using them…

How to Use Media Queries

So, What ARE Media Queries?

Here’s the official definition from the Mozilla Developer Network:

Media queries consist of a media type and can, as of the CSS3 specification, contain one or more expressions, expressed as media features, which resolve to either true or false.

Huh?

Think of media queries as conditional statements. You create a query that either resolves as true or false… and if TRUE, you designate certain CSS that then gets applied.

Here’s an example snippet:

@media screen and (max-width: 600px) {
  .facet_sidebar {
    display: none;
  }
}

The above query says:

If the media type equals screen and the width is 600px or less… then apply these styles.

The “screen” part is the media type and can be things like “all”, “screen”, “print”, “tv”, etc ( Click here for a list of available media types).

The “max-width: 600px” part is the “expression expressed as a media feature” and can be things like max-width, max-device-width, orientation, etc (Click here for list of available media features).

And, the above statement will also resolve to true or false.

This is what allows us to target certain screen widths and change the way our site behaves based on that screen size. Essentially, this is responsive web design.

Finally, you have logical operators that help you “chain together” multiple expressions to target certain scenarios more specifically. Here’s an example snippet:

@media (min-width: 700px) and (orientation: landscape) { ... }

Here’s you’ll notice the word “and” sandwiched between two expressions. This query states:

The screen must be at least 700px wide and the orientation of the device must be landscape.

You can use different logical operators like: and, comma-separated lists (or), not, etc (Click here for a list of available logical operators).

Where Do I Start?

At this point, you’re probably wondering where to start with all this. The most common way media queries are used is to target certain devices widths in order to change the display of a web site for different devices.

Here’s a common set of media queries (taken from Bootstrap 3.0):

@media (min-width: 768px) and (max-width: 979px) {}
@media (max-width: 767px) {}
@media (min-width: 1200px) {}

Here we’re targeting three different widths:

  • BETWEEN 768px and 979px
  • LESS THAN 767px
  • MORE THAN 1200px

Now, you might be wondering WHY these widths?

In my Responsive Web Design 101 course, I cover that particular question in great detail and show how you exactly how to structure your media queries to target different devices…

But, in a nutshell, these widths are based on common device widths. A number of devices (included iPads) have a width of 768px in the portrait orientation… thus the focus on widths around 768 pixels.

Also, common laptop widths are 1280px and 1366px… so the min-width:1200px is target those devices and anything bigger… while excluding iPads in the landscape position (1024px).

Why the 979px?

Well, 980px became a design standard on the web. So, designers (and web users for that matter) have gotten used to using that width. So,  you’ll often see media queries based around that width, as well.

All in all… media queries are about targeting certain groups of devices based on their widths. This allows us to create different designs for different devices and make our site look and function good no matter the device viewing it.

QUESTION: How do you use media queries in  your responsive designs? Do you have a standard set of queries that you use?

You might also like

Do you want more freelance clients?

I’ll show you what I learned over the last 15 years to grind out (from absolute scratch) a backlog of new clients wanting to hire you. Who your best client prospect are, what services you should be offering them, where to find them and more. Just enter your email address in the box below and let’s get started:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you want more freelance clients?

Enter your email below to get started building your system for consistently bringing in new freelance clients:

WHAT OTHERS ARE SAYING

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Close Menu