Let’s talk about CSS media queries in this web development tutorial. And if you’ve never heard of what a media query is, that’s perfectly fine. We’re going to start from the ground up. A media query is a tool in CSS that allows for us to implement responsive design elements. When I say responsive, what I mean is what I’m going to show you right here. I have two nearly identical sites. One is responses and uses media queries, and the other one is not. We’re going to walk through, after this demo, we’re going to walk through the code and we’re going to implement a full media query for this homepage. Let’s first look at a media query based site. If I open this up, what I can do is it looks good on desktop and then if I were to access this on a mobile device, which I can mimic by just bringing this down here, you can see that the entire site readjusts. This is what someone coming on an Android or an iPhone would see. You notice how we have the logo has readjusted. It’s now at the top. And then we have the navigation elements are stacked on top of each other. And then the same thing with the contact information.
Then everything else has also readjusted. This is looking really good. This is the kind of experience you’d want to see on a mobile device. Now, if you open up a site that does not have the media query though, and then try to perform the same action, you’re going to get very different behavior. Notice now, if I take this down to the same size, that it does not readjust. Someone accessing the site’s going to load it up and they’re going to see this distorted looking page. They’re going to have to scroll to the right. A bunch of things are kind of shrunk in and overlapping. This is really not a good experience. This is the reason why media queries are so powerful. Now that we have a good idea on what they are, let’s walk through how we can implement them.
I have this page open and I have the code open for it right here. I’m going to start by creating a new style sheet here called media queries. This isn’t necessary, this is just because I want to be able to have all my media queries in one spot. It will also be easy for us to see them. Let me open that up and in the styles directory, I’m going to save a file called media queties.css. Then here, we’re going to be able to add all of those. Now, the syntax for using a media query is … it looks a little bit different if you’ve never used it before. It starts with a @ symbol. Then you say media. Then this is a method or a function. That means it takes an argument. The very first thing that we have to provide is the breakpoint. If I say medium max-width. Then I’m going to use 615 pixels. What this is going to do is this is going to say that whenever we have a screen that is below 615 pixels wide, which is a pretty standard size for using with smartphone devices, then I want you to apply these styles.
Now, whenever you’re using media queries, a trick to make sure that you are following is your media queries should be at the very end. They should be the last styles that you include. The reason for that is because if you have media queries and then you call other styles after that, they will override it. You need to make sure you call that at the very end. What this is going to do is the browser’s going to look, it’s going see all of these media queries, and it’s going to check and say, “Okay, these are the styles I am going to apply, and I’m going to run these if the screen is 615 pixels or less.” You could do this for any of them. Let’s just test this out.
If you look at the index here in the navigation wrapper, you can see that we have navigation wrapper and then we have all of these items inside of it. We have a left column, a center column div, and then a right column. I think this is going to be a great place to start. If I say navigation-wrapper. Then inside of here, I’m going to change the flex direction. Instead of having the default row, I’m going to change it to column. Then I also want to update the height so it’s 100%. Let’s see what this does for us on our site that previously was not working on mobile. Let me open this up in a new browser window. Now, if I shrink this down, you can see that when we hit that breakpoint, you see how this got readjusted. Before, that was not working. Before, it just simply went to the side and then it didn’t shrink down and it didn’t stack it on top of each other like that.
That means that our media query is firing and it’s working. Now, we still have some work to do, because this doesn’t look very nice. We still have to adjust some of the other items. I don’t want the phone number here at the top. I want to have the logo at the top. We’re going to be able to leverage Flexbox in order to make that possible. I’m just going to take this down to about right here, about this size. Now, when we switch back, we’re just going to be able to look at that. I’m also going to keep the code open at the same time, so that we can save some time and just look at it simultaneously. Okay, now that we have our navigation wrapper updated, I want to talk about a very powerful tool inside of Flexbox that you may or may not be aware of.
Flexbox gives you the ability, not simply to line out and align your items in a really nice and easy manner, it also gives you the ability to change the order. That is how we are going to readjust the order that these elements are shown. This is one of my favorite parts about Flexbox. Right here, if you remember we have our left column, our center column, and then our right column. What I can do is say navigation wrapper. Then I want to grab the left column, just like this. Then inside of here, I want to change the order to two. That’s going to allow us to change the default order, because usually the order is simply the order that it is declared in the HTML. But what we can do is we can actually override that using Flexbox. I’m also going to add some margin top and some margin bottom. Let me do 10 pixels on the top and then margin bottom, I’m going to go with, let’s say 15 pixels.
That’s all we need the left column. Now, for the center column, remember the center column is the logo. The … if you want to take a look at it … This is the final site, but this also is what we started with. Here you can see that the left column is the phone number and the hours. The center column is the nav component and logo. Then the third column is this address here. That’s what we’re looking to adjust. I want to take the center column and I’m going to change the order here so that the order’s going to be one. Then I also want to change the width. The width here is going to be 100%. Then lastly, we want to grab the right column and, as you may have guessed, since we used two on the left column, for the right column, we want to use three here. Then I’m going to also just add some margin on the top. Here I’m going to say 15 pixels.
Now, this isn’t going to be perfect yet, because we still have those links that we need to fix, but this might get us a little bit closer. Let’s see. Hit refresh. You can see that is looking much better. Do you see how we have this logo now that is at the very top? Our images, or I’m sorry, our links here, that these need to get fixed. That’s what’s pushing everything over. But we can see that everything now is in the correct order. That is looking really nice. As you can see, all we have to do is because we leverage Flexbox, we’re able to alter that order and control exactly how we wanted the page to be rearranged. Then let’s switch over to the index HTML page. Let’s see, we have a class of links wrapper. Then a nav link inside of each of those.
That’s going to be the next class that we’re going to add to our media query here. Now, what we can do is we’ll say links wrapper. Then here inside of it, if you want … if you’re using Flexbox and you want to change the order so it’s no longer is showing from left to right, but instead it goes from top to bottom, you can simply change the flex direction. We’re going to change that to column. Then I’m also going to add some margin to the bottom. For that, we’ll go with 20 pixels. Then let’s also update the nav link, but before we do that, let’s just see where we’re at. If I hit refresh that is looking much better. See, that this is exactly what we’re looking to do. That is how we’re able to leverage media queries to be able to adjust dynamically how the page is laid out.
I’m really liking that. Now, let’s go to our links wrapper. We’re going to select all of the nav links, the nav link class. Inside of here, I’m going to just add … They’re stuck a little bit close together. That would be hard for someone on mobile to click the right button. Let’s add some margin to the top and bottom. Then from there, we’re going to update the font size. Let’s go with 1.5em. Then lastly, we’re going to adjust the width to be 100%. Now, if you hit save, come back, hit refresh, there you go. Now, what we’ve done is we’ve completely changed the layout of this nav bar so that it matches exactly what we’re wanting on a mobile device. Imagine you are building out this website and you’re building it for a restaurant, just like we have here, and someone clicks on the link from Yelp. They are going to want to see something that looks like what we have here, not have distorted site where they don’t even know where to click or anything like that. They want to have something that really fits with that mobile user experience.
We’re able to leverage media queries in order to do that. Let’s just review, really quick, what we’ve done. The syntax, once again, for a media query is using the @ symbol, the work media. Then defining the width. You can have multiple media queries. You could have a media query that is for smartphones. Then you can adjust and have a media query that is for iPads or tablets. You could have as many media queries as you want. Then the page would dynamically change. This is almost kind of like a conditional. You can think of this as being a way that you can tell the browser that these are different rules that you want it to follow whenever the user is on a different screen size. Then from there, you simply have to call that in the HTML, just like we did right up here and it’s going to work and it’s going to readjust.
Then lastly, using tools like CSS Grid and Flexbox give you the ability to control how you want the layout to change. If you weren’t using a tool like that, it’d be much harder to change the order, like we have right here. But because you have those kinds of tools in place, you can simply readjust the entire page based on the screen size.