Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Media queries allow you to determine which styles are applied to pages based on specific media properties, such as screen width, color, or resolution. Now, if you're familiar with using the media attribute to apply styles for screen devices, print, projection, that sort of thing, then media queries are going to feel very natural to you, because they're just an extension of existing media capabilities. So to kind of show you what media queries are capable of, I have the media-queries.htm file open from the 07_02 folder. And I want to preview this page in the browser real quick, so we can do a quick rundown of the syntax before we actually start using them.
So the basic syntax you can see in the first block of code here, basically after whatever media declaration you are doing, you can put before it, you can put two keywords, optional keywords: not or only. Not will negate everything that comes after it, so it's sort of testing to see what isn't true, and then only is designed basically just to hide the media query from any older user agents that don't support them. Then after that, you have the media type, like you would normally, so screen, projection, print, all of those that you are probably pretty familiar with using. And then after that, we have an optional and keyword, which is followed by an expression, and that's where the real power of media queries comes in.
I am going to scroll down just a little bit. You can see, for example, in the second code block we have a sample media query, and this one is saying, for only screen devices that have a minimum width of 720 pixels, so 720 pixels and above, go ahead and apply these styles. So we are basically focusing exactly when and where our styles are applied. Now, below that I have a listing of all the different media features that you can test for, things like width, height, device-width, and device-height, orientation, device-aspect-ratio, color, so there's a lot of things that we can test for when we're choosing when to apply our styles or not.
Anything that's a length value, like width or height, you can also expand what you are doing by adding a minimum and a maximum prefix to it. All right! I am going to go back into our code, and we are going to experiment with media queries by writing one of our own. So I am going to scroll down. And because we are using embedded styles and we are not linking out to an external style sheet, we are going to use a different form of syntax than what you saw there previewed on the page. We are going to use what's known as a @media block. So I am going to do @media. Then I am going to open up a curly brace and then have the closing curly brace.
Now, essentially, this is a way of grouping styles within a larger style sheet. So anything that goes inside that media block is governed by the media query that's going to follow this. So right after @media, I am going to go ahead and do my media query. I am going to type in only screen, and then in parentheses max-width, and then a colon, of 920 pixels, so you don't need a semicolon after that, just the 920 pixels. So this is saying that, hey, for just screen devices, any screen device that has up to a maximum width of 920 pixels, I want you to apply these styles that follow.
So any styles in those curly braces that follow that are going to be applied if they meet this criteria. So we are basically saying 920 and below is when this is going to apply. So we are going to go down into our @media block, and this is where it might seem a little bit strange if you hadn't seen the syntax before. Inside this I can just start writing additional rules. So I am going to type in body, and we will do width of 90%. So I am going to go ahead and save that. Now, I know that looks a little weird to have a closing curly brace here and a closing curly brace here, but you've have got to remember that this is a big containing media block. And you want to be very careful when you're writing these, because you don't want to leave off a curly brace.
That's one of the reasons why, when I created that media block, I went ahead and created the curly braces first before I begin authoring any styles inside of it. So I have gone ahead and saved that. I am going to go back out to my browser and refresh. Now, right off the bat, we don't notice any difference, because currently my screen is above 920 pixels, but notice that as I begin to resize it, when I hit that trigger point, you can see when I do it. As soon as I hit that trigger point, the body width changes and I have a flexible layout below a certain screen size, so you can see exactly when that trigger is happening. And what's nice about this is as our screen size gets smaller, the layout is going to respond to it, but of course it still breaks at a smaller size.
That's one of the great things about media queries. You can have media queries that target multiple conditions. Let's go back in and modify our existing media query and then write another one so that we are targeting three totally different situations. So I am going to go back into my code. And the existing media query, I am going to add some constraints to this. So essentially, right after the first and, I am going type in min-width this time. So not max-width, but you can also do min-width. Now, I am going to do a min-width of 600 pixels, and then between those two I am going to do another and. So you can just chain as many of these as you would like.
So essentially what we are doing here is we are setting a range. We are saying, hey, anytime that the screen value is between 600 pixels and 920 pixels, make the body width 90%. That means that when this condition isn't true the default styles will go ahead and apply. So I am just going to do @media, and I am going to type in only screen and max-width of 599 pixels. And if you look at the media query that we have above this, it's pretty obvious why I'm doing that, because as soon as the last condition has been met, which is min-width of 600 pixels, 599 pixels picks it up from that exact moment.
So we are creating a media query that's going to be active the moment the one above it is not, in terms of screen size. And then inside that I'm going to do a body selector, and the body selector, I am going to go ahead and set a width on that, and I am going to do a width of 320 pixels. I also want to do one more selector, and that's going to be a pre selector. The pre is the element that's representing our code snippets on our page. And so for that I am going to take the font-size and I am going to reduce the font-size to .8ems, so I am going to make it a little bit smaller. I am going to give it some padding, so I am going to apply a padding of .5ems to it.
And then I want to change the background color, just because we can. And I am going to do rgb(246, 247, 160), and don't forget your semicolon there. All right! So I am going to save this, go back out to my browser, and refresh, and now we have three different conditions. When the browser is above 920 pixels, we see the page in its default state. As we begin to scroll down a little bit, we get a flexible layout. And then when we hit the last trigger point, the screen size changes again and fixes again, and then our preformatted code also changes, in terms of the background color that's being used, the font-size, the padding, so all those things kind of change.
Now, I will honest with you. These really aren't very well-thought-out media queries. I was being a lot more reactive to what I was seeing as I was styling rather than really thinking through and planning my styles the way I should be. Media queries are incredibly powerful, but if you don't have proper organization, your code can get really sloppy very quickly, and that makes it hard to maintain, and it's going to have a lot of unintended side effects when you have conflicts between media queries and other styles, so you have to be very, very careful about that. Now, to deal with that you are going to have to carefully plan and organize your styles when using media queries, and that is something that we are going to discuss in our next movie.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.