Media queries can be placed anywhere in a CSS document, or even in a separate CSS document. The question is where to place them. I'll give you some general ideas of different approaches and then you can fit those approaches to your situation. The most obvious place to put a media query would be right next to the actual style itself. So for example, if we want to change the background color of the pre-header in a certain size of window and nothing else, we might place the media query directly after the style that creates this background to begin with.
So, I could place it right here. Here is a media query that is placed directly after the pre-header, and then all I have to do is create a new style. So, I'll grab the name of the original style, and paste it in, and I'll close my bracket, and then I just have to put in the extra code I need to change the background. So I'll say background: red. When I save that and load my page and then resize the page, you will see that when we get to that point, I am changing the background color.
However, if I am going to make a lot of media queries, it's going to be really confusing if I need to edit them later if I have to keep going up and down in my document and try to find them. So if you have a lot of media queries that apply to one specific size, it's a better idea to bundle them all together and place them either at the top or the bottom of your document. So let's say I want to change both the pre-header and some other elements. I can then place one set of media queries here and then just bundle the elements underneath.
So let's say I want to change the font-family of the body again. I'll save it, reload my browser to reload the media query, and now you see we are changing both the super header and the font at the same time. This is a strategy I personally use. I place all the styles from each query in a separate section at the bottom of the document. And you are going to ask, why do you place it at the bottom of the document? Well, the way I do this when I design responsive sites is, I first try to figure out, what is the most common way people are going to access this site? If the most common way is cell phones and small devices, then I write my main styles to fit that scenario.
If the most common way of accessing the site is going to be on a large screen, then I write my main styles to fit the large screen. And then I stack the media queries at the bottom, because this is a cascading style sheet, meaning the cascade goes from the top to the bottom. So I'm assuming that the person visiting the site will use the styles on top and only in the cases where they use a different type of device will they get to the bottom and read that style instead. You could choose to put the media queries on top of the page, but if the page loads slowly, you may experience weird situations where the media queries will make the content pop in and out as it gets loaded, and that looks really weird.
So my general suggestion is to place the media queries at the very bottom of the document. There is always also the option of placing media queries in a separate document, and I have seen a lot of people do this. They create an entirely separate style sheet that is only called if certain media query conditions apply. I think this is a really bad idea, and the reason for that is in those cases, you will then first load a regular style sheet and then the browser will load an entirely different style sheet and apply it on top of the regular one. If you are going to do that kind of approach, it's better to just put the styles directly into the main style sheet, and that way the browser doesn't have to load several different style sheets.
As you can see, there are many approaches to where to place media queries. I like to organize them at the bottom of the style sheet so that they are at the end of the cascade and organize them from the biggest screen to the smallest screen, but that's just one option. Play around and see what works best for your situation.
Get unlimited access to all courses for just $25/month.Become a member