Start learning with our library of video tutorials taught by experts. Get started

CSS: Page Layouts

Using media queries


From:

CSS: Page Layouts

with James Williamson

Video: Using media queries

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.
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS: Page Layouts
8h 57m Beginner Feb 07, 2012

Viewers: in countries Watching now:

CSS: Page Layouts introduces basic layout concepts, gives advice on how to create properly structured HTML based on prototypes and mockups, and goes into critical page layout skills such as floats and positioning. Author James Williamson shows how to combine these techniques to create fixed, fluid, and responsive layouts. Designers are also shown how to enhance their pages through the creative use of CSS techniques like multi-column text, opacity, and the background property. Exercise files are included with this course.

Topics include:
  • Reviewing the box model
  • Calculating em and percentage values
  • Controlling how elements display
  • Creating fixed, fluid, and responsive layouts
  • Structuring content with HTML5
  • Floating elements
  • Using relative, absolute, or fixed positioning
  • Defining column spacing
  • Creating grid-based assets and layouts
  • Considering mobile-design-specific issues
  • Working with multi-column text
  • Enhancing page design CSS Sprites
Subjects:
Web Web Design
Software:
CSS
Author:
James Williamson

Using media queries

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.

There are currently no FAQs about CSS: Page Layouts.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Page Layouts.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked