Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Organizing styles

From: CSS: Page Layouts

Video: Organizing styles

Media queries are amazing. The ability to totally change your layout based on factors like screen size or orientation really opens up a lot of possibilities for us as designers. However, because of their selective nature in how they apply styles, media queries can create a huge headache for you if you don't properly plan and organize your styles. In this movie we will explore organizing your media queries in a way that allows you to write the most efficient styles possible and prevents styling conflicts when media queries are triggered.

Organizing styles

Media queries are amazing. The ability to totally change your layout based on factors like screen size or orientation really opens up a lot of possibilities for us as designers. However, because of their selective nature in how they apply styles, media queries can create a huge headache for you if you don't properly plan and organize your styles. In this movie we will explore organizing your media queries in a way that allows you to write the most efficient styles possible and prevents styling conflicts when media queries are triggered.

So I have the organizing.htm file open here, and for all intents and purposes, it's pretty much exactly the same file that we were working with in our last exercise. However, there have been some slight changes to the styles. And if I scroll down a little bit into the styles, I can see that I have a grouping of styles down here now that say "styles that change." So essentially, one of most important parts of planning media queries is to identify what I call global styles versus styles that change. Now, global styles--I am going to scroll up and we have our global styles right up here-- these are styles that are consistent regardless of which media query is being triggered or which condition is currently present.

And by identifying those styles at the very beginning of your planning process, you can group those styles together, keep them out of the media queries. This will help prevent conflicts, style conflicts from one media query to the other. It's also going to make your styles a little bit more efficient and a little bit more lightweight. So what I am going to do is right above the opening style tag, I am just going to go ahead and start a comment, and I am going to type in global styles and then sort of close that comment there. Now, there are two ways of approaching dealing with styles that change, and we are going to kind of tackle both of them.

One would be to split selectors up. You can see here, for example, that from the body tag, I have pulled out the width value, because the width is going to change based upon which media query is active. The rest of the properties for the body tag doesn't change, so I've placed them in the global styles. Now, that's one way of doing it. The downside of organizing your style this way of course is that you have multiple selectors that you'll need to remember to modify if you are going to make changes to them. So you have to kind of remember, okay, the width for body is in the media query. Everything else is up in global styles. Now, the second way that you could do it is the way that we are going to do with the preformatted elements.

So for those elements we're going to place that code in every single media query, and in some cases we are going to be duplicating code. So it creates slightly larger code, but your styles are localized, and you know where to find that property. The big problem with that, however, is something like this, the border-bottom which doesn't change. If you want to change that for everybody, you now have to update and modify that selector three times in three different media queries instead of just changing it up in the global. So there's pros and cons of doing it either way. Okay. So above styles that change we are going to right our media queries, and we are going to go ahead and just write all three of them.

We are going to write one that targets mobile styles, another one that targets tablets, and another one that targets desktop styles. So I am going to start off by doing a comment and just commenting out mobile styles, and then underneath that I am going to right a media query. So I am going to do a @media block, and after that I am going to type in "only screen and," and then we are going to do max-width of 480 pixels, and I am going to open and close curly braces. Now, the reason that I did 480 pixels is because that's basically the measurement of an iPhone screen size at landscape, so that's targeting really a lot of the newer generations smartphones that happen to be a little bit wider than the 320 that the iPhones are, and it also targets an iPhone that would be in landscape mode. All right! Now, I am a big fan of saving myself a little bit of time, so I am going to copy these and I am going to paste them twice.

The second one I am going to change to tablet styles, and then the third one, I am going to change the comment to desktop styles. Now, of course we also have to modify the media query. So the tablet styles media query, I am going to change max-width here to min-width. And I am going to go just one pixel higher, 481, so we are targeting any screen size 1 pixel up from this one. And then I am going to create a range by doing another expression. So I am going to say and, and then we are going to do max-width, and the max-width here is going to be 768, which is targeting an iPad.

