Start learning with our library of video tutorials taught by experts. Get started
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.
While media queries give us the ability to alter our page layouts based on screen size, they can't always control how the page is viewed on devices like smartphones. Now many mobile devices will either misrepresent their screen size or apply an initial zoom to your site that can cause your site to be difficult to read. In this movie I'm going to show you how to ensure that your page displays initially the way you want it to, and a few techniques that you can use to speed up the performance of your site on mobile devices. So I have the mobile.htm from the 07_05 file opened, and I've just opened this up in a browser really quickly because I want to talk a little bit about the meta viewport tag, which is what we're going to be using in part of this lesson.
So the meta viewport tag, it gives you the ability to communicate some of your preferences to the different devices that might be accessing your pages, and smartphones almost universally understand this tag. So I've got two things going on in the syntax that you're seeing here. The first thing is the content width= device-width, so that is essentially asking the mobile device to go ahead and set its screen width to what the actual device width is. So essentially a lot of mobile phones, such as the iPhone, will go ahead and display your page as if they had a 980-pixel widescreen. Obviously they don't, so essentially what they are doing is they're zooming out.
So this is something that we call the viewport. So the viewport in a mobile device is usually bigger than the actual screen. So essentially by telling it to set its width to the actual device width, you're ensuring that the entire viewport is going to be visible within the screen. Now the second property that I'm passing into it is the initial-scale=1.0. What that does, it's going to ensure that the device is showing the page at 100% and not zooming out or zooming in on any one portion of it. A lot of mobile devices have a default zoom of about 50%, so that you're seeing more of the page at once.
So this scales it to 100%, so that if you design it for that screen size, then it's going to be viewed exactly the way you want it to. Okay, so with that out of the way, I'm going to go into our HTML. Now you can see that I have several files opened right now. In addition to the mobile.htm file that I have opened, I have four style sheets opened. Those can all be found in the 07_05_CSS folder now. We'll take care of out meta viewport tag first, and then I am going to come back and talk about the style sheets. So after our meta character encoding tag, I'm going to come up and do meta. And then for the name attribute, I'm going to type in viewport. And then after that I'm going to go ahead and pass in content, and for content I'm going to do width = to device-width.
Now I can comma-separate these property, so before I close my quotation mark I'm going to do another comma, and I'm going to type in initial-scale =1.0. So there are a lot of properties that you can pass into that. You can disable user scaling. I mean there are all sorts of stuff that you can do with the meta viewport tag. If you do a quick search on it, you're going to find out all the really cool things that you can do to control how somebody on a mobile device is viewing your file. All right, so I'm going to save that, and the next thing I want to tackle are our style sheets down here. Now this is something that I see pretty commonly when people are using media queries.
They'll create separate style sheets. They may create an overall global style sheet, and then they may separate style sheets for each one of the devices that they're targeting, and then link to them using media queries. Well, this is not a bad approach. The only downside to this, and it is a really big downside, is that you're making several different http requests to the server for each style sheet. And so what happens is for mobile devices, those requests are still made even if they don't need the style sheet. So for a mobile device it would still be making the request for the desktop CSS, even though when it wouldn't be rendering those styles.
Those requests, especially over a slow network, can take a lot of time. So you want to limit the number of HTTP requests you're making when you're designing for that context of responsive design. So what we're going to do is we're going to get rid of all of these except for one request. So I'm going to go ahead and simply request the global.CSS. And again, I can pass any media attribute I want into that. In this case, I'm testing for screen and projection devices. So that's fine. I don't have to change it at all. What I can do, however--I'm going to go into the global.CSS to show you this-- if I scroll down to the bottom of my global.CSS styles, you can see that I have some commented-out style sheets.
I have my mobile files, I have tablets, and I have desktop, and these are using that @media block syntax that we were using in our previous examples. So @media blocks work, even when they're within an external style sheet. They don't have to be just within a style tag in an HTML document. What that means is I can have one style sheet that has not only just the global styles, but also all the groupings of the media queries as well. Now it's going to create a larger style sheet. You're going to have much bigger file, but you're only making one request rather than several requests, and the savings is definitely worth it. So what I'm going to do is I'm going to come in and I'm going to uncomment these out.
So I'm just going to basically comment out each of the titles of these guys. And then after I do that, I'm going to very carefully going to each one of these and I'm going to select all of the text and copy it, so all the styles and copy it, and then come back into the appropriate area and paste them. And I'm going to do that for desktop. Then I'm going to do it for tablet. And I have be very careful. Remember, make sure you're inside the opening and the closing curly braces. And then finally, I'm going to do it for mobile. Now, remember earlier I said I was going to talk to you about how to create cross-browser-compliant word wrapping for your preformatted text, and there is the selector for it right there.
So that's a nice code snippet for you to copy and paste in your files, or if you are ever looking to control word wrapping, that's a really nice snippet of code to have around. It's quite handy. So I'm going to copy these, go into global, roll up to my mobile styles, and then paste them. Now I'm doing one more thing here that I want to point out to you guys, for my mobile styles, that I'm not doing for my tablet or for my desktop styles. For example, this page, if you look at it, we've got this photo gallery and if I resize my browser and hit my target points, the gallery itself changes to sort of fit that format. And when I get into my mobile styles, it becomes much smaller.
Well, it's not just scaling those images. We know that we can scale images, but that's not what it's doing. You'll notice that in the mobile styles, it's actually requesting a smaller version of it. So instead of requesting a much larger image that would take longer to download, by using media queries, I'm able to request a smaller file, which is going to come over faster. It's going to look better, because it's not scaling down. There is no scaling factor involved. And it's a better overall solution for this, and it's only requested if the mobile styles are triggered. So I'm going to save this. Then I'm going to go back to my mobile.htm and save it as well.
You'll notice now that instead of four separate requests for style sheets, we only have one. Now if I save this file and I go back into my browser and I refresh it, I'm not going to notice any difference whatsoever. Everything looks the same, everything reacts the same, and everything is styled the same. The only difference is the amount of requests we're making to the server, and of course we have little bit of control put in place there for our mobile devices. Now if you have an existing hosting account, I encourage you to try out that meta viewport tag, upload it to a live file, and then test that on your phone, and view it in a site that doesn't have the meta viewport tag, and look at the difference between the two of them.
It's a really interesting and powerful way of controlling how your site is displayed. Now there is, of course, a lot more to designing for mobile devices than we were able to cover here. However, the meta viewport tag, combined with media queries, gives you a fair amount of control over how your sites are viewed on mobile devices. And as we were discussing, make sure that you keep file overhead, the HTTP requests, and resource sizes in mind when planning your mobile styles. You know, as designers, we tend to not think about those things very much for desktop, but they are critically important to creating good mobile experiences for your viewers.
There are currently no FAQs about CSS: Page Layouts.
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.