Make your site mobile friendly with media queries

“If you think responsive’s simple, I feel bad for you son. We got 99 viewports, but the iPhone’s just one.” -Josh Brewer, March 10, 2010.

For me, “responsive design” means creating the right experience for the right device. Media queries allow us to do that.

By using media queries, we can target CSS,  based on  particular screen sizes (or device-orientation or other property) and specifically style the content to display in a precise way. This can be quite a daunting feat though, not only because of the numerous devices out there, but because new devices of differing sizes will continue to be developed.

With this in mind, developers have begun to move away from using device-specific breakpoints in our CSS, rather, we’ve stared using content-specific breakpoints, meaning that we design media  queries for the specific project at hand. The size of the device doesn’t really matter as long as we’re using content focused breakpoints.

If you want to read more about device specific breakpoints, Justin Avery talks about the pitfalls of using them in this interesting article.

How to write a media query:

Media queries are “if” statements. For example, “if” this is true, then “do” this.

Media query syntax
Media queries begin with @media, followed by a media type” (ie: screen) and  “expressions” that check for certain conditions (ie: min-width).

The expression is inside parentheses; the value of the expression is the device “breakpoint”:

For example:
@media screen and (min-width: 1200px) { }

The CSS style that will be applied when these conditions are met, is going to be inside of the curly braces.

Media Types:

Here are some common media types to use in media queries:
all –All devices

print-Used for documents viewed on screen in print preview mode.

screen-Used primarily for smartphones.

Expressions:

Some examples:
width-The width of the current window
height-The height of the current window
device-width-The width of the device
device-height-The height of the device

So…..for example, if we want to make the background color to be red when viewed on a desktop computer, we’d write:

@media screen and (min-width: 1200px) {
.classname{
background-color: red;
}
}
The media query says that when the screen size is a minimum of 1200px (so…it has to be  1200px or more) that the background will be red. When viewed on a smaller screen (smaller than 1200px) the background will be whatever color is already styled in the regular CSS.

In the next example, an image will not display at all on any devices that are smaller than 991px wide (have a maximum width of 991px) but the image will display on wider screens (bigger than 991px):

@media(max-width:991px) {
.classname-img{
display:none;
}
}

A couple of things to keep in mind:
Mobile First Media Queries have become the standard for media queries. They make pages display faster on smaller devices, and Google actually takes this mobile responsiveness into consideration when ranking websites. (Since 2015, non-mobile friendly websites get ranked lower in Google search results)

IMPORTANT:
Using a “mobile first” media query means that small screen styles are already in the regular CSS and as the screen gets larger you override those styles with some new style, using “min-width”.

In the following Mobile First example, the background is red on smaller devices until the device width reaches 600px and larger; and then the background becomes green:

Regular CSS
.classname{
background-color: red;

}

This media query will change the background to green on larger devices (a minimum width of 600px):

@media (min-width: 600px) {

.classname{
background-color: green;
}
}

So, devices with a width of 600px and more will display a green background. The media query is not actually taking effect until the page is viewed on a larger screen. Get it?

A word about Desktop First media queries
With “desktop first” it’s the opposite approach: the large screen styles are in your regular screen CSS and as the screen gets smaller you override those styles with different ones, using “max-width”. I’m showing this as an example of what NOT to do:

Here, the background is red on larger screens, and  when the the screen width is smaller (less than 600px,) then the background becomes green:

Regular CSS
.classname {
background-color: red;
}

This media query will change the background to green on smaller devices:
@media (max-width: 600px) {
.classname{
background-color: green;
}
}

Now here’s a list of the typical breakpoints, because they’re still very useful:
/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {…}

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {…}

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {…}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {…}

/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {…}

And here’s a downloadable PDF of prewritten CSS for laptops, phones, tablets and even wear-ables. (from CSS-Tricks, Media Queries for Standard Devices by CHRIS COYIER, 2017

Enjoy! 🙂

Questions?

[email protected]

My Contact Page

Other articles you might like:

-Using images: Tips to improve your SEO rankings

-Use a customer thank-you page to avoid missed opportunities

-6 Tips to Make Your Blog Post SEO Friendly

 

About the author

diane-author-300x181 Make your site mobile friendly with media queries

I developed Image and Aspect because I believe that professionals need to have an impactful web presence. One that showcases their unique talents, skills, and abilities as well as their values and style. A presence that focuses on social engagement and connection.

I’m passionate about what I do; I like helping fellow humans, I like having all kinds of social connection with others, and I want to give back, to make the world a better place.

I do much of the designing and coding myself, and I also have a wonderful network of professionals that may contribute as well; photographers, copywriters, branding experts.

I love designing and coding beautiful, elegant and responsive web creations. I ALSO teach and help others who want to learn how to do it themselves.

‘Tips and Snips’ is my blog, and it’s full of information and inspiration to help transform any online persona from “meh” to AMAZING! Sign-up HERE to get blog posts right to your in-box every Friday! I write about Design, Marketing, Search Engine Optimization, Branding, Vlogging, Color Theory, HTML5, CSS3, Bootstrap, WordPress, Social Media…anything you’d want to know to get yourself noticed online.

Visit Image and Aspect to learn more about your web presence options

Diane M. Metcalf, M.S.

Read more

Summary
mobile Make your site mobile friendly with media queries
Article Name
How To Write A Media Query
Description
“Responsive design” means creating the right experience for the right device. Media queries are what allow us to do that.
Diane Metcalf
Image and Aspect
Imageandaspect.com