Now, as far as tablets go, the sizes of these devices are changing all the time, so one of the things you are going to have to do, if you are into responsive design and you want to start using media queries, is tracking the different devices that you are targeting and finding out what the ranges of those devices are, so that you can write a media query that's going to target exactly the screen size that you're looking for. And then finally, we are going to go down to our desktop styles and we are going to modify that one as well. And here we are just going to change max-width to min-width, and the min-width of our desktop styles is going to be one greater than our last range, in this case 769 pixels.

So now I am going to go ahead and save that. We know the styles that change, so I am going to go ahead and grab those selectors and I am going to, again, just cut them. We don't need them below our media queries, so I can go ahead and get rid of all this as well. And then I am just going to paste them in each media query. Now, I have to be very careful about how I do this. I am going to start in mobile. I am just going to paste them right there in mobile, and I have to be careful to paste it inside the curly braces of the mobile media query. It's very easy to paste outside of it, and then you are going to have some syntax errors, and nothing will work, so be very careful about that. Now, the first thing I am going to do with my mobile media queries is I am going to change the width from 700 pixels to 320 pixels.

Then I am going to go in and change the font-size from 1.2ems for the pre to .8. I am going to change the padding to just .5. And then I am going to change the background color as well. So the rgb value here is going to be 246, followed by 247, and that's going to be followed by 160. I am also going to add a property here, so really quickly, I am going to go down just below the pre and I am just going to type in font-weight, and the font-weight is going to be bold. I am going to save that.

Now, I am going to go into my tablet styles and I am going to paste it into my tablet styles as well. And for tablet styles I am going to change the width to 80%. So again, we are making it flexible in that middle range. And then for font-size, I am changing the font-size down to 1em, I am changing the padding to .5ems, just as it is with the mobile devices, and I am going to also give this a different background color as well. So I am going to go 240 for rgb values. I am going to go 240 for red, 213 for green, and then 154 for blue. I am going to save that.

And then finally, I am going to go down into my desktop styles and again, being very careful about where I paste these, I am going to paste these right into the desktop styles. Now, these don't change. These were the desktop styles, so I don't need to make any modifications there. I am just going to go ahead and save this. And before we preview it in the browser, again, I want to talk about the organizational structure here. We have our global styles. These are styles that don't change and will apply globally to every single context of how this site is viewed. Then we have an @media block that only will apply to mobile devices, and in this case it's really only going to apply to anything whose screen is below 480 pixels.

And it would help if I type pixels there. It's a good thing we go through our styles at the end, isn't it? So yes, if you typed that along with me, because you were doing what I told you to do instead of what you should be doing, type pixels in there if you don't mind for me. That's another reason why you always go over your styles. So after the mobile styles, we have our tablet styles. And, again, they are going to apply between the measurements of 481 and 768, and it's pixels again. I guess because I copied and pasted, it's going to be like that on every single one of them, so be sure to check all of those out. And then finally, we have pixels there, so yay, for our desktop styles.

I am going to go ahead save this, go out to my browser, and refresh my page, and now you'll notice as I begin resizing it, we get a change in page size. We get a flexible layout. We get a change in the background color of the media queries. Finally, I get my mobile size, where the page is constrained to a specific size. We have a different background color for our preformatted text. And the preformatted text is not fitting in there anymore, so in just a little bit I am going to show you guys how we can word-wrap that, to help it fit there at small sizes. All right! So I will go back in my file and we will save it.

Now, obviously we are only changing two elements on the page: the body and the preformatted tags. Now, in a real-world scenario, it's really critical that you plan your styles out very, very carefully. Really think them through before creating your media queries. Now, the resulting CSS files are usually really complex, and if they are not well-planned, you are going to find that maintaining them will be an almost impossible chore. Not to mention trying to find the find source of conflicts that might rise. Now, we're going to take media queries a little bit further in our next exercise, as we take a look at how we can make the content within our pages as responsive as the styles themselves.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 41888 viewers

James Williamson
Author

 
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

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

Join now Already a member? Log in

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 preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.