02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Media Queries within a CSS file with the @media rule

There are two ways to define media queries. The first is to define it for a full file as we explored in the last lecture. In this lecture, we will take a look at the second way of defining CSS media Query rules. By the end of this lesson you will know the two ways to define media query rules.

Keynotes:

  • How to define Media Queries within a CSS document.
  • When to use the first and when the second.
  • Where to find the full list of supported CSS2 media types

properties:

media queries, HTML4, CSS2.1, print, screen, aural, braille, handheld, print, projection, screen, tty, tv

Video Transcript

00:00Now that we've seen the first way of working with media types, it's time for us to go our
00:08way to the second way.
00:09And the second way as we said is to work directly inside of the CSS file and add customizations.
00:16The way we're going to do that is using the media rule and we're going to see it in
00:19action and really by the end of this lesson, you'll know both versions and elementary
00:25because we now know both versions, it'll be time for us to ask what's better?
00:29Should we use the first version?
00:31The second version?
00:32I'm going to give you my perspective on it and also kind of a big picture of what should
00:35you be thinking about to try to figure out for your solution what is the best solution.
00:41By the end of this lecture, you're going to be familiar with all the different media
00:44types and really ready to build for all the different media types in general.
00:49So I'm going to jump right into it and in my source files, I just want to make sure
00:54that you go ahead and or delete that link to that print version or just comment it
00:59out.
01:00I went ahead also and I was playing around with the fonts, you'll see your own extra
01:03style sheet fear not.
01:05It is a Google font called Oswald, I believe.
01:09All right, now in the print version that we had previously, really what's going on,
01:13we had basically an override of our font to turn it into Verdana.
01:17We said that there won't be a margin to our article and we got rid of our advert.
01:23What I want to do next is I want to go into our main styling and I'm going to go in my
01:27style all the way in the bottom of my document because that's where I want to put my media
01:32queries and really the reason is because I'm overriding the existing media queries that
01:37are there.
01:38Now, the way I do that is using the media query rule or directive by typing media.
01:46Very simple.
01:47The next step is to define the rule itself and so far we've met the rules that were
01:51based on the name or the type of the media.
01:54In this case, we want to approach anything that is print.
01:57Now that I've opened and closed a curly bracket and set my media and let me just go ahead
02:01here and break a few lines so we won't be on the bottom of the screen and how about
02:05we also make it a little bit bigger so it's easier to take a deeper look in this media.
02:11All that's left for me to do is to go ahead and to copy the rules from that print and
02:17move them into my media element.
02:19I'm just going to go ahead here and add a few tabs and save the file.
02:23If I go ahead and click on refresh, now the next step, if I tried to print it originally
02:30without changing modify the design, something like this would have come out.
02:35Now that I've made that update, it's going to override those margins.
02:39It's going to override the font.
02:41It's going to get rid of also the advert, although on this one we don't see the advert
02:45because it's actually not exactly the original print, but this is what will come out
02:49in the end with this media format included.
02:54That's really the basics of working with the two different versions of the media
03:00queries. One we saw by setting it directly into the file path itself and
03:05we've seen that example using the print type. Another is by defining a media rule
03:10and wrapping around all the different situations that you would want to use
03:14that rule to modify your design based on the need of that specific device.
03:20Now you might be asking yourself, so when should you put it in a separate file?
03:24When should you put it in the same file?
03:25The answer is really from what we saw here.
03:27If it's just a little bit, then I would just go ahead and integrate it into the
03:31file itself. If you want to create completely different designs, for example,
03:35in a scenario where there is a different device and you want to use a
03:39different visual language altogether, so you have that control of deciding do you
03:43want to just load up a file with exceptions or do you want to load up
03:47separate files for the different mediums that you want to work with.
03:50So that's it for the basic rules of how to define the media types.
03:55In the next lecture, we're going to go ahead and take a step deeper into working
04:00with media queries. So we'll see in the next lecture, we will see how do we
04:03combine different rules together to define our designs.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!