navigate site menu

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

CSS: Page Layouts

CSS: Page Layouts

with James Williamson

 


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

show more

author
James Williamson
subject
Web, Web Design
software
CSS
level
Beginner
duration
8h 57m
released
Feb 07, 2012

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi! I am James Williamson, senior author here at lynda.com, and I want to welcome
00:08you to CSS: Page Layout.
00:10In this course we are going to explore the properties and techniques around
00:14controlling page layout through Cascading Style Sheets.
00:16We will start off with a quick overview of basic page-layout concepts and then
00:20talk about how to design your pages with CSS in mind, and properly structure the HTML.
00:26We will then discuss CSS-page-layout concepts like floats and positioning
00:30elements, and move on to exploring different types of page layouts, such as
00:34fixed, flexible, and responsive.
00:37After that, we will focus on how to control the content itself by tackling
00:41navigation, images, and techniques that can enhance your overall page design.
00:47By now, I'm sure you're eager to get started,
00:49so crank up your favorite code editor and let's explore CSS: Page Layout.
Collapse this transcript
How to use the exercise files
00:00If you're a Premium number of the lynda.com Online Training Library or if you're
00:04watching this tutorial on a disc, you have access to exercise files used
00:08throughout this title.
00:10The exercise files for this title are arranged by chapter and are located in
00:14folders that are named based on the movie number they represent.
00:17Now since each exercise is self-contained, you will want to work with those
00:21folders individually;
00:23however, I recommend copying the entire exercise files folder to the desktop and
00:28then working on the files from the appropriate location.
00:30I will call out the location of the exercise files for each movie and you should
00:34see a brief overlay that will also tell you where to find those files.
00:38Now this course focuses on common page- layout techniques with CSS, and not on
00:43any one authoring tool.
00:45As such, it doesn't matter which authoring tool you use.
00:48If you're comfortable with an existing HTML editor, simply continue using that
00:53one for this course.
00:54I'll be using Aptana Studio for the course for a couple of reasons. First, it's free.
00:59It's cross-platform, it's very lightweight, and it allows me to focus solely
01:03on the code itself.
01:05I am going to give you a brief tour of how I am going to set up Aptana in case
01:09you want to use it and have your workspace look a little like mine, but in the
01:12end it really doesn't matter which tool you use.
01:15So here I am, in Aptana, and I'm just going to run through a very brief setup.
01:20The first thing I am going to do is create a new project so that I can have
01:23quick access to my exercise files.
01:25So I am just going to go up to File and choose New, and I am just going to do a new web project.
01:29I will just go ahead and call it CSS Page Layout. And instead of using the
01:36default location for root folder, I will just browse out to my desktop and find
01:41those exercise files right there.
01:42I will open that up and I will finish it.
01:46Now I have access to all of my exercise files right from this panel and I can
01:50just click and open them up.
01:52Now the next thing I want to do is make life a little bit easier for you guys
01:55that are trying to read my code.
01:57The default code in Aptana is pretty small, and I will just make it a little bit
02:01larger, maybe change font up a little bit to make it easier for you to read.
02:04So I am going to go up to Aptana Studio and go to Preferences, and I am going to
02:08click on the Aptana Studio category and then click on Themes.
02:13The first thing that I want to do is just make the text a little bit easier
02:15for you guys to read,
02:15so I am going to go down to Font and just choose Select.
02:18I will go up and choose, I will do Arial, and I will take it up to about 24 points.
02:23Now again, you guys don't have to do this with me.
02:25The only reason I'm doing this is to make life a little bit easier on you guys
02:28and make it easier for you to read my text.
02:30I will go ahead and apply that, and I can see right away that that's happening.
02:33Now in terms of the themes, I am just going to use the default Aptana Studio theme,
02:37but feel free to use any of these.
02:39Notice they even have one that mimics Dreamweaver.
02:41So if there's a coding environment you're more comfortable in and you're not
02:44actually using that program, feel free to choose that particular theme.
02:47The one last thing I want to check is I am going to go up to Editors here and I
02:50am going to make sure that Enable word wrap is selected and then just click OK.
02:54From time to time I might resize some of my dialog boxes, but for the most part
02:59that is all I'm going to do with Aptana Studio.
03:00I am just going to keep the settings as basic as possible.
03:04Now it's also crucial that you're going to be using the most recent versions of
03:08Chrome, Firefox, Safari, Opera, and Internet Explorer if you're on a PC.
03:13So go ahead and download and install those, and of course throughout the course
03:17we are going to make it a habit of testing our pages with multiple browsers. All right!
03:21So now that we are all set, let's go ahead and dive in and get started with
03:24CSS page layouts.
Collapse this transcript
1. Layout Basics
Box model review
00:00Before we start exploring the individual techniques of page layout, I want to
00:03begin with a general overview of CSS page-layout concepts.
00:07At the core of almost all page-layout techniques is the box model, so that makes
00:11reviewing it a logical starting point.
00:13Before we get into that, however, I feel like I need to mention again that
00:16this course is a continuation of my CSSL Core Concepts course. That means that in this course
00:22we are going to be building on the material that covered in the CSS:
00:25Core Concepts course.
00:26There will be some repetition, designed to reinforce important concepts, but for
00:30the most part, if you're new to CSS and are starting with this title, I actually
00:34recommend that you go back and start with the Core Concepts title and then come
00:38back to this on when you finish.
00:39I had pretty much said the exact same thing in the Welcome movie, but I know
00:43that a lot of you guys skip those, so I wanted to mention it again here
00:46before got too far into the title. Okay, cool!
00:49Now the box model is the term that we use to describe the rules and
00:52properties surrounding the physical properties of all HTML elements on the
00:57page, and basically every single element on your web page is considered to be
01:01in a rectangular box.
01:03The properties of this box--its width, height, padding, borders and margins--
01:08define not only the size of the element, but also how it relates to all of
01:12the elements around it.
01:14Without these properties, we would not be able to control layout at all.
01:17So to visualize these properties a little bit better, we are going to go ahead
01:20and modify the properties of a sample element.
01:24Okay, so here I am, in Aptana Studio again. Feel free to use whatever code editor you want.
01:28It does not matter, in terms of which tool you use.
01:31You just want to be able to access and modify the code. That is it.
01:34You can even use just a basic text editor if you want. All right!
01:37I have the box-model.htm file opened, and you can find that in the Chapter_01
01:42folder, 01_01 directory.
01:45Just a brief overview of the structure of the page here. You can see we have
01:48some default styles in the head of the document and if I scroll down, very
01:52simple HTML file. We have two div tags, one wrapping the other one. The outer
01:56div has a class of outer applied to it, the inner one has a class of box, and
02:00inside that we have a paragraph.
02:02If we look at the styles, we have some preexisting styles for outer that are
02:06giving it a little bit of definition.
02:07We have a background color for our paragraph that's going to show you what
02:10makes it a paragraph itself, and then we have a blank selector, an empty
02:14selector here for box, and that's of course what we are going to be
02:16experimenting a little bit.
02:17Now before we do that, let's just preview this in a browser and kind of see
02:21what it looks like.
02:22So I am going to go right up here in Aptana Studio to my configurations.
02:25Now one more Aptana Studio note, if you are using it along with me: if I
02:28click on this little pulldown menu for Configurations, I can see all of my browsers here.
02:33You may not see that list.
02:34You may have those browsers installed, but Aptana might not pre-populate that list.
02:38So if I go right here to Run Configurations, I can go down to my web browser
02:42configurations and choose a new one and then just point it to the different
02:45browser executable files that I want, and you will have to do that for each
02:48browser that you want to load in, all right!
02:50So I am going to go ahead and preview this, say, in Firefox.
02:54I can see the blue color here is the background color of the outer div tag;
02:59the orange color is the background color of the paragraph, okay.
03:02So I am going to go back into my code, and let's start adding a few things to our box element.
03:07Now the first thing I am going to add is a background to this. And I am just
03:10going to go ahead and give this a color of #f90.
03:14Now, the background property is not considered part of the box mode; however,
03:18it does help us define this element, and you will often see it included in
03:22discussions of the box model. But in terms of whether it is a part of the box
03:26model or not, it's generally not considered that. All right!
03:28So if I save this file and go back into my browser and reload the page, I can
03:33see that it doesn't look like really anything has changed.
03:35Well, that has everything to do with sort of the stacking order, if you will.
03:39This paragraph is sort of stacked on top of the div tag that it's inside of,
03:44which is in turn stacked on top of the div tag that it is inside of.
03:47So the color is there, but because the inner div tag doesn't really have any
03:51properties assigned to it, it's sort of shrinkwrapping to our paragraph, and
03:54that's why we don't see the background color. All right!
03:57I am going to go back into my code. Now let's go ahead and add some
03:59definition to our box.
04:01Let's go ahead and give it a width of 300 pixels and let's give it a height
04:07of 300 pixels as well.
04:09All right, so I am going to save that, preview that in the browser, and now we
04:13can see a little of a change here.
04:15We see that the paragraph looks like it's a lot wider, and it's a lot taller, but
04:18again, we are not seeing the background color.
04:20Now the reason for that has everything to do with how block-level elements react
04:24when they are inside of another element.
04:26Now block-level sort of are elements that occupy through their own space within
04:30the normal flow of the document. Paragraphs are block-level elements, div tags are,
04:34headings, or things like that.
04:36Well, a block-level element is told basically that if it doesn't have a
04:39defined width and height, it should just go ahead and expand to fill the container element.
04:44So in this case, it's expanding to fill the div tag that we just made a little bit larger.
04:48Okay! The next thing we want to do is let's throw some padding on here.
04:51So I am going to come into our box properties.
04:53I am just going to type in padding, and I am going to type in 25px.
04:58Now this is padding's shorthand notation.
05:00We could also say padding-top, padding- bottom, padding-right, padding-left, that
05:04sort of thing. By saying just padding and passing one value into it, we are
05:08going to get the same amount of padding all the way around our element.
05:10So remember, padding is the space inside the element.
05:13It separates the content from the border edge of the element itself,
05:17so we should expect to see a little bit of cushion between the paragraph now and
05:20the edges of this inner box.
05:22So we save this and preview it, and indeed that is exactly what we get.
05:27Notice that the color that we defined for the inner box is a
05:30slightly darker orange.
05:32So what we are seeing on the inside, that's the paragraph's dimensions, and now
05:36on the outside, we're seeing the box for the actual box dimensions for the inner box.
05:41Now one thing to note here is that even though we've defined its width and
05:45height at 300px by 300px, the 25px that we've just added all way around it have
05:50actually added to the overall width and height.
05:53So now we have 25px on this side, 300px here, and then 25px on this side.
05:58So now the total width of our element is now 350px.
06:02Okay, so what about borders?
06:04Borders are also considered part of the box model, so right after padding, I am
06:07going to type in border and then we are going to do 2px solid black.
06:14So again, this is shorthand notation.
06:15This is us telling the user agent that we want a border all the way around of our element.
06:19We want that border to be solid, we want the color to be black, and we want
06:22it to be 2px in width.
06:24So again, if I save this, preview it in my browser, I can see that indeed our
06:29inner box now has that 2-pixel black border.
06:31If you're wondering, yes, that is cumulative as well.
06:34So we've added an additional 2 pixels on either side of this, so now instead of being
06:39just 350 pixels wide and 350 pixels tall, it is now 354 pixels wide and 354 pixels tall. All right!
06:49One last property to take a look at and that is margins.
06:53So if I come back into my code and I go down to the next line and type in margin,
06:57and we'll just go ahead and do 25 pixels of margin there as well.
07:01Just like padding, we are using shorthand notation here for margin.
07:04Now margin doesn't always calculate to the total width of an element.
07:08It basically controls the spacing from one element to another. And almost always
07:13it does calculate in terms of the space it's going to take up within the layout,
07:16but there are instances, for example, when it's inside of a container that's too
07:20small to contain it, where sometimes the margins will basically be ignored, or in
07:24the case of a vertical margin, sometimes that is going to collapse.
07:27That actually is such a big deal that we are going to discuss that in a separate
07:31movie in just a little bit, when we talk about calculating element dimensions. All right!
07:35So I am going to go ahead and save that, preview that in the browser. And it
07:39doesn't look like a whole lot has changed, except for it looks like our box
07:42has shifted position.
07:44Essentially, what's happening here is now this margin is pushing the top of the
07:47box away from the top edge of the containing element, and it's pushing the edges away.
07:53So basically, it's sort of creating its own space inside the containing
07:56element at this point.
07:58Now what about total width and height?
08:00Well, remember we have 300 pixels here;
08:03we have 25 here and 25 here, so it's 350.
08:05If you take a look at the margins, that's another 25 on either side, so that's 400
08:09pixels wide. But the border, remember 2 pixels and 2 pixels, so that'd be 404 pixels wide, or
08:16at least it's taking up 404 pixels' worth of space.
08:19It may not be that physically wide, but it's at least taking up that amount of space.
08:23So just remember that width, height, padding, borders, and margin, all those
08:29properties combine to make up the box model.
08:32Now when we plan our layouts, you are going to need to understand how all those
08:36properties combine to create the overall width and the height of an element.
08:39Like I had mentioned just a second ago, that is so important we are going to
08:43take a closer look at calculating element dimensions in our next movie.
Collapse this transcript
Calculating element dimensions
00:00One of the most important skills in CSS regarding page layout has nothing to do
00:04with positioning elements on the page.
00:06It is in fact the ability to accurately calculate an element's dimensions.
00:10Now on the surface that sounds like something that's pretty easy to do, but as
00:14we are going to see, it gets a little bit more complicated than maybe you'd
00:16think at first glance.
00:17So to illustrate this concept, we are going to be working with the
00:20dimensions.htm file, and you can find this one in the 01_02 directory, in
00:26the Chapter_01 folder.
00:28Okay, so if I take a real quick look at the structure of the page, you are going
00:31to see that it's very similar to the exercise file we just worked with.
00:35We have two divs, an outer div and then an inner div with the class of box
00:39applied to it, and that has a paragraph inside of that.
00:43Our sort of default styles that we have going on:
00:45the outer div tag is now set to a width of 600px wide,
00:49it has a margin of zero for top and bottom, and then left and right is set to auto.
00:53Now what that does for us is when we set left and right to auto, it'll go ahead
00:57and just take that content and center it within whatever parent element it's
01:00floating inside of, which in this case is the body tag. And then we have a
01:04background color applied for our outer that's a light-blue color.
01:06For inner box, the only thing that we have right now is just a background color.
01:10And for our paragraph,
01:11we have not only a background color that's going to allow us to sort of visually
01:14define the contents of the box, but we also have the height set to 100%, and that
01:18way no matter what happens with the inner box, the paragraph will sort of expand
01:22to fill the height of it as well.
01:24So the first thing we are going to do is we are going to go into our box here, and
01:27we are going to go ahead and define its width as being 300 pixels wide. Easy enough.
01:31I am going to save that, and let's just preview that in one of our browsers.
01:36Okay, so what we're seeing here is we are seeing the overall width of
01:39600 pixels--that's our outer div tag--and then our inner div, or the box div tag, is 300px wide.
01:45So we're getting exactly what we expect here.
01:47We are getting exactly half of that outer div.
01:49Okay, so I am going to go back into my box and let's add some more box model properties.
01:54On the next line I am going to go ahead and do padding of 50px.
01:56Now remember, padding and width are cumulative, so this is going to add 50 pixels
02:01worth of padding all the way around our box, so it's going to increase both our
02:04height and our width, but we are just going to focus on width.
02:07So what's happening here is we have 50 pixels' worth of the padding on the left-hand
02:10side, 50 pixels' worth of padding on the right-hand side, and then a width of 300px.
02:14That gives us a total width of our element now of 400 pixels.
02:17So I am going to save this, refresh that in my browser, and that is indeed
02:22exactly what we are seeing.
02:23We are seeing the background color now of the box div tag.
02:26This is the background color of the paragraph, and then we can visualize that
02:2950 pixels' worth of spacing here and 50 pixels' worth of spacing here, 300 pixels' worth of
02:33spacing here, and that it's taking up a total of 400 pixels' inside of that outer div tag.
02:38Awesome! Now one of the elements that people really often forget about when they are
02:43calculating the width of their elements or factoring how much space something
02:46can take up within the layout are borders, but borders are part of the box
02:49model and they do calculate to an element's size.
02:52So if I go down on the next line and I type in border, and let's say we do 2 pixels
02:57of border solid, and then black, all right, so I am going to save that,
03:01if I preview that in my page, that does increase the overall size of my element.
03:06Now borders show up on the outside edge of the padding edge,
03:10so essentially 2 pixels have been added to the outer edge of my element.
03:14But because it's inside of this outer div tag, it is inside that.
03:18It doesn't hang over or overlap.
03:20It stays inside the content edge of its parent.
03:23So essentially, now we have added 2 pixels to the left and 2 pixels to the right, assuming
03:28now we have a total width if 404 pixels.
03:32Okay, so how do margins play into that equation? It's confusing.
03:35Sometimes you will hear people say margins don't calculate to the overall width
03:39of an element; sometimes you will hear people say they do, and it becomes a
03:42really confusing thing.
03:43So let's take a look at what margins add to our element and sort of how they
03:46play into the overall size of our element.
03:48So if I go back into my code, if I go down onto another line within the box
03:52selector and this time I am just going to apply a margin to one side.
03:55I am going to do margin-left. And we'll go ahead and do 50 pixels there, okay.
03:59So I am going to save this and again I am going to refresh this in my browser.
04:03Okay, so here is essentially what's happening.
04:05The actual width of my element really hasn't changed, but the amount of space
04:09that it takes up has changed.
04:11So we are pushing our element away, 50 pixels from the edge of its containing
04:15element, so the amount of space it needs within the layout to do this are now
04:19454 pixels, so 404 pixels for the total width of the element and then the 50 pixels over here
04:25on the left-hand side.
04:26So let's clarify the relationship between margins and their elements, in terms of size.
04:32The best way to think about it are there are two sorts of blocks of calculations
04:36that are used for an element.
04:36You have the containing block and the content block.
04:39Now the content block are all of the border, padding, and width of an element. So
04:44the width property, any padding, and border,
04:47they all go together to make up the actual content block of an element.
04:50The containing block are the margin edges.
04:53So in this case the containing block is 454 pixels or the content block is 404 pixels. May
05:00not seem like a big deal.
05:01We certainly need to understand how much space something is taking up within the
05:04layout, so understanding the margins is extremely important.
05:07However, certain things happen when an element is considered overconstrained,
05:12so what does overconstrained mean?
05:14Well, overconstrained means an element no longer fits inside of its parent.
05:17So what happens when we start increasing the values of the size of our
05:21element and increase it to a point to where it just doesn't fit inside this 600 pixels anymore?
05:25Well, when something is overconstrained, your browser has to make a decision, or
05:29the user agent has to make a decision, about what to do, and often it will
05:33sacrifice margins and padding in order to make things fit.
05:37There are other properties that we can use too to control this sort of overflow,
05:40if you will, and we'll talk a little bit about that in just a moment.
05:43But let's go ahead and make this overconstrained and see what happens with our margins.
05:46All right! So I am going to go back into my file, and I am going to ahead and change the
05:50width of our box now to 400 pixels.
05:53So that gives us a total of 504 pixels of width.
05:57Now remember we are inside of a div tag whose width is set is 600.
06:00So for the moment we still fit; however, what I am going to do is I am going
06:02to take margin-left,
06:03I am going to get rid of left, and I am just going to apply that as margin.
06:06So if you think about this, the content block is going to fit, because the
06:10content block is 504 pixels.
06:13The containing block will not fit.
06:15The containing block, with 50 pixels' worth of space on either side, is now 604 pixels,
06:19whereas our outer div tag is now 600 pixels. All right!
06:22Everybody still with me? Cool! So save this.
06:25And I am going to update this in my browser.
06:27Now at first glance, it looks like everything is okay.
06:30It fits, right? Not entirely.
06:32What happens is because we have 50 pixels' worth of margin on either side, it's sort
06:37of centering, or floating, if you will, in the middle of our div tag, and it gives
06:40us the illusion that it fits.
06:42But the problem is--well, it's not really a problem, but what our browser has
06:45done is it essentially has ignored certain margin values.
06:48It simply just throws out the margin values that would overlap on either side.
06:54Now, if you don't believe me, let's take this to a little bit more of an extreme.
06:56So I am going to go back into box, and let's say, I go in and say, okay, on
07:02the margin-right, go ahead and add 100 pixels' worth of space of there on the right-hand side.
07:08So if I save this and test it. Nothing happens.
07:11And 100 pixels, it's just thrown away, because the browser is saying, "No, I can't push it that far.
07:15It won't fit anymore.
07:16So I am just going to sort of ignore that, okay."
07:19Now what about the content block, what happens when the content block no longer fits?
07:24All right! So what I am going to do now is I am going to go in and remove all those
07:28margins, and then I am going to take my padding and I am going to change my
07:32padding to 98 pixels. Why 90 pixels?
07:33Well, remember we have borders of 2 pixels on either side, so that it's essentially
07:39giving us a combination of padding and border of 100 on either side. So we have
07:44100 pixels' worth of padding and border on the left-hand side, 100 pixels' worth of padding
07:48and border on the right-hand side, 400 pixels' worth of width,
07:51that's going to give us an equal value to our outer div tag of 600 pixels.
07:54So if I save this, and preview it, it fits precisely. You can't see the outer
07:59div tag anymore, but I promise you, it fits precisely.
08:02Okay, so what does happen if I make it a little bit wider?
08:05So if I increase the width to say 500 pixels and save that, what happens?
08:10Well, it's hard to see what's happening right now.
08:12So let me do something really quick so you can visualize what's happening here.
08:16I am going to go ahead and take my height here and move my height to say 800 pixels
08:19so you can see this. All right!
08:21So let's save that and let's preview it. All right!
08:23So you can see what's happening.
08:25The inner div tag is simply overlapping,
08:28it's sort of spilling out of the outer div tag.
08:32So what happens when a content block no longer fits
08:34is we have something that we call overflow.
08:36Now you can control overflow by using the overflow property.
08:39If I go back into my outer div tag, for example, and I am just going to type in
08:43overflow. And there are a couple of values you can use here.
08:46You can use hidden, and if I use hidden, it simply crops off any content block
08:52that would overflow the element. I can do visible.
08:55I can do scroll.
08:56I can do auto. Now auto and scroll are very similar.
08:59Auto basically just says, if it does overflow, go ahead and give me scrollbars, and
09:02you can see that at the bottom in the element here I have scrollbars that let me
09:05scroll through this. Harder to see, because of the height of the element, but I
09:09think you get the idea. All right!
09:11So that's the overflow property.
09:12Now I am going to get rid of the overflow property here. And I want to show
09:15you one more thing, in terms of what happens when we have an overconstrained element,
09:19so we've got one more thing to take a look at here. All right!
09:21I am going to go back to my box and I am going to change the width of the box back to 400 pixels.
09:26Now I am going to go up to my outer element here and I am going to get rid of
09:29this height property so that we are not confused by that, and what I am going
09:33to do is I am going to set my padding on the outer div tag to 50 pixels.
09:39So that's just affecting the overall width of that element as well.
09:42So now that the outer div tag is now 700 pixels wide, obviously. All right!
09:45Now with my box width being 400 pixels, of course I have 100 pixels on either side, so
09:50that's 600px, right?
09:52So if I save this and preview it, we can see there is the additional 50 pixels' worth
09:57of padding for our outer div tag.
10:00Now keep an eye on that padding.
10:01If I come back in the box and I increase the total value of box here, let's
10:06say I increase its width to 450px and save that and preview it, notice what happens.
10:13It expands to fill that empty space.
10:15The padding on this side is basically sacrificed.
10:18So essentially, it fills this in and it says, "Okay, I have 50 pixels' worth of
10:20padding here and I have this huge total width over here," and this padding is
10:24basically sacrificed.
10:25It's actually still there and other elements would respond to it, but because
10:29this is now overflowing, it overflows that padding value.
10:32Now the reason I'm mentioning this is because it's really important for you to
10:35understand what happens if you don't really plan your elements properly.
10:39If you are not carefully considering the dimensions of your outer elements and
10:43the dimensions of your inner elements, then your layout is either going to break
10:46or behave unexpectedly if elements become overconstrained.
10:49So obviously the more complex the page gets, the harder it can be to accurately
10:53calculate an element's width.
10:55This can cause layouts to break, spacing to be uneven, and content to
10:59overflow element boundaries.
11:01That's why it's so important not to lose sight of how even small changes to
11:05your elements, like increasing your border by 1 pixel, can make a big difference in
11:09your entire layout.
Collapse this transcript
Understanding margin collapse
00:00One CSS concept that can really trip up new designers is vertical margin collapse.
00:05Essentially, when two vertical margins touch, they collapse to the larger of the two values.
00:11Although I covered this in the CSS: Core Concepts course, not fully understanding
00:15how it works can cause some real problems in your layouts,
00:19so it's important enough to take another look at.
00:22To do that, we are going to open up the margin-collapse.htm file, and you can
00:26find that in the 01_03 directory, which is inside the Chapter_01 folder of
00:32your exercise files.
00:34Now, just going over the structure of this page for a moment, if I scroll down,
00:38I can see that inside the HTML code we have a heading which is followed by four paragraphs.
00:44Now the bottom two paragraphs are wrapped inside of div tags, so keep that in
00:49mind that we have four paragraphs in a row but the bottom two are wrapped
00:52inside of inside of div tags.
00:54And then if I just look at the default styles, I can see that we're using some
00:57transparent backgrounds, so we can see through them.
01:00So our paragraphs have a background, the div tags have a background, and then
01:03I have a background applied to the HTML, which is the root element, which is a grid.
01:08So if we look at this in the browser before we start working with this, you will
01:12see what I am talking about.
01:12So we have this grid structure
01:14that looks a little odd, but what that's going to allow us to do, these are 10-pixel
01:17increments on this grid, and it's going to allow us to sort of really calculate
01:21the margins that we have between these elements--right now we have none
01:23obviously--and really tell when this sort of collapsing is occurring. Okay.
01:28So I am going to go ahead and go back into my HTML, and we are just going to add
01:33some margins to some of our elements.
01:35So we are going to start with our paragraph rule.
01:37So I am going to scroll down until I find my paragraph rule and just below my
01:40line-height, I am going to go ahead and add a margin. And we are going to do 10px 0.
01:43Now if you remember your shorthand syntax, the first of these two values
01:49applies to the top and the bottom of the paragraph, whereas the second value
01:53applies to the left and right.
01:54So essentially, we're saying, "Hey, go ahead and apply 10 pixels' worth of margin to
01:58the top and to the bottom of the paragraphs, but none, right and left."
02:02Okay, so I am going to save this, go back in my browser, and let's refresh that,
02:06and you can see we are getting exactly, more or less, 10 pixels' worth of space between
02:10each of our paragraphs.
02:11Now here is what's interesting.
02:12Remember, we applied the margin to both the top and the bottom of our
02:15paragraphs, so the bottom of this paragraph should have 10 pixels and the top of
02:19this one should have 10.
02:21And if our vertical margins didn't collapse, we would have a total of 20 pixels' worth
02:25of space there, but as you can see, we only have 10.
02:28So you're seeing vertical margin happen right there, and this is actually the
02:31number one reason why we have vertical margin collapse.
02:34You can see, we are only getting 10 pixels' worth of space between the two of them and
02:38that presents sort of double spacing our paragraphs, if you will, and that's the
02:41whole reason for vertical margin collapse, or at least the main reason for it.
02:45So I am going to go back into my code, and let's see what happens when we have an
02:48unequal amount of margins on the top and bottom of elements.
02:51So we are going to go back to our paragraph and just below the existing margin rule,
02:55I am going to write another margin, but this time I am going to do margin-top.
02:59And we are just going to do 20 pixels' worth of space margin top.
03:03So essentially what we are doing is we are overriding the previous top, but the
03:07bottom remains, so essentially now on our paragraphs, we have 20 pixels' worth of
03:11margin on the top of them and 10 pixels' worth of margin on the bottom. So let's see
03:16how that is resolved.
03:17So I am going to go ahead and save this, go back into my browser, refresh my
03:20page, and I can see that the larger of the two values, the 20 pixels, is the one that's being used.
03:26So if you have two margins touching each other and one is 20 pixels and one is 10 pixels,
03:30you are going to get the 20 pixels.
03:31You are always going to get the larger of the two values when the two of the collapse.
03:37Now you will notice that the bottom two paragraphs here have a slightly
03:40different background color, and the reason for that is because you are seeing
03:43the background of the paragraph and the background of the div blending
03:46together, because they are semi-opaque, which is why we can sort of see the grid behind them.
03:50Okay, but what happens when margins between child elements and parent elements
03:55react with each other?
03:57That's a little bit trickier.
03:58So let's go experiment with that. All right!
04:00So going back in our code, I am going to go down and I am going to go into my
04:05div selector and just below that I am going to type in margin, and once again, 10px 0.
04:11So, just as we did the paragraphs, that's going to give us 10 pixels above, 10 pixels
04:14below of margin on our div tags, 0 side to side.
04:17Now keep in mind our paragraph still have 20 pixels of margin on the top, 10
04:22pixels to the bottom.
04:23So if I save this and refresh this in my browser, nothing happens.
04:27Now why does nothing happen?
04:29Well, if you consider the fact that each of these paragraphs have 20 pixels'
04:32worth of margin to the bottom and our div tags have 10 pixels here, here, here,
04:38and here, the 10 pixels and the 20 pixels are touching each other.
04:40Those margins are touching.
04:42So again, they collapse down to 20 pixels.
04:45Now what's particularly interesting is I wanted you to take just a moment to
04:48visualize the space between these two div tags, if you will.
04:51This top div tag has 10 pixels' worth of bottom margin.
04:55This bottom div tag has 10 pixels' worth of top margin.
04:59So if we didn't have margin collapse, that's at least 20 pixels between the two of
05:02them; however, this paragraph inside of it has an additional 20 pixels of margin to the bottom.
05:07This paragraph inside of this div tag has 10 pixels of margin to the top.
05:12So if we didn't have margin collapse, instead of looking at just the 20 pixels' worth
05:16of the space between this, we would have 30 pixels pushing down this way and 20 pixels
05:21pushing down this way, meaning there'd be 50 pixels' worth of space between them.
05:25So now you can sort of see why margin collapse is so important.
05:29Without it, our elements would begin to really start to stretch and grow apart
05:32from each other, and it would be very limited in terms of how we could apply our margins.
05:36Now the reason that the margins between the divs and the paragraphs are
05:40collapsing are because the margins are touching.
05:42That means that there's no padding to get in the way.
05:45There's no borders to get in the way.
05:47There's nothing to interrupt those two margins touching each other.
05:50That is the only time that margins collapse.
05:53If margins have something separating them, then they do not collapse. All right!
05:57So if I go back, for example, into the code and let's say we go into our div tag
06:02again and I just come in and add a border to them.
06:05So let's just say border: 1 pixel solid black.
06:09Okay, so if I save this and test it, you can see that now we are getting our margins.
06:17We are getting the 10 pixels between the div tags.
06:20We are getting the 20 pixels of margin at the top of the paragraph and then 10 pixels of
06:25margin at the bottom of the paragraph.
06:27Those margins are not collapsing, because they are not touching anymore.
06:29They have this border separating them.
06:31So if you'd really need that spacing, you need to make sure that something is
06:36interrupting those margins from touching each other.
06:38It could be border or even it could be padding.
06:40For example, if I go back into my code and I replace border with padding, and
06:45let's say I just do one pixel's worth of padding, top and bottom.
06:48So if I save this and test it, you can see we are still no longer getting the
06:52margin collapse and we don't have to worry about the border showing up anymore.
06:56You know, the only difference there is of course we do have to account for that
06:59one pixel's worth of padding top and bottom.
07:01They do add to the overall height of our element there, so in certain layouts you
07:05may want to be careful about that.
07:06But if you are trying to prevent margins from collapsing,
07:09that's one of the easy ways to do it.
07:10You just put a little bit of padding or a border between the two of them to
07:14prevent those two margins from touching.
07:15Now vertical margins can be a little bit tricky,
07:18so if you'd like, go ahead and take this file, experiment with it a bit. Add a
07:22little bit more structure, add additional margin values, and really kind of play
07:26around and explore with how this works.
07:28The more you work with those vertical margins, the more comfortable you'll get
07:31understanding when they collapse. And there are certain instances such as with
07:34floated elements and absolutely positioned elements that we'll see later on in
07:37this title, when those vertical margins are ignored.
07:40Now the good news here is that we only have to worry about vertical margins in
07:43terms of collapsing, because horizontal margins do not collapse.
07:47Overall, just keep margin collapse in mind as you begin to plan your layout, and
07:51if you're not getting the vertical spacing between elements that you expect to
07:54be getting, go ahead and explore whether or not margin collapse is the culprit.
Collapse this transcript
Calculating em values
00:00Figuring out the value that you need for an element's width, height, or other
00:03physical properties is pretty simple if you use pixels.
00:06However, what if you are going to use a relative unit of measurement, like an em,
00:09or even percentages?
00:11Well, in those instances it can sometimes be tricky to accurately calculate the
00:15computed value of the element's properties.
00:17Now understanding how this works is critical to creating certain types of
00:20layouts, so it's really important for us to take a closer look at it.
00:23Now, if you remember, an em is a relative unit of measurement that is equal, 1 em
00:27is say equal to the default font size of an element.
00:31Now if you need to review what an em is or any of the different units of
00:34measurement that are available to us in CSS, be sure to go back to the CSS: Core
00:38Concepts title and review the movie on units of measurement where I talk about
00:42them in a little bit more detail.
00:43Okay, so for this example we are going to be using the em_values.htm file. You
00:47can find that in 01_04 directory. And I just want to give you a brief overview of
00:52what's going on here on the page.
00:53Okay, so if I scroll down into the body tag of the page, I can see that I have
00:58an article with a heading inside of it, and then I have two sections.
01:01The first section has a class attribute of em, and these sections are made up of a
01:04heading 2 followed by some paragraphs.
01:06The second section has a class property of percentages, and then we have a
01:10footer down here at the bottom of it all.
01:12Okay, now if I scroll back up into my CSS, I can see that on the HTML selector.
01:17I have a background applied that is that 10x10-pixel grid that we were using in the last movie.
01:23So that's going to help us visually sort of understand how these em values
01:27are being calculated.
01:28And the other thing that we have going on here is that for our body selector
01:32currently we have a font size of 100%.
01:35Now what that's doing is it's going up to whatever user agent or browser,
01:38smartphone or tablet or whatever is viewing this page, and it's saying, "Hey,
01:42whatever your default font size is, go ahead and give me 100% of that."
01:45Now in most browsers, that's going to be about 16 pixels, but since adding 16 pixels
01:50to 16 pixels gets confusing for me after a while, I am going to change that value.
01:53So change that value from 100% to 10 pixels, that's a little bit easier.
01:57It gives us a nice little baseline to use, in terms of our font sizes.
02:01Okay, I am going to scroll down to about line 41. And I have some placeholder
02:05selectors there for heading 1, heading 2, and paragraphs,
02:08so we are going to start with those guys.
02:10Since, ems are relative to font size, that's typically where I like to
02:13start describing them.
02:14So with h1, I am just going to do font-size of 3em; for h2, I am going to do
02:20a font-size of 2ems; and for paragraph, I am going to do font-size of 1.5em, all right.
02:30And we'll save that. So how is this value going to be calculated?
02:34Well, essentially anytime an element has an em used as a unit of measurement in
02:39a font-size declaration, it's going to go to that element's parent and it's
02:42going to say, "Hey, what is your font size?"
02:44And it's going to say, "Okay, fine. Well give me three times that or two times
02:47that or 1.5 times that," based upon whatever value you have given for the ems.
02:51Now since, we applied 10 pixel's worth of font size to our body tag, that's now being
02:56inherited by all the elements on the page.
02:58So essentially the section tag that these guys are sitting inside of has a
03:02font size of 10 pixels, and that's what this value is going to be relative to.
03:07So if I save this and test this in a browser, you see that our font size now is
03:12relative to the 10-pixel value that's been applied to all the elements on the page.
03:16Now that is what happens when you apply an em value inside of font size.
03:21What happens when you apply or use ems on any other property?
03:25Well, then you have a slightly different way of calculating that value.
03:28Let me show you what I mean.
03:29I am going to go back to my heading here and I'm going to apply a margin-bottom
03:35of 1 em to that. And I am going to copy this value because I'm going to apply
03:40the exact same value to my h2 and in my paragraph.
03:43Now, you might think, okay, fine, this is going to give us the same margin on
03:47our heading 1s that we are going to have on our heading 2s, that we are going to
03:50have on our paragraphs, but you'd be incorrect, because remember, ems are
03:54relative units of measurement.
03:55If I save this and preview this in my browser, you can see actually that I am
04:00getting about 30 pixels' worth of space after the h1.
04:03I'm getting about 20 pixels' worth of space after the h2, and I am getting about 15 pixels'
04:07worth of space after paragraph.
04:10Well, now that actually makes sense, if you think about it, because anytime that
04:14you use ems as a unit of measurement for any property other than font-size, so
04:18margins, padding, border, whatever, what it does is instead of looking to the
04:22parent, it looks to that element itself.
04:24It basically says, "Hey h1, what's your font size?
04:27Okay, I want one of those.
04:28Hey h2, what's your font size? Okay, I want one of those."
04:32So in this case, if this is calculating to 30 pixels, 1 em is worth 30 pixels.
04:36If this calculates to 20 pixels, 1 em is worth 20 pixels, and so forth and so on.
04:42So remember, if it's used on font-size, it's relative to the parent's font size.
04:46If it's used on any other property within that element, it's relative to that
04:50element's font size. Now that's true even if it's not declared.
04:54Let me show you what I mean.
04:55So if I come right after my paragraph, I can write a class selector for em, and
05:00I am just going to write another shell here for percentages.
05:04Now one of the great things about using relative units of measurements are that
05:08they are sort of platform- independent if you will.
05:11So, for example, if I am told my paragraph, my body copy, to be 1em, on a
05:15browser, if the browser is using its default font size, that might be 16 pixels,
05:20but if the same thing is opened up on a smartphone, well, then the smartphone's
05:24default font size is going to be used at 1 em.
05:26So you can see how valuable it is to be using relative units of measurement.
05:30You are not tying your page to any one specific size that might not look correct
05:35on a specific platform.
05:37Okay, so if I go into my ems, for example, here, I might say, give me a padding
05:41of 1 em and for percentages, let's do the same thing, padding of 1 em.
05:46Now I haven't explicitly declared a font size for either of these items;
05:50however, because we went ahead and applied 10 pixels to the body that's being
05:57inherited by each one of these elements, so we should end up with 10 pixels' worth of
06:00padding on both of these. So if I save this and test it, you can see that's
06:04exactly what happens. We get 10 pixels here.
06:06We get 10 pixels here.
06:07But remember, ems are relative, so if I come into the ems section and inside
06:13that I change the font-size there to 1.5-- and here is what's really interesting--
06:18not only is the padding going to get bigger, so we should see more padding on
06:23the em than we have on percentages, but everything inside of it is going to be
06:26slightly larger, because remember, this heading 2 and this paragraph for
06:31their font-size, they are both going to their parent elements and they are
06:33saying, "What's your font size? Okay, I want two times that, or 1.5 times that."
06:37So inside the percentage section it's still at 10 pixels, but now in the em section
06:42that's actually a little bit larger. Same thing with their margins. Same thing
06:45with all those values.
06:46They all should scale up in a relative fashion.
06:48So if I save this and test this in the browser, you can see that's exactly what happens.
06:53Everything that's inside of that section gets a little bit larger--1.5 times
06:57the size it was before actually--whereas, everything inside the percentages stays the same.
07:01So that's the value of using a relative unit of measurement.
07:04It's very easy to make a global change to a top-level object like that and
07:07have the relationship or the sizing relationship of all the elements inside of it stay the same.
07:12I really doubt that you are going to create an entire layout using nothing but ems.
07:15Now those layouts do exist and people do do them, but they're fairly rare.
07:19It's actually a lot more common to use ems for margins and occasionally padding
07:23for specific elements, and it's really common to use for font size.
07:26Now regardless of where you are going to be using ems, it's important that you
07:30understand the rules that the browser is going to use for determining what the
07:33calculated value will be, so that your layout and then all of the elements
07:37within it look the way that you intend them to.
Collapse this transcript
Calculating percentage values
00:00Even though percentage values are always defined as being relative to another value,
00:05they're still defined separately in the CSS specification, meaning they are not
00:09grouped with other relative units of measurement;
00:12they are just sort of defined by themselves.
00:14Now one of the reasons for this is that percentages have their own very unique
00:18rules when it comes to how their values are calculated.
00:22To use percentage values in your layout with confidence, you're going to need to
00:25understand exactly how their values are computed.
00:28So if you are going to do this along with me, go ahead and open up
00:31percentages.htm, which can be found in 01_05 directory.
00:37This is exactly the same file that we used in our last movie,
00:41so if did the calculating em values with this--let me just go ahead and
00:45just preview this--you can see it's pretty the same file. Same structure, same everything.
00:50Now I am going to jump back into my code here, and let's talk about this for a moment.
00:55If I scroll down into my body, you can see that currently right now the width of
00:59this page is set to 960 pixels.
01:02Well, let's just go ahead and set that to 80%, so I am going to take it to 80%,
01:07go ahead and save that, and if I go back into my browser and refresh that, you
01:11can see that the width of the page changes a little bit there.
01:14You know the other thing that it does for us is now the width is relative to the viewport.
01:19You can see that resizing the page is causing those elements to flex because
01:24the actual size of the page is flexing, and if there's not enough room for these
01:27elements, you can see it's dropping the elements down, based upon how wide the page is.
01:32Now, let's talk about percentages and how they are calculated.
01:35If you use percentages on box model properties, like width, padding, and
01:40margins, then those values, they are calculated relative to the width of the parent element.
01:46Think about our body for just a second.
01:48The body is relative to the viewport, so that 80%, we're getting 80% of
01:53the available viewport.
01:54That means that any time you set percentage on one of those properties, it's
01:58going to be that percent of the element's parent.
02:01Now that's pretty easy when it comes to width, because you are saying, hey, I
02:04want to be, you know half the size of the element I am sitting inside of.
02:07But it does get a little bit harder to remember that when you're doing padding
02:10and margin, so let's go ahead and kind of experiment with that a little bit.
02:13Okay, I am going to take a look at these two selectors right here: em and percentages.
02:16Remember, those are those two sections that we were looking at earlier.
02:19And I am going to change the em's width to, say, 60%.
02:24And then I'm going to take the percentages and I am going to change its width to, say, 30%.
02:30Now, 60%, 30% of course is 90%, right?
02:35But that's not 90% of the viewport.
02:38If I scroll back down, for example, I can see that this section with the class
02:43attribute of em and this section with the class attribute of percentages,
02:47it's actually sitting inside of this particular article tag.
02:51So essentially, that 60% and 30% are relative to the width of this article. Not even the body.
02:58Now the article as a block-level element is getting its width from body, because
03:02we haven't explicitly gave article a width yet,
03:04so it's basically sort of expanding to fill the whole body tag.
03:07So essentially, that 60% and the 30%, they're really asking for a 60% of
03:13the body or 30% of the body, because we don't have an explicit width set for article.
03:17So if I save this, go back in my browser and refresh that, you can see that
03:24those values change rather dramatically. And now, if I resize the page, you can
03:29see that they are basically changing their values, but they are still staying
03:33relative to the fact that this is 60% of the available space within the body and this is 30%.
03:40The body is 80% of course.
03:42So it's actually 60% of that 80%.
03:44Now if you go too far down that rabbit hole you can drive yourself crazy; just
03:47remember, it's 60% of its parent element.
03:50So now, I mentioned earlier that it gets little trickier when you start
03:55talking about margins and padding and things like that. Let's take a closer look at that.
03:59Go into both of these selectors here for the em and the percentages, and I am going
04:03to go ahead and give them a padding of 1%. Same thing for percentages;
04:08I am going to give it a padding of 1%.
04:12Now one of the most difficult things to do when you're working with a layout
04:16like this, and we are going to talk more about flexible layouts defined with
04:19percentages is little bit later on--
04:21we have a whole chapter on those as a matter of fact--
04:23but one of the most difficult things about calculating these values is exactly
04:27how much do I need to get the visual amount of padding that I want for this?
04:31And one of the things that you might want to consider doing is thinking about
04:34starting with sort of preferred size if you will.
04:37Even though we've stated this to be 80%, you know if think that the preferred
04:41size of it is at 950 pixels, for example, I can say okay, at 960 pixels, I want
04:4710 pixels' worth of padding, all right.
04:48So then you look at 10 pixels on one side of an element and 10 pixels on other
04:52side and you can add that up to 20.
04:54You can say okay, 960, if I take a look at 960, what percentage of that is 20?
05:01That calculates out to about you know 1%.
05:04So if I save this and test this, you can see now we are getting you know, at 960
05:10pixels we're getting roughly 10 pixels' worth of padding, which is kind of cool.
05:14And if I resize that, notice that that value gets smaller as I get smaller and
05:19as the page gets bigger, that value also gets a little bit larger based upon the
05:23available size for it.
05:25Now if your calculations that you do happen to turn up decimal points every now
05:29and then, don't be worried about using those.
05:31Browsers can handle decimal points really, really well.
05:33So if I come and say 1.4% instead of 1% and test that, you can see, it increases
05:41just fine. Nothing breaks. The computer isn't smoking.
05:44There is no fire coming out of it.
05:46It can certainly handle that type of math. That's not an issue.
05:49I am going to go in and talk a little bit about what happens when you start
05:53dealing with some of these interior elements.
05:55So if I scroll down, I am going to go down to this h2 selector here and I am
06:00going to change it's margin-bottom value.
06:03I am going to set that margin-bottom value to 5%.
06:05Now the reason I'm doing this is because I want to illustrate the fact that
06:10anytime you use percentages on a box model property, in terms of calculating the
06:15value of that, it calculates relative to its parent.
06:18So this h2, remember is inside of this section, which is em, and it's inside this
06:25section, which is percentages.
06:27Remember, em is set to 60%, so it's available width is a bit larger than
06:31percentages, which is set to 30%, so it's available width is smaller.
06:35So if this h2 is giving me, say, 5%, it's going to give me 5% based on that 60% size,
06:45whereas the percentages is going to give me that 5% based on its 30% size.
06:49So if I save this and preview it, indeed you can see that I'm getting more
06:55bottom margin here on this h2 than I am on this h2.
06:59It has everything to do with the relative size of those elements.
07:02And if I shrink that, notice that the margin is shrinking.
07:06If I expand it, notice that the margin begins to expand as well.
07:10Another thing that I want to point out to you guys here. We don't really
07:12need to illustrate this.
07:13I just want to talk about it. Percentages should not be used for border width.
07:18This is actually per the W3C CSS specification.
07:21They are not allowed.
07:22If you go ahead and use them anyway, well, what the browser will do is it's
07:26just going to display whatever the default border width is, regardless of which value you used.
07:30So you could say 400% on a browser border width value and you still just get
07:34the default border.
07:35We are going to have a chance to put what we've learned here about
07:37percentage values to the test a little bit later on when we create
07:41percentage-based flexible layouts.
07:43Now that you know how browsers calculate percentage values, you'll be better
07:46prepared to plan out element sizing when working with those layouts.
Collapse this transcript
Normal document flow
00:00When people first learn how to control page layout with CSS, they're very eager
00:04to learn about things like floating, using positioning, and other details about
00:08page-layout techniques.
00:10While this is understandable, one of the unfortunate side effects of this is
00:14that people often don't pay enough attention to one of the most important page-
00:18layout concepts of them all, and that would be normal document flow.
00:22Normal document flow is exactly what happens to your page when you do nothing at all.
00:26It's the rules that all browsers use to control the appearance of elements on the page.
00:31It's also more powerful than most people think,
00:33so let's take a closer look at normal document flow and how it relates to page layout.
00:38And to do that I've got the normal.htm file opened, and you can find that in the
00:4301_06 folder in your exercise files.
00:47Now the first thing I want to do is just take this page and open it up in the
00:50browser so you can see what it looks like with absolutely no styling applied to it whatsoever.
00:55Okay, so this is the page with just the browser's default styling applied to it.
00:59We have a headline up top,
01:01we have a photo gallery with about six photos in it, and then we have little
01:04section down here where I expound a little bit on normal document flow and
01:08kind of what it is.
01:09So essentially normal document flow says that elements are going to appear at
01:13the top of the page, they are going to read left to right and they are just
01:16going to stack one on top of another.
01:18So paragraphs are going to stack on top of each other.
01:20Headings, those block-level elements will occupy their own space within the layout.
01:24So here is the heading followed by another heading, followed by a paragraph.
01:27And then in-line elements, like these images for example, will appear on a line
01:31until they can no longer fit, and then they'll break down to the next line.
01:35So this is normal document flow in a nutshell.
01:38Now, I want to show you what we can make the page look like by just applying a
01:42few block-level properties that we already know how to control.
01:46So I am going to flip over to the finished version of this, and we are going to
01:49do this in just a moment.
01:50And this is what you can do to the exact same content through just the use of
01:54CSS styles with no "positioning properties," only the box model properties. No
01:59floating, no positioning, nothing like that.
02:02Okay, so I am going to jump back in our code, and we'll go ahead and get started on this.
02:05All right, so the first thing I want you to do is to take off the comments.
02:09So I've got some code already commented out.
02:11If you look at about line 11, that forward slash star, that's the beginning of a CSS comment.
02:17I am going to get rid of that.
02:18And then I have to go down to the very bottom of our styles and get rid of
02:22the star forward slash.
02:23So I've just gone ahead and uncommented those out.
02:26Now before we go back up and begin working on our styles, let's take a look at
02:29the structure of the code, so we'll know what it is that we are controlling.
02:33So we have a header and inside the header element we have a single h1.
02:38Then we have two articles. The first article has a class with attribute of
02:41gallery and inside that we have a heading, a paragraph.
02:44And then we have this div tag right here but with a class of photos, and that just
02:50has that series of images inside of it.
02:52So we have six of those images. Notice they are each 250 pixels wide.
02:56And then when you're using the div tag as a wrapper to help us control that, so
03:00we are going to control exactly how many of those images fit on a line by
03:03controlling the size of the outer wrapper around it.
03:05Following that, we have another article. Its class attribute is normal and it
03:09just has some explanatory text that goes into normal document flow and is
03:14certainly worth reading through as you work on the exercise.
03:17Okay, so I am going to go back up into my code.
03:20Now a lot of it, I kind of already pre- populated to save us a little bit of time,
03:24so I am just going to start from the top and talk a little bit about what the
03:27existing code is doing.
03:28Our first line is telling any browser that doesn't support the newer HTML5
03:33elements how those only should be displayed and the fact we want them treated as
03:37if they are block-level elements, so we're telling the browsers to do just that.
03:41Most modern browsers you won't have to do that with, but just in case somebody
03:44is happening to use an older browser, that line of code comes in very handy.
03:48After that, we're stripping off default margins and padding for just a serious
03:53of elements, the div tags, headings, paragraphs, and image.
03:57This is something that's called a CSS reset and we are actually going to take a
04:00closer look at CSS resets in their own movie in just a little bit.
04:04For the time being, just understand that we are sort of telling the browsers,
04:07hey, if you are already putting margins and padding on these elements, don't.
04:10We want to control them on our own.
04:12Following that, we do the same thing to the HTML tag, which is a root-level
04:16container of our file, and we're applying a background color to that.
04:20And for our body, we're applying a little bit of padding.
04:23Notice we are applying 25 pixels' worth of padding to the bottom.
04:26That's just to hold the content on the bottom away from the bottom just a little bit.
04:30Applying just a slight bit of typographic formatting here, where we are saying, hey!
04:33Go ahead and give us your default font size and we are going to use Georgia, and
04:37then we are going to give it a background color as well.
04:39All right, well let's add to the body selector.
04:41So, on another line inside of our body selector I am going to go ahead and set
04:44the width of the body's content to 900 pixels.
04:49So doing that, we're basically constraining all of the content on the page, and
04:52we're saying we only want it to occupy 900 pixels wide.
04:56Now the padding gets added to that, if we had padding on the right and left
04:59sides, but we don't,
05:00so this is going to be constrained to just simply 900 pixels wide.
05:03Right now, on the next line, go ahead and apply a margin and here for margin we
05:08want to do zero for margin top and bottom, so no margin top and bottom, and then
05:13auto left and right.
05:14Okay, so what this does for us is this is a very simple technique for centering
05:18content on the page.
05:20So by saying no margin top and bottom, we're just sort of making sure that the
05:23content adheres to the top of the page. And for when we say auto for left and
05:27right, we're saying okay, the content needs to be 900 pixels wide,
05:31but on the left side and on the right- hand side, if you have any space left over,
05:35go ahead and split that space and put it equally on the left-hand side and the
05:38right-hand side, and that has the result of centering the content on the page.
05:43Okay, I am going to keep going down. And for our header element, we're going to
05:46apply a few properties to that as well.
05:48The first I want to do is just a little bit of padding, so I am going to type in padding.
05:52I'll 20 pixels' worth of padding for the top and the bottom, and then I'll do
05:5625 pixels' worth of padding for the left and the right, so 20 pixels, space, 25 pixels.
06:01That will give me a little room above and below
06:04at the top and the bottom of the header, and it will give me a little room
06:07side to side as well.
06:08And then we are going to give it a background color, so I am going to type in background:
06:12rgb, and the color I am going to give this is 76, 67, 65.
06:15And it's sort of that dark sort of brown gray color, if you will.
06:22I call it ash, but I don't really know what it is.
06:24So now we've given a background color to our header, and we've got a certain
06:27amount of padding for us here.
06:29Right now I am going to go down to my article.
06:30Now remember, we have two articles on the page: the top article has a photo
06:34gallery and then the bottom article just has the sort of informative text about
06:38the normal document flow.
06:39So this is going to be very generic styling to sort of style both of those regions.
06:43So the first thing I want to do is type in some padding, and I am just going to
06:47do zero padding top and bottom and 25 pixels' worth of padding left and right.
06:51So it kind of mirrors what we are doing with the heading, as far as the left
06:53and right padding goes, and assures that content will sort of lineup, but in the
06:56header em and with those articles.
06:58And then one last thing we are going to do to article is we are going to do is a
07:00margin-top, and here we're going to do a top margin of 25 pixels.
07:05What that will do is it will create a little bit of space above each of the
07:08articles, 25 pixels' worth of space, to give separation between the headers and
07:11their articles and the two articles themselves, okay.
07:14So I am going to go ahead and save this and before we start working on our
07:16galleries, I want to preview it.
07:18So I am going to go back into my browser and I want to preview the changes we've made.
07:22Okay, so that's a lot of changes.
07:23Now I know a lot of the work was sort of already done for us, but look what we've done.
07:26We've centered our content on the page.
07:28We've restricted it or restrained it,
07:30I should say, to 900 pixels wide from side to side.
07:33Here is the 25 pixels' worth of padding that we've placed already coming into
07:37play, and it's looking fairly decent.
07:39So the next thing we need to do is we need to work on our photo gallery.
07:42Now one of the things I want to do for our photo gallery is I want to make
07:45sure our photos are displaying in two columns of three, and right now they're doing that.
07:50And the only thing that's controlling that right now, there is no floating
07:52that's controlling that.
07:53There is nothing controlling that other than the constraints that we've already
07:56placed on the content.
07:58Remember, these images are 250 pixels wide.
08:01Our page is 900 pixels wide.
08:03So there is a limit as to how many of these guys can fit on a single line.
08:07Because images are in-line elements, meaning they stack from right to left until
08:12they get to the end of a page or the end of the available room for content and
08:16then they go and form the next line, we don't really need to do anything fancy
08:19to get two rows of images;
08:21we merely need to restrict the space that they are in.
08:23Since they are already sort of restricted, what we are going to work on now is
08:26sort of styling that photo gallery so that it looks maybe a little more
08:29presentable than we have it right now.
08:31Now one of the things that we can do for that is remember we have the div tag
08:35that wraps around those images, so by controlling that div tag, we can sort of
08:39create a defined visual region for those images.
08:42So I am going to go back in our code, and that is the first we are going to
08:45tackle is this photos.
08:46Remember, that's the div tag that surrounds those guys.
08:49So what I am going to do for that is I want to type in margin: 0 and then auto.
08:53So we are doing the same thing for the photos region that we did for the page
08:56itself; we are going to center it within its container.
08:59And the photos container is that body tag, so this should be centered within
09:03that single-column layout that we have going on.
09:05The next thing I want to do is define a width for it as well.
09:08So I am going to choose a width of 822 pixels.
09:12Now why 822? It sounds kind of how weird. Bear with me. We'll get into that when
09:17we start talking about the widths of the actual images themselves.
09:20The next thing we want to do is we want to do some padding, and we are going to
09:23give it a padding of 10 pixels all the way around, and then we are going to give
09:27it a background. And the background we are going to give this is rgb (100, 98,
09:30102), and that's kind of a gray, if you will.
09:37All right, so I am going to save this and again sort of update my browser.
09:42You can see what this is doing for us.
09:43It's actually right around 842 total pixels of width, because of the padding on
09:48either side, but you can see that this is the region that we want our photos in.
09:51We've got padding, 10 pixels' worth of padding going around.
09:54And now what we want to do is we want to sort of space our images in this
09:59area so that they have a little bit of space between them and that they are centered as well.
10:02Now by defining the width the way we did, we've actually helped ourselves out a little bit.
10:06And I'll be honest with you.
10:07When I plan this out and when you plan something like this out, you are going to
10:11approach it from the images first and then figure out how much space you need.
10:14So let me tell you how I did that.
10:16Okay, so our images are 250 pixels wide each.
10:20I want to get about 20 pixels' worth of space between each of them.
10:23So what I am going to do is I am going to 10 pixels of margin around the images.
10:28Now, vertical margins collapse unless you are dealing with in-line boxes the
10:31way we are already here with images, so I'm not
10:33actually having to worry about collapsing margins.
10:35I can just go ahead and put a 10-pixel margin around these images and I'll know
10:39that I am going to get 20 pixels' worth of spacing.
10:41I have the padding going all the way around the interior of the div tag too,
10:44so the 10 pixels of margin on the outer edges of images will combine with that
10:49padding on the div tag to give me 20 pixels' worth of space there as well.
10:52I am also going to go ahead and put a two-pixel wide border on my images, but I
10:56can't ignore that two pixels when it comes to the total width of the images.
11:00So if you have a calculator handy, you can look at it this way. Each image is
11:05now 274 pixels wide.
11:07We've got 10 pixels of margin on each side and the two pixels of border on both sides of it.
11:12If I multiply that times three, I get 822, and 822 with the 10 pixels' worth of
11:17padding on either side gives me the total width of 842.
11:20So math, maybe we should have paid more attention to it on our school, I don't know.
11:24All right, I am going to go back into my browser, and let's go ahead and set
11:27those values on our images and see what we get.
11:30All right, so the first thing I want to do with my image is just give it a
11:32margin of 10 pixels, and then the next thing I want to do is apply that border to
11:36it, so a border of two pixels, solid, and then white.
11:40I am going to save this.
11:41I'll preview it in my browser, and as if by magic, we get exactly the spacing,
11:46we are looking for.
11:47We get our borders around it, which helps offset the photos a little bit.
11:50And because we carefully planned the width of that, we get equal spacing all the
11:54way around and we get a nice little container there with our div tag.
11:57That is very, very nice.
11:59Now, I know that this layout is very, very simple,
12:02but it is a perfectly good, single- column layout, with no styling required,
12:06outside of the simple box model properties that we've been controlling in
12:10order to make it work.
12:11You know, when I first started out controlling page layout through CSS, I'll be
12:14honest with you guys. I really, really overdid it.
12:17Now I was trying to float or position almost every single element on the page.
12:21I thought that somehow if I didn't, I wasn't really controlling my layout
12:25through, you know, positioning.
12:26Oddly enough, the more experience I gained through doing page layout, the more I
12:31began to rely on normal document flow to do the majority of the work for me.
12:36You know, at this point, I try to do as much possible with normal document
12:39flow and then just merely tweak the layout using floats or positioning when required.
12:43Now this approach uses less code,
12:45it's more likely to be compatible across a wider array of devices and browser
12:49versions, and requires less work on my part.
12:52So, I know the focus of this title is on page-layout techniques, but as we
12:56work, pay attention to how important normal document flow is to the majority of
13:01our layouts.
Collapse this transcript
Controlling element display
00:00The display property is by far one of my favorite CSS properties.
00:04Now when I first learned about it, I remember thinking to myself, "There is no
00:07way you can really do that, can you?"
00:10So what does it allow you to do?
00:11Well, the display property allows you to change how an element displays
00:15within the browsers.
00:17Block-level elements can become inline.
00:19A div tag can display as a table cell, or you can even tell elements to
00:23not display at all.
00:24As I'm sure you can imagine, controlling how an element displays within the
00:28browser is one of the most powerful CSS capabilities a designer has when
00:32creating page layouts. It's also surprisingly simple to do.
00:36So what I have right here is I've got the page that we are going to be working
00:39on, I've got it previewed within one of my browsers.
00:42And I did that because I wanted to show you what the possible values for the
00:46display property are, and talk about them a little bit.
00:49So, this little group of values right here are the options that we have when
00:52setting the display property.
00:54We have inline, block, list-item, inline-block, and then a whole host of table-
01:00related properties as well.
01:02Then we have none, and we have inherit, which basically just says, hey, whatever
01:05the display property of my parent element is, that's what I want as well.
01:09Okay, so if you want something to display as an inline-level element, such
01:12it's an image or a span tag or you know just an element within a line of text,
01:17you would use inline.
01:18You want it to display as a block-level element, like a div tag or paragraph or
01:22heading, then you'll use block.
01:24List item would display as a single item within a list.
01:27Inline-block is really interesting.
01:29We are going to take a look at that in just a moment.
01:30What that allows you to do is to display elements as inline elements but
01:34still be able to control their box model properties as if they were block-level elements,
01:38so that's a very interesting one. These table ones,
01:41this is really sort of outside the scope of this course,
01:44but what they allow you to do is to take an element and essentially display it
01:49as if it were a table cell or a table row.
01:51You can even create CSS table-based layouts by using that, and that's something
01:55that we are going to be doing in an upcoming title.
01:57So with all of that sort of in mind, we've got some elements down here, this
02:02list, we've got some images, we have some headings down here that we are going to control.
02:06So we are going to go ahead and experiment with a lot of these
02:08different properties.
02:09So I am going to jump into my code.
02:11You can find that in the file display.htm, which is found in the 01_07 directory
02:19in your exercise files.
02:20Okay, so I am going to scroll down into my code and you'll see at about line 61
02:27or so we have little area in our styles where we can begin to add some styles.
02:31Now to just again, talk about the structure of this a little bit,
02:34we have a list here that we are going to control,
02:37we have these images right here that we are going to control just underneath
02:40this headline, and then we have some headings down here that we are going to
02:43change to the display on as well.
02:45Notice that they all have the inlineB class applied to them as well.
02:49And then our final headline has a class of none applied to it.
02:52Okay, so just in that layer where we are going to add some styles, the
02:55first thing we'll do is we are going to change how these list items are going to display.
02:59So I am going to create an li selector and we're going to use the display
03:03property, and I am just going to tell it to display as an inline element.
03:07Now list items, those individual elements within a list, by default they display
03:11as block-level items, meaning they stack one on top of another.
03:14So if I save my file, go back into my browser, and refresh it, you can see that
03:19the list now displays as inline elements.
03:21They are all on the same line.
03:22So they are indeed individual elements, but the bullets go away and they all
03:27display as inline elements.
03:28A lot of people use this technique to create menus out of lists where they have
03:32links in them, and that way you can create horizontal menus. Just add a little
03:36bit of spacing between them now, you have everything on a single horizontal line.
03:40Next up, let's take a look at our images. Right now they're all displaying as
03:43inline elements, meaning they're stacking right to left right next to each other,
03:48and are they on the same line because they have enough room to fit.
03:50If we had more images they'd break down to another line.
03:52Okay, so I am going to go down into my code and right after the list item I am
03:56just going to do a selector for image.
03:58And for image I am just going to say display: block. And then just to help us
04:02out with spacing a little bit, underneath that I'm going to do a margin-bottom on them of 1 em.
04:10So if I save that and preview my page, you can see what happens to our images.
04:15They now display, each on their on line because they are now block-level elements,
04:19and that margin between them gives us the spacing that we are seeing there.
04:23Okay, so now I am going to go down to these headings here.
04:25With these headings what we're going to do is we are going to experiment a
04:28little bit with the inline block display property.
04:31And this is the one that I really like and I've been using a lot lately.
04:34Well, what this allows you to do is to take block-level elements and make them
04:37display on the same line as if they were inline-level elements.
04:40The downside to telling block-level elements to display as inline-level elements
04:44is that you start to lose control over some of the box model properties, simply
04:48because of the very nature of inline elements.
04:51But displaying something as an inline- block element sort of allows you to still
04:55control most of those block-level properties, and I'll show you what they mean in just a moment.
04:59So I am going to go into my code and we are going to write another selector, and
05:03this selector is going to be for the inlineB class selector, so .inlineB. And
05:09what we are going to do here is we are going to do display: inline-block and then
05:15just to help us out with our spacing, we are going to give it a margin right and
05:19we are going to do a margin right of one ems.
05:21So we are going to save that and when I preview that, you can see that now these
05:25headings are displaying on one single line as if they were inline-level elements
05:28and then the margin that I am giving them is helping space them.
05:31Now remember what I said earlier about being able to control certain block-level properties.
05:36We can still do that.
05:37So if I go into my code and create another rule, let's just say .tall, so
05:43we'll just create a class called tall, and we'll go ahead and give that a
05:46height of 200 pixels.
05:48So if I save this and then go down to the very last heading within that group
05:54and also apply the class of tall--now if you've never done this before, if you
05:57want to apply multiple values to a class attribute, you just separate them with a white space.
06:00So, inlineB tall. If I save this and test it, you can see that our third one is
06:08now 200 pixels tall and then while it has pushed the other content down, it
06:11didn't affect these guys at all.
06:13So you were allowed to control these individual box model properties that might
06:17be a little more difficult otherwise.
06:18Now I want to turn our attention to our last element here. You can also tell
06:22elements not to display as well.
06:24Now I am going to take a break from just using only the display property
06:27for just a moment, because I want to compare and contrast this method of
06:31causing something to disappear with another CSS property, and that property
06:34would be visibility.
06:35Okay, so this h4 class has a class of none.
06:38The first thing I want to do is go back up and write the class selector for
06:42none, and I want to compare and contrast methods of sort of making this content go away.
06:47The first one I want to use is the visibility property.
06:50Now visibility allows us to set three properties on it.
06:54We can have--all right, so we have visible, hidden, or collapse.
06:57Now collapse deals with table rows or table columns, so if it's not applying to a
07:02table, then collapse is the same as hidden.
07:04So if I say hidden and save it and go back and preview this again, you can see
07:11the element goes away.
07:12And what I want you to notice about this, however, is that even though I told
07:15the visibility be hidden, notice that the space that the element was taking up is still there.
07:19Okay, so that's one of things that the visibility hidden property does for us.
07:23It does hide the element visually, but the space that the element would take up
07:26in the layout, and how it's affecting other elements, remains.
07:29So if we were to increase, say, the height of that, for example, the rest of the
07:32layout would react to that.
07:33Okay, now I want you to contrast that with using the display property of none.
07:40So if I save this and preview it, you can see what happens.
07:44That element is gone.
07:47Not only does it not display, but it's physically removed from the layout, and
07:50it's no longer affecting any other elements, and other elements aren't affecting it.
07:54It is as if it were never there.
07:56Now one thing I do want to point about this is that even though the
07:58element is not there visually anymore,
08:00it's still available through the document object model.
08:02So, that element would still be available for scripting and you get still
08:05affect it through that.
08:07Another thing I want to point out too: if you were thinking about using this as
08:09a method of maybe hiding text and then putting something in its place, screen
08:15readers, accessibility devices, are actually programmed now to ignore objects
08:20that have display of none.
08:22So if you are wanting a screen reader to still read this content while maybe
08:25not displaying it visually, this probably is not the method that you want to be using.
08:29So as you can see, the display property gives us an extremely powerful way to
08:33control these elements on our page.
08:35Now like almost all the CSS capabilities that we are talking about, it's just a tool,
08:39and like most tools, you are going to need to decide if it's the right one for the job.
08:43Now later on we are going to use the display property to enhance various layouts
08:46that we are going to be working on, and we are going to explore different ways to
08:49use it within the context of a wider layout strategy.
Collapse this transcript
Using CSS Resets
00:00In many cases, how you structure and write your CSS will be determined by your
00:04personal design philosophy.
00:06Nowhere is this more true than whether or not you're going to use a CSS Reset,
00:11and if you do use them, what type of Reset that you are going to use.
00:14Now if you are not familiar with them, CSS Reset is a term that's used to
00:18describe a collection of styles that are designed to neutralize a portion of a
00:22browser's default styling.
00:24Now they come in a wide variety, with varying degrees of complexity and functionality.
00:28Let's take a look at some of the practical applications for using CSS Resets, and
00:31after that I am going to leave you with some additional resources; that way you
00:34can learn a little more about them.
00:36So I have the Reset.htm file open and you can find that in 01_08 directory,
00:43and I am just going to create a simple little CSS Reset.
00:46Now just to show you kind of what it is that we are going to be going, if I
00:49were preview this page in the browser right now, I can see that there's no
00:53styling going on, on my part, but what I am looking at are the browser's default stylings.
00:58So all those spacing here, the space between the paragraphs and the
01:01headings, all the margins that you are looking at, the font size, that sort of thing,
01:04that's all controlled by the browser's default settings.
01:08Now if I'm going to go ahead and style this page, as a general rule, I will
01:12probably end up overriding most of those, but in some cases I might not.
01:16Sometimes that's helpful, and sometimes it might cause some styling issues, or
01:19there might be some conflicts between my styles and the browser's default styles
01:23that I didn't anticipate.
01:25So the nice thing about a CSS Reset is they just sort of go ahead and give you a
01:28baseline to start from that goes ahead and takes away, or sort of zeroes out if
01:34you will, a lot of those default styles.
01:36Okay so I am going to get back into my code.
01:37And one thing that you always want to make sure you do when you write a CSS
01:40Reset is place it towards the top of the code.
01:43The idea is that your styles later on will sort of overwrite what you're
01:47doing with the CSS Reset.
01:49The only thing you are trying to with this is overwrite the browser's default styles.
01:52So right underneath the comment that says "add styles here,"
01:55I am just going to create a very simple CSS Reset.
01:57Now I am going to start just by grouping a lot of selectors together, so I
02:00am going to start with "html,body, div,h1,h2,h3,P,ul,li" and well
02:15that's it for right now.
02:16Now that might seem like a lot but trust me, that is a small CSS Reset.
02:20I've seen them with two to three times the number of elements sort of
02:23grouped together here.
02:25And the only thing we are going to do within the selector is we are going to
02:27just going to go margin: zero;
02:29padding: zero.
02:30Most CSS Resets are a lot more robust than that,
02:33throwing in some typographic rules, maybe even doing some things designed to
02:37promote cross-browser compatibility.
02:39So, those are all sorts of things that you can find in CSS Resets.
02:42Okay so if I save this file, go back into my browser and refresh this, you can
02:46see the effect that the CSS Reset had on this.
02:49Now that looks terrible, but what it does is it gives us that sort of starting point.
02:53I don't have to worry about the default margins and padding of any of these
02:57elements conflicting with the styles I am going to write.
02:59I am now free to write them without worrying about what the browser's default
03:03styling might do in an instance or that I might forget about a browser's
03:07default margins in certain locations.
03:09All right, now I am going to go back and show you kind of the nice thing that
03:12this Reset does for us based on looking at the fully styled page.
03:16So I am going to go back into my code, and you'll notice we have a lot of styles
03:20here that are already commented out, so I am just going to remove the front part
03:23of the comment, which is right there on line 18.
03:25I am going to scroll down to the bottom of styles and get rid of the last part
03:28of the comment right there. Now be sure not to delete the closing curly braces
03:32from a. Now, let me go ahead and save this.
03:34And again I am going to preview this in my browser. Okay, so there is my page,
03:39fully styled. It looks nice, not a bad- looking page. But what is it that the CSS
03:43Reset is actually doing for us, you know, how can we visualize what that's doing?
03:48Well, if I go back into my code and if I comment out the Reset and then preview
03:55that in another tab and you can sort of preview what the Reset is actually doing for us.
03:58So I am going to go ahead and comment out the Reset that I just did.
04:00So /* for the beginning of the comment */ for the end of the comment and
04:08remember, you just want do that right around the CSS Reset that you created.
04:11So I will save that, and then I am just going to preview that in a new tab. There we go.
04:16And now if I tab back and forth, you can sort of see the difference that we have
04:20between one layout and another. Notice one where we don't have the CSS Reset, for
04:25example. The default margin on this heading up top is causing the page to be
04:29pushed down a little bit, and we have some additional spacing issues throughout
04:32that are just slightly different that might make a really big difference in
04:36certain layouts. In some layouts you might not notice it at all; in others
04:39there might be a really big difference between having or using the Reset or not using it.
04:43Now, I think you can kind of see how beneficial Resets are, but I do want to
04:48point out that they're not without their problems.
04:50You know one of the reasons that designers are really critical of using CSS Resets
04:54is that they are pretty complex. A lot of times they require you to add a lot of
04:58weight to your styles that you don't always need.
05:00You are not always going to need everything that a lot of these CSS Resets that
05:02I am about show you are going to do for you.
05:05Now another thing to think about is how much work you actually want the browser to do.
05:09Think about what we are asking it. We are asking the browser to go through, strip out all its styling and add a
05:13bunch of styling on top of it.
05:15If your styles were already going to overwrite the browser's default styles
05:18then your CSS Reset is really just doing a lot of work for nothing.
05:21The other thing is, if you do decide to use CSS Resets, don't just go ahead and
05:26copy and paste them from somebody else's site, even the ones I am going to show you.
05:29Mow Eric Meyer even says in his introduction to his Reset that he hasn't
05:33designed it to be used as is, but to be altered and customized to meet the needs
05:37of the designer or whatever project you are working on,
05:40so really think about them as a starting point and take from them what you need
05:43and then strip out the things that you don't need.
05:45Now my own personal approach, if you are wondering about that, to using
05:48Resets, it's very minimal.
05:50All I really do is I prefer to zero out the default values for elements that I
05:53know I am going to be using in that particular site.
05:56Now for that reason, I don't have a set Reset that I use for every project. You
06:00know, even the ones I am about to show you, there isn't one of those that I
06:02just use every time out.
06:04I just have a guiding principle of about using a smaller efficient Reset if I need them.
06:08Now one of the things that I have provided for you here on the page is a series
06:11of links that can take you to different Resets.
06:13So I have Yahoo's YUI Reset, Max Design's, Eric Meyer's, HTML5 Reset, and the
06:20HTML5 Boilerplate Reset.
06:22Now you can certainly go to each of those, read to the documentation, and learn
06:26more about them, figure out what works for your projects and what doesn't, but
06:29the site that I really wanted to point out to you was CSS Reset.com,
06:33so I am going to go ahead and open that up in a new tab.
06:35This site has a lot of information about CSS Resets, what they are, some
06:39documentation on them,
06:40and then there are links to not only some of the ones that I gave you links to
06:43as well, but other ones, so you can either go ahead and just grab the code from
06:46them or read the documentation on them as well to figure out whether or not that
06:50CSS Reset is right for you.
06:52Okay, so visit those sites, read the documentation for those Resets, go ahead and
06:57dig through, explore into the code.
06:59You are going to gain a greater appreciation for dealing with browser
07:01default styles, what's involved with that, when its appropriate to use Resets,
07:05and most importantly, where the use of CSS Resets will fit into your own
07:10design philosophy.
Collapse this transcript
Fixed, fluid, and responsive layouts
00:00In this course, we are going to focus on building three main types of layouts:
00:04fixed, fluid, and responsive.
00:06Now I have got examples of all three of those layout types open here, and I
00:10want to go over the basics of each of them, as a way of introducing these layout types.
00:14So we have got fixed.htm, fluid.htm, and responsive.htm,
00:17those three files, open.
00:18I have opened them from the 01_09 directory.
00:23Now before we preview these in the browser, before we go into the CSS and
00:26take a look at the actual concepts that's driving the different types of layouts,
00:30I want to go over the structure of the actual HTML. So I am in fixed.htm and
00:34if I scroll down into the body of this document, I can see that the page starts
00:40with a header that has the headline Alice in Wonderland, and that's followed by
00:44an article which contains some of the opening part of the Alice in Wonderland novel.
00:49And then if I go down a little bit further, I can see that I have it an aside pass
00:53the article, and this aside has a personal note from Lewis Carroll in it.
00:57And at the very, very bottom we just have a simple footer.
01:00Okay so that is the structure that is used throughout all three of the pages.
01:04So the only thing that's going to change when we preview these pages in the
01:08browser and we take a look at how they respond to changes within the browser,
01:12it's important to remember that the underlying structure of the page of HTML is
01:16exactly the same. Same class attributes, same structure, same elements; nothing
01:21changes other than the CSS.
01:23Okay, so we are going to start with the fixed.htm.
01:26So I am going to go over to browser and preview that page.
01:28And here we have our layout.
01:29Here is our header. We have got a simple two-column layout. If I scroll down, we
01:34can see at the very bottom there is our footer down there.
01:37So very simple page structure, but this is a fixed layout.
01:41Now that refers to the fact that it is set to a fixed size.
01:45So any change I make to the browser--I am just going to resize my browser window
01:49here--does not affect this layout at all.
01:51It remains consistent no matter what device, not matter what user agent, no
01:55matter what screen size this is viewed at, it always stays the same because it
01:59is fixed to a very specific size.
02:01Now if we take a look at the CSS for this page, we can see that it does not take
02:05a lot to accomplish this. If I scroll down, as a matter of fact, I can see in
02:08this body selector here the page is being set to a width of 960 pixels.
02:13So we are using absolute length values for these measurements.
02:16And if I go down into the actual layout styles for these three sections, the
02:20article, aside, and footer, you can see that we are using floating to move
02:24elements from one size to next.
02:25We have an entire chapter on floating command.
02:28But more importantly, the width values here, each one of these is giving a very
02:32explicit width value that's telling it how much space within that fixed size
02:37those elements should take up, so that is a fixed layout.
02:40Okay I am going to switch over the fluid layout now and preview that in the browser.
02:46And now you can see, probably at first glance, there is little something
02:50different about this page.
02:51It appears to be occupying a bit wider space, so it looks a little bit wider
02:56than our previous layout.
02:57Same basic concept in the layout.
02:59We have two columns. The main content is on the left-hand side, the note from
03:02Lewis Carroll is in the right-hand side, and of course we have our footer down in the bottom.
03:06So what is so different about a fluid layout?
03:09Well, unlike a fixed layout which remains fixed based upon a specific size
03:13that you set on the page, fluid layouts do respond to changes made in the user
03:17agent or the browser.
03:18So as I resize the browser, you can see that the layout changes as well.
03:23Now it stays a two-column layout, but essentially each of these sections is
03:27taking advantage of the available space.
03:29So the upside of this is no matter what size this is viewed within the browser,
03:34we are still going to get our two-column layout.
03:35We are not going to have any horizontal scrollbars or content cut off, but the
03:39downside is is when you start getting to the extremes, you can see that the layout
03:44starts--really has a hard time holding up with this.
03:46And actually what I've done here is I have some minimum width values set on this,
03:50so that it wouldn't even go further than this.
03:52And the other thing is that at the other extreme, when we start taking it
03:56really, really wide, some of the content begins to get maybe a little bit wider
03:59than we'd like for it.
04:00There are of course minimum and maximum width values that we can put on our
04:04elements to prevent this, but that's one of things that you have to aware of
04:07with flexible layouts.
04:09Now if I go back into the CSS of my fluid layout here, I can see that again the
04:14body has a width value set, but this time instead of an absolute value, we are
04:17using percentages, so at this point we are just saying hey, go ahead and let this
04:20page take up 85% of the available space within the browser.
04:23If I go into the individual layout styles of these, I can see that again, we
04:27are using percentage values for widths and padding as well so that each one of
04:31these elements is occupying a specific amount of space based on a percentage of its parent.
04:37So while you're not really sure exactly how much space each of these elements is
04:40going to take up, you understand that the relationship between the two of them
04:44will remain consistent, regardless of how much space is available, because of the
04:48percentage values that you're setting on these.
04:49Okay now let's go ahead and take a look at responsive layouts.
04:52So I am going to switch over to the responsive.htm, and I am going to look take a
04:56look at that in the browser first.
04:57Now responsive layouts are very similar to fluid layouts in the fact that they
05:01do respond to various things.
05:03One of the things that they can respond to, and the thing that we are going to
05:05demonstrate here, is the size of the browser window. They can then also respond
05:09to different things,
05:10for example, the orientation of the device. Responsive layouts can, if a device
05:14is in portrait or landscape mode, for example, the height of the device.
05:18So there are a lot of different things responsive layouts can respond to.
05:21So in this case, you will notice that initially we actually have a three-column
05:23layout based upon the available size.
05:25So our main content area has a two column layout, and we also have the aside still here.
05:30We notice that our sections have a little bit of a separation between them in
05:34this particular layout, and we have a footer down here as well.
05:37All right, so if I resize this, at first it looks really no different than the
05:42fluid layout. It's just changing based on size. But I also notice that this
05:45appears to have a range.
05:46So when I get down to this section, it stops flexing.
05:50And if I keep going down, I reach a break point and when it hit the break point I
05:54get sort of an entirely different layout.
05:55You will notice, for example, the heading changes in size. You will notice that
05:59the column over here is now single column instead of a double column.
06:02We don't have the visual separators at the end of the section anymore, so all of those change.
06:06And you can have as many of those break points as you want or need within a layout.
06:10So when we get down to another certain break point, instead of remaining a two-
06:14column layout, it goes down to being a single-column layout and in this case
06:18again a lot of things changes, like the size of the headings, the text size
06:21changes a little bit.
06:23We now get a separator above the aside and below the article, and then our footer
06:27changes a little bit as well.
06:28Now, responsive layouts are relatively new in terms of a layout concept.
06:33It's designed around the way that people are now consuming content online, which
06:37is to say, using multiple devices.
06:39So here we have a layout that's adapted for the screen we have one that's
06:42adaptive for tablets, and we have another one, a smaller single-column layout,
06:46that's adapted for phones.
06:47Let's take a look at the CSS and see how that's actually accomplished.
06:50So if we go back to the CSS and start towards to the top of it, you can see in
06:54the body selector here, for example, there's no width specified here. Really
06:57the only thing that this is a accomplishing is centering the content on the
07:00page, in terms of layout.
07:02It continues for a while. There is no layout styles, so where have all
07:05those layout styles gone?
07:06All right, so if we go down to about line 74 or so, we can begin to see the
07:10layout styles and the inner rule that you may or may not have seen before.
07:13This is called a media query, and we have an entire chapter on response layouts,
07:17and we are going to talk about media queries in more detail.
07:19But essentially what media queries allow you to do is filter which styles are
07:23applied based upon the media query.
07:25So if these conditions are met, the styles contained within it are served, but if
07:29they are not met, they are not served.
07:30Now this allows you to present in this case pretty much three different layouts.
07:34So here we have styles that apply only if the minimum width is 780 pixels,
07:40so this would apply to any screen size of 780 pixels or greater.
07:44Now you will notice that we are using percentages.
07:46So this is more of a flexible or fluid layout, but we do have minimum and
07:50maximum widths, so that's basically giving it a range of measurements that you
07:54can flex inside of, but then it will sort of freeze in those positions if it's
07:59above or below those values.
08:01Now if I keep going down, I can begin to see each of these media queries. This
08:05one, for example, goes between 481 and 780 pixels, so that's for tablets.
08:10And if I just scroll down a little bit further, I can find anything that's below
08:14480 pixels, and that would be a smartphone.
08:17So that is a brief overview of fixed, fluid, and responsive layouts.
08:22I don't want to imply, or I do want to make it seem from this movie, that there
08:28are only those three types of layouts.
08:29For example, we didn't even talk about elastic layouts, which are layouts that
08:33are totally based on ems. Nor did we discuss how some designers just mix
08:37techniques from various layout types into hybrid layouts.
08:40Now in fact, you could argue that there aren't really types of layouts at all;
08:45there is just a mixture of techniques that results in the desired page design in
08:49the layout's behavior.
08:50Now that's what I am focusing in this title
08:52first on the layout techniques that we are going to be learning and then how to
08:56use those techniques to create a specific layout type.
08:59And I want to make sure that you understand that the layouts you build should
09:03conform more to the needs of your site than the rules of any one specific
09:07layout type.
Collapse this transcript
CSS debugging tools
00:00One of the things that I've really been intrigued with over the past few years
00:04is how the browser has evolved from being simply a passive receiver of web
00:08content to being a very active part of the web development process.
00:13Now that's especially true for CSS authoring,
00:16as most browsers now have built- in debugging tools for checking and
00:20modifying your CSS.
00:21Now if you're using a HTML editors like Dreamweaver, you already have a robust
00:26set of CSS debugging tools built right into the editor itself.
00:30If on the other hand, you are using one of the more lightweight editors, such as
00:35the one that I'm using for this title, Aptana Studio, then you are going to
00:38rely on the browser for much more of your testing and tweaking.
00:41Now this process can be made a lot easier if you know how to use the
00:45browser debugging tools,
00:47so in this movie, I am going to introduce you to a few of those tools,
00:50show you have set them up, and give your brief tour of some of their CSS capabilities.
00:54Now one of things that I want to point out here is that the tools that I am
00:58about to show you change all the time,
01:01I mean even weekly. They are constantly being updated,
01:03so there's a good chance that the interfaces and functionality that you are
01:07going to see here might differ slightly from what you see as you use them,
01:11because you might be using updated version.
01:13Now since each browser has its own set of tools, I want to introduce you to
01:17them one at a time.
01:19Now to do that, I have the index.htm file opened up here from the 01_10 folder,
01:25and I want to start with Firefox.
01:28Now Firefox has a set of tools called Firebug, and if you have been doing web
01:32design or web development for any time at all, you've probably heard of this.
01:36If you want to get a little bit of more information on it or read the
01:38documentation or download latest build, go to getfirebug.com.
01:43So I am going to switch over to our page being previewed in our browser, and
01:47let's say I want to start inspecting some of these elements or checking out the
01:51CSS or even tweaking some of the properties.
01:53To do that, I could launch the Firebug console.
01:56Now there are couple of ways for me to do this. One of the easiest ways is to go
01:59right over here in the toolbar and just click Firebug. Now that will turn it on.
02:02Now what I get is this console at the bottom. Now most browser debugging tools are
02:07going to look exactly like this, so if you get comfortable using one, moving
02:10into another browser and using its set of tools is pretty much just making a lateral move.
02:16Okay now I'll notice if I go right over here to Inspect Element, I can mouse
02:21around the page, find elements to inspect, and click on them.
02:24And when I do that inside of the HTML, it shows me exactly where this is. As I
02:28mouse around the HTML, you will notice that's highlighting different elements
02:31as well. It's even showing the CSS box model properties. Padding is indicated
02:36by that sort of purple color that you are seeing there and margins is that yellow color.
02:40Now I have different panels here.
02:42I have an HTML panel.
02:44I have a CSS panel. The CSS goes ahead and let's me inspect any of the CSS.
02:49And if I'm looking HTML panel and I inspect my elements and I settle on a
02:53specific element, you will notice that I also get these panels right over here
02:57that show me the computed styles. I can even turn certain things off. For
03:01example, I could say, hey, only show me the applied styles. And within that, I can
03:05do things like well, what would it look like if its color wasn't white, or if the
03:09font style wasn't italic.
03:11So I can begin to sort of tweak my styles right here by turning certain
03:14features on and off again, and I can even start writing some styles.
03:17So I can begin authoring in this environment and then copy and paste the
03:20styles into my own code,
03:22so there are a lot of really cool things that we can do here.
03:24We even have a console where I can run different profiles and warnings. So for
03:29example, if I reload this page and say I want to know what the CSS warnings for
03:34this page are, I can get all the warnings on it.
03:36Now don't be too alarmed here.
03:38These are just certain properties or selectors that Firefox doesn't support;
03:42it doesn't mean that the code isn't going to work.
03:44Okay, so that's Firefox. Next
03:46I want to switch over to Chrome and show you some of Google Chrome's tools.
03:50Now Google Chrome is WebKit-based browser, so it uses something known as
03:54WebKit Web Inspector.
03:56Chrome and Safari use the exact same set of tools, so I am going to show you
04:00these in chrome, but in Safari it works exactly the same way. So if you browse
04:04in Safari and test in Safari, you can use exactly the same set of tools.
04:08And if you want to learn a little bit more about Web Inspector, you can go to
04:11trac.WebKit.org/wiki/Webinspector and get a little bit more information on this.
04:17So I am going to switch over to our previewed page again.
04:20And here, we don't have a console button, but the easiest way to activate Web
04:24Inspector is just finding an element that you want to learn a little bit
04:27more about and inspect, right-click it, and I can say, hey, I want to inspect this element.
04:31So as soon as I do that, I have the WebKit Web Inspector open up, you can see it
04:35looks very similar to Firebug. Some slight differences obviously.
04:38But as I mouse through the code, again I am getting the CSS box model properties
04:43visualized, which is very nice.
04:46And again, I get my computed styles and here. I can begin to sort of play around
04:50with those, turn them on and off again.
04:52So I get a lot of the same functionality with the Web Inspector that I get with Firebug.
04:56Now if you use Opera, Opera has a tool called Dragonfly and I really like Dragonfly.
05:02It's very, very good. Very similar Web Inspector, very similar to Firebug, but
05:05it's very powerful in its own right.
05:07If you want to learn more about Dragonfly, go to opera.com/dragonfly and
05:12they have the whole section of their web site dedicated to teaching you how to use this.
05:15All right, so I am going to go over to my page opened in Opera, and again, it's a same drill.
05:20I just find an element, I right-click it, and I say okay, inspect that element for me, and Dragonfly opens
05:25up. And let me just make this a little bit larger.
05:27As I begin to mouse around elements and inspect those elements, notice that I
05:31get a series of guides that help me find out whether elements that I am
05:34wanting to are lining up with each other and how much space they are taking up within the layout.
05:38I also get a nice little preview of some of those CSS box model properties as well.
05:42Just as before, we have a console that we can work with. We have styles that
05:46show us computed styles.
05:47We have different elements that we could sort of turn on and off here. We could
05:51get in sort of a preview how the styles are working.
05:53And Dragonfly even has a zoom, so you can zoom up on certain elements as well,
05:57baked right into the console, which is really nice.
05:59Now I'm sort of focusing on the CSS capabilities of these tools, but obviously
06:03there is a lot more to them.
06:05They all have JavaScript consoles where you go in and debug your JavaScript.
06:09They have a lot of web development tools available to them as well.
06:12So there's a lot to these tools.
06:14Now if you are on the PC and you are using Internet Explorer, Microsoft does
06:18have a similar set of tools. And if you go to microsoft.com/download, and search
06:22their Download Center for the Internet Explorer Developer toolbar, you'll find a
06:27toolbar that you can download and install into Internet Explorer, and it gives
06:31you a similar set of functionality.
06:33So, now that we gotten a good idea as to where to find CSS testing and debugging
06:37tools within the browser, we need to explore exactly how these tools can help
06:41us author our CSS, and we are going to go ahead and do that in our next movie.
Collapse this transcript
Using the Firebug Inspector and the WebKit Web Inspector
00:00Now that we've seen how to access a browser's web debugging tools, let's
00:04experiment with using them to troubleshoot our CSS and refine our page layout.
00:08Now, for this exercise, we're going to focus on using the two most popular web
00:12debugging tools, Web Inspector and Firebug, as a way of getting more comfortable
00:16with using the tools, but don't be afraid to use Dragonfly or Microsoft's tools,
00:21whichever browser that you're going to be using most frequently, and really
00:24should be comfortable with using all of these tools.
00:27Okay, so I have index.htm opened up here from the 01_11 folder, and the first
00:32thing I'm going to do is I'm just going to preview this in Firefox.
00:36So, this is very common.
00:37We work on a page for a while, we save it, we test it, we preview it in the
00:41browser, and we see something that doesn't exactly look the way that it's
00:44supposed to. And in a lot of cases, we have to sort of guess why or go back in
00:49our code and figure something out.
00:50In this case, I want to use is browser debugging tools to help me figure out
00:54what's going on here. So, what's happening is this banner headline right here, We love urban
00:57photography, does not look the way that it should.
01:00We should see slightly a little bit more letter spacing and that should be all
01:03caps rather than mixed caps.
01:05So, I'm not really sure what went wrong here,
01:07so I'm going to turn on Firebug and see if it can help me with this.
01:09So, I'm going to go right up here and turn the Firebug console on, and again just to
01:14sort of refresh your memory about Firebug, we have these different tabs which we
01:18can look at: the HTML; we can look at the CSS;
01:21we can look at any scripts that are going on; we have console view, that sort of thing.
01:24I want to focus on the HTML right now because notice that one of the things you
01:27can do is you can just sort of parse through your code and you'll see a preview
01:32of the element and you'll actually see some of the box model properties.
01:35Purple is padding, yellow margin, and then the blue that you're seeing that's
01:40the actual content itself. And we can go through and inspect any part of our code that we want.
01:45We can open up our code structure.
01:47We can look inside these various elements, so we can really inspect all
01:51the things we want to. All right!
01:52So, going back to our banner here, we also have this little tool right here that
01:56says, "Click an element on the page to inspect."
01:58So, if I grab that, I can just sort of mouse around, visually grab the
02:01element that I want.
02:02That's going to focus on that within my code.
02:05Okay, if I look over here on the right- hand side of the Style panel, I can see
02:08the computed styles for this element.
02:10Well, the computed styles aren't what I expect them to be.
02:13Color, font-size, and padding-top, they're exactly what I expect them to be, but
02:17I'm missing a couple of properties.
02:18I'm missing my letterspace property and I'm missing my text transform property.
02:23Now, there are a couple of things that I can do here.
02:25One of the things, for example is, I can go to the console.
02:27So, if I go over to the console and I run my warnings--now sometimes you have to
02:31refresh the page in order for this to cycle through again--
02:34I notice that right at the very top of this it says, "Expected end of value but
02:38found ':' Error and parsing value for 'letter-spacing'.
02:40Declaration dropped."
02:42Now, it does tell me this happens on line 285, but it doesn't tell me exactly
02:45which selector this belongs to.
02:48I happen to know for a fact it belongs to the one we're looking at, but if you
02:51didn't know that, you'd still be kind of almost needle-in-a-hay-stacking this,
02:55but it does tell me that something is going on wrong here.
02:58For example, that colon there, when it's expecting a semicolon, that's going to
03:02cause some parsing errors in my CSS.
03:04So I kind of guess that that's one of those issues.
03:06Now, there aren't that many differences between WebKit's Web Inspector and
03:10Firebug, but this is one of the bigger differences.
03:12The fact that here it's just showing me the computed styles.
03:15It's not showing me all of the properties that are actually in the CSS.
03:19It's showing just the finished computed styles.
03:21So, I want to go over to Chrome for a moment and show you kind of what happens
03:25when I look at the same element in Chrome.
03:27So, if I go right over here and I view and inspect the element for that banner
03:31headline, I get a slightly different view.
03:34You can still parse through all the elements in the code. You still get your
03:37visual aids, but the colors are slightly different here in Chrome--or Web Inspector
03:42I should say because it looks exactly the same as Safari.
03:44We have sort of that orange color for margins.
03:46We have sort of that green color for padding. And again if we inspect an element--
03:50here's the inspect element tool here in Web Inspector--
03:53one of the things that Web Inspector does for us that I really like that Firebug
03:57doesn't is it gives me those CSS properties, that sort of visualization, as well
04:01as computed values as I move through the elements.
04:04So, that's really kind of nice.
04:06Okay, but back to the task at hand.
04:08Here, I see something very different.
04:09When I look over here in the Styles pane, I can see that letter-spacing has a
04:14strike through and if I hover over this, I can see that I've got a little warning
04:18message here, and then my text transform is not applying at all.
04:21So, it's just basically parsing this as one big, huge error.
04:25So, I can tell that that's a problem.
04:27Another thing that showing all of these properties does for me is it helps me
04:31catch other types of mistakes as well.
04:33Let me show you what I mean.
04:34Now, I've got a couple of choices when inspecting elements.
04:37One, I can come through and use the Inspect Element tool and just click on elements.
04:41I can do it that way, or I can come directly down here in the code and really
04:45sort of target exactly the element I'm looking for.
04:48Now, when I click on this heading 1 that's in the header, if I look over here in
04:52my Styles, I notice there's a strikethrough with this line-height and if I look at
04:56that, the line-height is set to 100 pixels, but there's another line-height
05:00declaration down here at 165.
05:02Now, the likely culprit here is when I was authoring the styles I
05:06probably tried the line-height at 100 pixels and didn't like it and added the line-height:
05:10165 property to the end instead of modifying it,
05:13for whatever reason, who knows, I just forgot to go back and remove the
05:17initial line-height.
05:18Now, what I really like about this in WebKit Inspector is well, notice it tells
05:22me this is on main.css line 185.
05:25So, very quickly and very easily, using both Firebug and Web Inspector, I found
05:30two errors in my CSS that I need to address.
05:33So, I'm going to switch back into Aptana Studio, go back into my code, and I'm
05:37going to fix those things.
05:39So, I'm going to open up, from the CSS folder here in 01_11, I'm going to open
05:44up main.css, and using the information that I got, I'm going to scroll down to around line 185.
05:52Here we go. And I realize I don't need that line-height declaration, so I'm
05:55going to get rid of that. And then down around line 280 or so, 279 now, I can
06:02find that banner h1 rule, and here's the error, right there.
06:06So, instead of a colon, that is supposed to be a semicolon,
06:09so I'm going to change that and save it, and now I'm going to go back into my
06:14browsers and I'll refresh the page.
06:17Now, notice the change in the visual.
06:20So, we now have our text-transform is working, the letter-spacing is working,
06:23everything is happening there. And in Firefox if I look over here in Firebug, now
06:28I'm actually seeing those values.
06:29There's the text-transform.
06:31There's the letter-spacing. I wasn't seeing that before.
06:34If I go over to Web Inspector and refresh the page, I see similar results. And
06:39if I focus on that element, I can see that those properties no longer have a strikethrough.
06:44So, we've seen how we can use Web Inspector and Firebug to debug some of our
06:49CSS, but what if you just want to sort of play around with it? What if you want
06:52to manipulate it and really see almost immediately the visual impact that you're
06:56going to have on your code?
06:58Well, that's pretty easy to do as well.
07:00So, here I am, in Web Inspector, and one of things I want to do is I want to focus
07:04again on our heading here.
07:06So, again I can do this through HTML code, I can find this and click on it, or I
07:10could just inspect it directly on the page.
07:12Now, I've got more than just styles and computed styles available to me over here.
07:15If I come over, I have this Metrics pane, and this Metrics pane is giving me the
07:20box model properties of that particular element.
07:22You'll notice, for example, if I hover over a border, nothing shows up, margin
07:26nothing shows up. It indicates those properties don't exist for that particular
07:29element. But if I go over padding, I can visually see it; if I go over to the
07:32computed width and height, I can see that as well.
07:34Now what's really nice about this is let's say I want to change that padding value.
07:38I can actually click right here, double-click the value,
07:41I can use arrows to go up or down and I can visually see that happen. Or I can
07:45even type in a value. Let's say I go down to say 230 and hit Return. I can kind
07:50of see what effect that's going to have on that headline, and whether I like it a
07:54little bit further down or not.
07:56Now, I'm going to switch back over to Firebug and show you pretty much how
07:59the same thing is done.
08:00So, they all have the same tools for the most part;
08:02they're just in slightly different locations. All right!
08:04Let me resize the Firebug pane here for a minute and center on our headline here.
08:11All right! So, if I go over in this point to the Layout tab, now I really like this in Firebug.
08:16This is nice because again we get the same functionality.
08:18If we mouse over to different properties, you're going to be shown those properties.
08:22Let me zoom up a little bit so we can sort of see the padding there. There we go.
08:25But we also, in Firebug, get rulers. You can see the rulers show up on the browser now.
08:29I'm getting guidelines to show me where this is lining up. And again I can use
08:34my arrows to nudge this.
08:35I can type in a value.
08:36Let's say I type in 290 this time, and you know what? At 290, I kind of like that,
08:41and I like that a lot.
08:43So, I might go ahead and decide to use that in my own CSS.
08:47So, if I switch back to Style and my computed style for this, you'll notice that
08:52what it did is it put the sort of in-line style directly in my HTML.
08:56Now, it's not modifying the HTML on your hard drive, or even on your server;
08:59it's just modifying this particular instance.
09:02Okay, so I like that value of 290.
09:05Now, if I want to play around the way things look, I can also edit my styles
09:08directly in this window. And I can do this in WebKit Inspector or Firebug, and in
09:13this case I'm just going to show you this in Firebug.
09:15Notice I can click to create a new line here, and I can experiment.
09:18I can say okay, well, what if I put a border on the bottom of this and I did
09:23just a 1-pixel solid white border? And now I can preview what that looks like.
09:30I can toggle that on and off to see if I like it or not, and I can decide whether
09:35or not that particular property is for me.
09:37Now, anytime, if I add these properties, I could just go ahead and highlight
09:41those and I could copy and paste all those properties directly into my page.
09:46So, if that's what I decided to go with, I can simply copy and paste them.
09:48I can even create brand-new rules here in Firebug or Web Inspector.
09:52So, you could even author CSS directly in the browser and then copy and paste
09:57them into your code.
09:58So, if I go back into my CSS, notice that I can come right here in the
10:03padding-top, I can go ahead and plug in that 290-pixel value that I liked so much.
10:08I can go back into my page.
10:09I can refresh that, and I can see the change that I made here in the browser.
10:13The visualization of that that I like is made.
10:16So, I know this is a brief introduction, but it should five you some idea of
10:19exactly how powerful these tools are.
10:21Now, taking advantage of them just makes sense to you as a designer because
10:25think about it. In a sense you're working directly in the browser.
10:29Now that means that you're testing it in the rendering environment that you're
10:33eventually targeting, and it just doesn't get any better than that.
10:35Now, I'm sure that by now you've realized these tools offer far more in the way
10:40of functionality than just CSS.
10:42They can help you author and debug your HTML,
10:45they feature a JavaScript console that helps you test and debug your code, and
10:49they have a full range of tools to help you understand page loading, resource
10:54management, and a lot more.
10:55So, in that sense, regardless of which editor you're using to write your HTML
10:59and CSS, be sure to take full advantage of these powerful tools as you begin
11:04to develop your sites.
Collapse this transcript
2. Design Considerations
Page design workflow
00:00In this chapter I want to focus on the process of designing page layouts and
00:04creating the initial structure of your page.
00:07Now unlike the rest of this course, many of the movies in this chapter will not
00:10be hands-on; rather, they're going to focus on the overall concepts of page-design workflow.
00:15One of the biggest misconceptions about a web design is that web designers spend
00:19most of their time working in code.
00:21Now the reality is that on most sites designers spend the majority of their
00:25time in the planning and design process.
00:28Now not all processes are the same, and designer workflows are going to very
00:31widely based on the personal preferences of the designer, the size of the team,
00:36client type, or specific site requirements.
00:38But with these variables in mind, I want to take a moment to discuss my own
00:42personal workflow and the tools that I use.
00:44Your own overflow is likely to differ from mine, but if you're new to
00:48designing web sites, this should give you a few things to consider when
00:51creating your own workflow.
00:53First I take on what I refer to as the discovery phase.
00:57In this phase I'm interacting with the client to identify what the goals of the
01:01site are, what users should expect from the site, what type of functionality the
01:05site will require, and what the site's content is going to be.
01:09Now I then use this information to begin to create wireframes and prototypes.
01:15Depending up on the site, this might range from simple sketches all the way to
01:19fully realized prototypes.
01:21The idea in this phase is to make sure the site architecture is sound, that
01:25content is organized within the site properly, and the site functionality
01:29has been thought through to make it is as usable as possible for the target audience.
01:34Around this time I'll also start working on color and type treatments for the site.
01:38Now color and typography are critically important to communicating brand and
01:43establishing the proper emotional response from users,
01:46so I'll usually play around with establishing typographic rules for the site,
01:50such as which fonts are going to be used and when and how headings and body
01:53copy should relate to each other.
01:55With color I'll establish primary colors and secondary colors and begin to set
02:00rules for how the colors are used within the site.
02:02If I'm working with a client, once I'm sure that the decisions on
02:06functionality, site structure, and color, and type are finalized, I'll begin to
02:10generate page mockups.
02:12In some cases I'll simply generate quick sketches that give me an idea of how
02:15page design will look, but most of the time I like to generate very detailed
02:19page mockups in programs like Fireworks, Illustrator, or Photoshop.
02:23Now these mockups give me several advantages.
02:26They allow me to refine the design with the client without having to generate
02:30code, and they give me the ability to create site assets directly from the mockup
02:34with pixel-level precision.
02:36I can export out things like images and icons while having a precise design that
02:40allows me to plan page structure and styles.
02:42In fact, most of the time I know what my HTML and CSS will be before I ever
02:47start working in a code editor.
02:49Now obviously this is a very high-level overview of the page-design process, and
02:53it's certainly not going to be the same for everyone.
02:56What you need to do is develop a process that works for you, supports your
03:00design aesthetic, and ultimately meets the needs of your clients and the
03:04site's users.
Collapse this transcript
Page design tools
00:00As I mentioned earlier, most web designers spend a tremendous amount of time
00:04planning and designing sites before they ever write their first line of code.
00:08As such, it's essential that you have a strong set of tools to help you with
00:12the design process.
00:13With that in mind, I want to spend a little bit of time talking about the tools
00:17that I use in my own design workflow and how they might be useful in your own.
00:22First I recommend getting a good sketchbook.
00:25Every one of my sites starts with a series of sketches.
00:28This allows me to get ideas down quickly, in a nonpermanent way, and I can
00:32take notes, write down measurements, and work through problems before I ever
00:36start creating digitally.
00:38In fact, I love working in sketchbooks so much that I continue to sketch things
00:42out and make notes throughout the entire design process.
00:45Next, I want to point out a few online tools that I use.
00:50First up, I want to show you Typetester.
00:52Now Typetester allows you to compare up to three different fonts to each other,
00:56and what's really nice about this is the list of fonts that you can choose from.
00:59You can choose from the safe list, which is essentially fonts that are on every
01:03single machine out there.
01:04You have your Windows defaults, your Mac defaults, and even they go down into
01:09Windows Vista and Google Fonts. So if you're thinking about using the Google
01:13Fonts API, you can test out some of their fonts directly here.
01:15So when you choose some fonts, after choosing some fonts to compare to one
01:19another, you can come in and change their size.
01:23You can change the tracking, the leading, the word spacing, colors.
01:27You can change the text that they display.
01:29This is a great way of comparing fonts side by side or even seeing how well
01:33certain fonts work together.
01:35Another online tool I use a lot is Adobe's Kuler.
01:38You can find this at Kuler and that's with K-U-L-E-R, kuler.adobe.com.
01:44Now picking a color scheme is one of the most important steps in web design, and
01:48Kuler really helps take a lot of the guesswork out of it, especially if you're
01:51really bad with color like I am. So let me show you kind of how this works.
01:55Number one, a lot of people are members here, and they'll upload their themes and
01:58they'll play around with themes, so lot of times you can just kind of go
02:01through these themes that they have here and choose one of those and kind of
02:04use that as a starting point.
02:06Now you can also sort of mix and match your own. So if I click right here to
02:10toggle the color viewer on, notice that I can begin to move these around.
02:14So I can move different colors around.
02:17I can mix up my own colors.
02:19I could even say, okay,
02:21I want this to be the base color, and let's use complementary colors. And then
02:26you can sort of move these around as well, and the relationship between those
02:30colors remains the same.
02:31Now once you do that, you can go down here and tweak these colors individually
02:35using the sliders, and you can grab the HSV, RGB, CMYK, LAB, and even the HEX
02:40value to use in your own sites.
02:41Now you are limited to five colors at a time, but of course you can pick a color
02:45and then continue to sort of build off of that to build a much bigger color
02:48palette out of that.
02:49So a huge fan of Adobe's Kuler, and it's a free service, so that's really nice.
02:53Now as far as Desktop tools go, I use a combination of three applications when
02:58designing page mockups.
03:00Now some designers prefer to design directly within the browser, but I really
03:03like to generate fairly detailed mockups before I begin writing code.
03:07To do this, I use Adobe's Photoshop, Illustrator, or Fireworks.
03:12Depending upon the project, I may only use a single program or some
03:16combination of the three.
03:18Now typically, I use Illustrator when the layout calls for heavy use of vector
03:23artwork, icon creation, or, like you are seeing here, when I need to design a
03:28responsive layout that's going to have several different views based on the size
03:33of the screen or the device that's being used.
03:36I use Photoshop for all bitmap graphics, photographs, or mockups that make heavy
03:41use of image composition.
03:43If I need to create a prototype, especially one that allows clients to preview
03:46interactivity and usability, I'll use Fireworks, as it has a lot of different
03:51built-in prototyping features.
03:53Now speaking of mockups, the one question I get asked over and over again is
03:59how do I go from my mockups to finished code?
04:02Well, currently there is no export option from any program that creates
04:06acceptable code, so if you're looking for that feature, I'm sorry to disappoint
04:10you, but it simply does not exist.
04:12Now what I do, and the process that we're going to be exploring for the rest of
04:16this chapter, is use the mockup to help me plan my initial page structure and
04:20generate assets by exporting out graphics and other page content.
04:24Of course everyone's design process is different, and the tools and processes
04:29that work for me might not be a great fit for you.
04:32If you're a graphic designer, you might already have a tool set that you use that
04:36you're very comfortable with and maybe it's a little different than mine, but it
04:39allows you to be just as productive. That's fine.
04:42I encourage you to use what works for you.
04:45Just think of this as an introduction into a specific workflow. Take from it
04:49what you think will work for you and then modify it for the specific projects or
04:53strengths that you possess.
Collapse this transcript
Determining page structure
00:00For new designers the process of going from mockup to code is probably the most
00:05challenging aspect of web design.
00:07Now obviously the more experience you have at building sites the easier this
00:11process becomes, but there are some things that you can do to simplify things a bit.
00:15Now first I recommend thinking of the basic structure of the page
00:20before anything else.
00:21Don't get too concerned with exactly how each element is going to be structured;
00:24rather, think of the overall groupings of content and what the basic structure of
00:29the page will look like based on this.
00:31It gives you a nice starting point, and it keeps things becoming too complex too quickly.
00:35Now to illustrate this, I want to show you the mockup that I created for the
00:39desolve.org site that we will be using in some of our labs, and walk through the
00:43process of determining the basic page structure for the homepage.
00:47Now if you have Illustrator installed, you can go through this with me, as I've
00:51included the Illustrator file in the assets folder. Let me show you where that is.
00:54I'm going to pull up my exercise files, and you'll notice that directly in the exercise
00:58files we have a folder called Assets.
01:00If I open that up, here you can see the mockup for the homepage, and I have also
01:04included a PDF version of it as well so that if you don't have Illustrator you
01:08can open up the PDF and kind of follow along with this as well.
01:11Okay, so what we're looking at is the homepage.
01:13Now typically when I do a mockup I'll design not only the homepage but also
01:17some of the secondary pages as well. I don't design every single page that's
01:21going to be in the site.
01:22I don't have mockups for that. But if there is something that's going to be on a
01:25page that needs me to worked through some of the design issues, otherwise I'm just
01:29doing some secondary pages so that I can determine what the consistent design
01:33elements and structuring of those pages are going to be as well.
01:36But for brevity's sake, we're just going to look at the homepage for
01:39this particular mockup.
01:41Okay, now the first thing that I like to do after the mockup is finished--and
01:44to be quite honest with you, this is actually something that I do over the
01:47course of designing it as well--
01:49I'd like to think about where are the basic structural areas? How is this
01:52content group together, how does it relate to each other, and what is that
01:55going to mean semantically in terms of how this content is going to be
01:59structured in HTML?
02:00So I'm just going to go from sort of the top down.
02:02I've designed a couple of visual aids to help us sort of visualize kind of what
02:06I was thinking of here.
02:07So the first thing I am going to do is, right at the top of the page we have the
02:10Desolve logo, and we have the tagline fine urban photography, so this is content
02:15that acts as an identifier, who is this organization, and that sort of thing.
02:18So that can all be grouped together in the header of the file.
02:21Directly underneath that we have a layer of navigation.
02:24This will be the site's main navigation, so it's going to sit inside of its
02:27own region as well.
02:28Now a lot of the choices that you make are personal decisions. You make
02:32decisions as designers about how content is going to be organized.
02:36Certainly this header content and the navigation content could be grouped together.
02:40It could all exist in one header. It's very common to see a site navigation
02:44inside the header of the document.
02:46In this case I just felt like I wanted the main navigation out by itself so
02:50that it was his own specific separate region, and that's how I'm going to
02:54structure the page. So a lot of times you're just making a decision based upon
02:57that particular site and the needs of that site, and sometimes it's just
03:01personal preference.
03:02Okay, now the next thing I'm taking a look at, just below this where it says, "We
03:05love urban photography," and it sort of has a little About Us section here where
03:08it describes who these people are and what the site is all about,
03:11that particular element is not going to be on secondary pages; it's only here
03:15for the homepage as a way of sort of introducing the user or the viewer to what
03:20this content is all about.
03:21I'm calling that a banner, and it's going to be in a separate region because it's
03:25certainly not going to be repeated throughout the site.
03:27Okay, if I scroll down a little bit more, we start to get into the actual main
03:30content of the site.
03:31I can see just below the banner we have our latest galleries, which is
03:34followed by a list of galleries, so that's displayed visually. We have some
03:37repeating structure there.
03:38So all this content right here on the left-hand side, all of that content relates
03:42to each other, and is probably the most important content on the stage, sort
03:46of the main content, if you will.
03:48So I'm going to group that together as a single article. So this is, that
03:51left-hand column, is a single article which we will refine the structure a
03:54little bit later on.
03:55Now beside it we have some links to archived galleries, so it relates to the article.
04:00We have some contest information right down here: this month's contest
04:04and previous contest.
04:05So all of this relates to the galleries, but it's not part of it, so that's
04:10going to go in the sidebar, and we'll talk about what type of elements we might
04:13use to represent that in just a moment.
04:15Now I finish this up by going down to the very bottom of the mockup, and here I
04:18can see I have an area for searching for content on the site,
04:21I have some copyright information, a link where they can follow us on Twitter.
04:25So all of this is really just footer information,
04:28so content that goes in the footer of the page, and that's exactly how I'm going
04:31to display that structurally. So I'm going to group that all as a footer.
04:35So if you look at this, including the footer, we have one, two, three, four,
04:39five, six distinct regions of content on this particular page, and that gives us
04:44a really solid idea as to what the initial structure of the HTML's going to be.
04:50Now I'm going to move my page over just a minute to give myself enough room to
04:53visualize this, but I went ahead and wrote out that initial structure if you
04:57will, right here on the page. We'll turn the visibility of that on. And what
05:00we're looking at is just the basic HTML structure for the content that we are
05:04displaying here in the mockup.
05:06So we have a header element, so that's going to be followed by a nav element,
05:09which is followed by a div.
05:10Now the reason I'm using a div here for the banner instead of another
05:14element, like a section, is because I'd really don't want this to stand out in
05:18the document's outline.
05:19If I was making a table of contents, for example, for this page, banner would not
05:23be part of that table of contents; it's really just sort of a promotional element
05:26design to just sort of show off what the site is about.
05:29So it doesn't really fit to any one distinct section, so I'm going to use a div
05:32there. And that's followed by an article element and aside because, it's related
05:36to the article, and then finally the footer element itself.
05:39Now a lot of the elements we're using here are HTML5 structural elements, and for
05:44some of you guys this may be the first time you're seeing these elements, or it might
05:46be the first time you've worked with them.
05:48So in order to introduce you to those I have a movie a little bit later on
05:51that's going to talk specifically about those HTML5 elements and sort of what
05:55they mean to the page.
05:57Okay, so in a nutshell, that's kind of my process. By focusing on the overall
06:00organization of this page content, I'm able to identify the basic page
06:04structure and obviously put some thought into how the site is going to work
06:08together as a whole.
06:09Now while I'm doing all this, I'm trying to solve certain problems, like how will
06:12this page structure work all the way throughout the site, how can I structure
06:16the content so that it's semantically correct so that the code is basically
06:20giving meaning to the content side of it and that meaning is consistent and
06:23correct throughout the site?
06:24How do I deal with changes to the page structure? Maybe there are going to be
06:27specific pages where page structure has to change; is it flexible enough to
06:30that? Is it flexible enough to handle modifications to the content at a later
06:34date? So those are all things that I like to think about.
06:37Now by focusing on questions like those, I'm able to create a coherent strategy
06:40around structuring the content, and if you do that, you're going to have that
06:44well before you actually begin to code your page.
06:46The more you do it, the better you're going to get at it.
06:49Now I know you've probably heard me say this before, but most of the time I
06:52really do already know how the HTML and CSS is going to be written well before I
06:56ever open up my code editor.
06:58So I go through this process, determine the page structure,
07:00I can look at the visual aspects of it, and understand how I'm going to
07:03style those elements.
07:04We're going to come back to this process in just a moment as we begin to code
07:08the initial structure of our page, but before we leave the mockup, I want to
07:11discuss a few things that you'll need to consider when planning graphics for
07:14your layout, and we're going to go ahead and do that in our next movie.
Collapse this transcript
Creating image assets
00:00Regardless of whether you create pixel- precise mockups or just sort of sketch
00:05your layouts on Post-it notes, at some point you're going to need to create the
00:08image assets for your site.
00:10Now because of the image requirements of your site will change based on multiple
00:14factors, you need to take just as much care in planning and creating image assets
00:19for your pages as you do with any other part of the process. Now to illustrate
00:23this, I'm going to return to the desolve.org.mockup.
00:26And remember, you can find that, if you go in the exercise files, you can
00:29find that directly in the Assets folder.
00:31And I'm going to use this to give you some examples of things that you're going
00:34to need to consider when creating image assets for your sites.
00:37Now if you don't have Illustrator installed, you can open up the PDF file of the
00:42same file that I've included in there as well.
00:44But you don't really have to open it up.
00:45We're not really doing anything here.
00:46We're not going to be exporting out any graphics.
00:48This is more a discussion of the decision-making process that I use when
00:52deciding how to deal with images within my site, and what type of images I'm
00:56going to be creating.
00:57Okay, so again, I'm just going to start at the top and talk about some of the
01:00factors involved in planning image assets for my site.
01:03Now the first thing that I'm faced with is the Desolve logo right here.
01:07You can see it's a separate graphic, but it has the text right next to it with a
01:10tagline. And one of the things that I need to start thinking of is is this
01:14entire logo going to be an image, or can I create this typographically in CSS.
01:21Now a few years ago that wouldn't have even been a question.
01:23This is a font called Cantarell that isn't a default system font,
01:27so it was something that I would not have been able to do, but with the advent
01:30of web fonts, I'm now able to take fonts like this and use them in my web site
01:35and not have to rely so much on images.
01:37So in this case I'm thinking that the background of the header element can
01:41certainly be handled through CSS, just assigning a color to it, and I could
01:45probably just export the logo out as a transparent PNG file and then use CSS to
01:50go ahead and style the text so that I don't have to rely on an image there and
01:55do any really complex CSS where I am hiding text and showing an image, that sort of thing.
01:59Now the next thing I have to decide are these icons, what I'm going to do, or how
02:03I'm going to treat these icons.
02:04These are all little icons that I've created in Illustrator so I've got a little
02:07camera, some word balloons there, a bag. And obviously I'm probably going to use
02:12those as background images.
02:14But do I really need to go ahead and export out four separate icons, or is there
02:18a way that I can sort of combine these?
02:20One of the things that I'm always thinking about when I create image assets is
02:24how to create the most efficient set of assets for my site as possible.
02:27I want to make as few requests to the server so that if the site is being viewed
02:32on mobile devices, for example, that it's a little bit more responsive and it's
02:35a little bit snappier.
02:36Well, one of the things that I can do through CSS is I could combine all of
02:40those icons into a single file, and then as I show those icons as background
02:45graphics, I can just sort of move to a new location for each of those images.
02:48That's a technique called CSS sprites that I've talked about before in a couple
02:52of my titles, and it works really, really well.
02:54Now before I went into creating my CSS sprites file for those icons, there were a
02:58few things that I had to think of.
02:59For example, you can see right down here in the Galleries,
03:01we have a larger version of that icon, and that's actually going to be
03:05consistent throughout the site.
03:06You go to the Contact page, for example, this icon is going to show up here in a
03:10much larger fashion.
03:11When I go to GEAR, that's going to show up in a much larger fashion.
03:14So do I go ahead and create sprites that are much larger and then just scale
03:17them down in the navigation?
03:19That's certainly an option.
03:20But one of the things that we continually have to think about now as web
03:24designers is what happens when the site is viewed on a mobile device? And
03:27scaling images up or down actually impacts the performance of lot of sites on
03:31phones and things like that.
03:33So basically, I really just did a compromise.
03:35I created two sets of sprites, and I want to show those to you.
03:38These are also in the Assets folder. And we have the icon sprites 25, which is
03:41a 25x25-pixel icon, and then we have the icon sprites 50, which is a 50xS0.
03:47So I use the 50 sprites for the larger one. I use the 25 sprites for the smaller one.
03:52Yes, it's one more request, but I've eliminated scalings. So these are all
03:56things that you have to think about, in terms of how are my graphics going to
03:59impact the performance of my site?
04:01Okay, now obviously I did not create these images here in Illustrator.
04:04The photograph of the New York City skyline, the Philadelphia photograph, the
04:08Chicago photograph, those are all the images that I imported in from Photoshop.
04:13Now the thing that I have to think about with those images, however, in time
04:16I've got these photos or any raster-based images, are they going to be
04:19represented through the use of an image tag, or am I going to use background
04:23graphics to show them?
04:25This is an extremely personal choice the designers make.
04:27A lot of people use image tags for everything.
04:30Some people try to use almost no image tags if possible.
04:33I have a very easy litmus test for whether this is going to be a separate image
04:38using an image tag or whether it's going to be a background graphic of a
04:41structural element, and it's simply this:
04:44Would that image need to be described to an assistive device?
04:48And what I mean by that is if somebody is browsing this site on a screen reader,
04:52with the site's content to be impacted negatively if that image wasn't described
04:57or if that image wasn't there?
04:59In that case I look at an image and I say, okay, is this image purely
05:02decorative in nature? Yes, this is purely decorative in nature.
05:05And so because of that, I'm going to use a background image for that.
05:08Same question gets put to these guys: Is that purely decorative in nature?
05:12And so you go throughout the site and you think about that.
05:15Is this something that's going to be a background image or is this something
05:17that's going to be foreground image that I'm going to use with the image tag?
05:20And that's typically how I decide it, but again, it is an incredibly personal choice.
05:24Now another thing that you need to think about when you're designing graphics
05:28for your site is, what can I handle through CSS, and what am I going to need an
05:31image to represent, and that is constantly changing, which is one of the reasons
05:35why you want to stay up with all the new CSS techniques.
05:38Take, for example, this overlay over here.
05:40Again, a few short years ago, I would have had to create this image as a whole
05:45with that overlay as part of it and then just sort of position the text over
05:49part of the overlay to sort of fake, if you will, that box.
05:52But now CSS can handle semi-transparency.
05:55It has basically alpha settings that you can put on different color values now,
06:00and so that is actually something that we can do through CSS now, and I don't
06:03have to worry about generating that image with that overlay already in place.
06:06That actually gives me a lot of flexibility, because now it means if I want to
06:10create a layout where this particular box conflicts over that image or if it's a
06:14flexible layout where the image can get clipped behind it, then I don't have to
06:18worry about that box resizing and repositioning.
06:21The same is true of certain icons. For example, if I scroll down and I see this
06:24inner button right here,
06:25again couple of years ago that is something that we would have to save out as a
06:29separate file, or I would have to understand that say a default button from the
06:33browser was going represent that. But now through the use of CSS and gradients
06:37and things like that I can go ahead and create that through CSS and not have to
06:41export that out as an asset.
06:43Now when you do decide that something needs to be exported out,
06:45for example, down here our Twitter icon, that icon is going to be a
06:49background graphic for us, but I'm going to need to export that out as a
06:52transparent PNG file.
06:53Now when I do that, one of the things I'm going to do--and I'm just going to take
06:56you over to that icon.
06:58I'll export this out as a separate graphic in its own file, and when I do that, I
07:02plan out the dimensions for the file very, very carefully.
07:05So this particular file is 90x90 pixels, and understanding that and
07:10knowing that, it's going to make it a lot easier for me to position this and
07:14apply the proper amount of padding to my element so that the icon is going to show up correctly.
07:19I'll also center the icon vertically or horizontally where I want it.
07:22I might add a little bit of extra space to one side or to the top or the bottom
07:26based on how I'm going to be positioning that graphic with other elements on the page.
07:30So when you start creating these sort of stand-alone images that you're going to
07:33export out, you have to think a lot about how it's going to interact with the
07:36other elements on the page, and do you need to modify it in anyway?
07:39What's the perfect size for it?
07:41Do you need extra space? Do you need it to be centered in any one location?
07:44So those are all things that you'll need to think about.
07:47Okay, now that is not by any means an exhaustive list of all the things that
07:51you're going to need to consider when planning images and assets for your site.
07:55I am fond of saying this and it's absolutely true:
07:57every single site is different, and the needs of images from one site to the next,
08:01they are going to change, and sometimes very dramatically.
08:04Now what's really important here is that you spend as much time as you can
08:07thinking about how images are going to be used on your site, and what's the most
08:10efficient way to display those images, and then how to create those assets that
08:13work exactly the way you need them throughout the site and with your CSS.
08:17Now if you're looking for more information on the specifics of generating assets
08:21for your site, I recommend checking out a couple of titles in the lynda.com
08:25Online Training Library.
08:26Those would be the Illustrator CS5 for Web and Interactive Design, by Mordy
08:30Golding, Fireworks CS5 Essential Training, by Jim Babbage, and Photoshop CS5 for
08:35the Web, by Jan Kabili.
08:37Those are the three programs that I use to generate most of my assets, and those
08:40are three very, very good titles in terms of describing that process to you.
08:44Now keep in mind that we're adding new titles to the library all the time,
08:47so be sure to check out the library for new titles on that subject, but those
08:51titles go into a lot more detailed information that I can in a single movie, so
08:55be sure to check those out.
Collapse this transcript
Creating initial page structure
00:00Now that we've taken a look at our mockup and discussed the initial page
00:03structure, it's time to go ahead and start coding it.
00:06Although we won't be authoring a lot of code in this exercise, what we'll be
00:11writing is extremely important, as it's the foundation for our entire site.
00:15Now as such, I'm going to have the mockup here open as we work and discuss what
00:19type of impact the decisions we make will have on our eventual layout, so I'll
00:23be switching back and forth between those mockup and the code that we create to
00:26kind of discuss how they are related.
00:29So I've got the mockup_home.ai opened, that we've been looking in the past couple
00:34of exercises, but what I'm going to do right now is switch back over to my code
00:37editor, and go ahead and crate a brand-new page for us to start working on.
00:41So I'm just going to go up to File and choose New, I'm just going to do a new file.
00:45If you're not using Aptana, just whatever code editor you're using, go ahead
00:48and create a new file.
00:49Now I'm going to place this file in the Chapter 2, 02_05 folder.
00:55So I'm just going to save it right in there, and I'm going to name it index.htm.
01:01Okay, so there is our new file.
01:03Now the first thing I'm going to do is just go ahead and code the initial page structure.
01:06I am going to start off with a doctype declaration, and I'm just going to do doctype html.
01:11You know, doctype are not case sensitive, so if you want to use a lower
01:15case here, that's fine.
01:16It doesn't really matter, just the exclamation point, DOCTYPE, and then html.
01:21Now you may be used to seeing a much longer doctype for older versions of HTML
01:25or even XHTML, but with new HTML5 doctype, we basically have the doctype there
01:30to trigger standards mode and just identify this as an HTML document.
01:34It really doesn't need to do anything besides that.
01:36Now on the line below that I'm going to go ahead and create an HTML tag, and I'm
01:40going to give myself a little bit spacing there.
01:43Inside the HTML tag the first thing I'm going to do is create a <head>, and
01:46inside the head, I'm going to go ahead and create a metatag with a character set
01:50attribute, so charset, and I'm going to make that value equal to "UTF-8," and I'll close that now.
01:58I don't need to self-close that because I'm doing HTML5.
02:00So the syntax is a little different.
02:01I don't need a closing tag.
02:03All that's really doing for us is insuring that the property character encoding
02:06is going to happen for our page.
02:08Okay, on the next line, I'm just going to go ahead and do a title, and inside
02:12the title, I'm going to type in "Welcome to Desolve." And then after the head I'm
02:17going to go ahead and create my body tag.
02:20I will give myself a little bit space between that. Awesome!
02:23Okay, I'm going to go ahead and save that. All right.
02:24So that's really just getting the base structure out of the way.
02:27We haven't really started structuring any of our page content yet, and we'll do
02:31that within the body, so this is just a very minimal HTML page
02:33that's going to give us our basic starting point.
02:35All right, so as we went back through and identified regions in our mockup earlier,
02:39remember, the first region is going to be our header.
02:42So I am going to go back into my code.
02:44We're going to start with div tags for our structure, but later on we'll go
02:47back and revise these with HTML5 tags, once we start talking a little bit
02:50more about semantics.
02:52So inside of this div tag I'm going to type in "Header content goes here," just
02:56so that we know kind of what belongs in that particular region, and I'm going to save the file.
03:01Now as I start building this, I'm going to start thinking a little bit about
03:04also the CSS that's going to be driving the layout.
03:07And if we start looking at these regions, we can see that the header region and
03:10the navigation and the banner, those all stack right one on top of another, but
03:15then down below that we have this article and our sidebar that sits side by
03:20side, and then we have finally a footer at the very bottom of the page.
03:23So we can see the normal document flow is going to account for most of this
03:26layout, but when we get to this point, we're going to need to define some type
03:29of mechanism within our layout to move this column to left-hand side and that
03:32column to the right-hand side.
03:34Okay, so I'm just going to keep structuring my code.
03:37The next step is going to be our nav element, and then that's going to be followed by banner.
03:41So in my code I'm going to come in and create a brand-new div tag.
03:45And this time inside the tag, I'm going to type in "Navigation goes here."
03:51I have another div tag below that, and for this one I'm going to type in,
03:54"Banner content goes here."
03:58Now below the banner, remember, we have our article and our sidebar. Even though
04:02those two elements are sitting side by side in our layout, structurally I have
04:05to figure out which one of these I want to come first within my content. And
04:09this is a very important decision because screen readers or other type of user
04:13agents are going to experience that content in a different way than somebody who's
04:17just say looking at it within the browser.
04:19So you really have to make a decision about which content is more important or
04:23which content should precede the other.
04:25In this case, I really want the article to come first and then the sidebar to
04:28come second, because the article is a little bit more important, and that content
04:32needs to be accessed.
04:33So I am going to go back into my code, I'll create another div tag, and here I'm
04:36just going to type in "Article content goes here," and then below that we'll do
04:43another div tag and we'll say "Sidebar goes here."
04:47Okay, now the only other element left on our page of course is this footer, so
04:51I'm going to go ahead and code the footer as well, and again just another div tag,
04:56and we're just going to say "Footer content goes here."
04:59Now it really wasn't that long ago, probably only two or three years ago, that I
05:04would begin structuring my page exactly like this, with this many divs.
05:07Now, with the advent of HTML5, and I have some more structurally or semantically,
05:11I should say, sound elements to use, and we're going to switch over to using
05:15some of those just a little bit.
05:16But I really want you just to understand how the content is being grouped, in
05:19terms of the structure of the page, before we talk too much about semantics.
05:22Now there are other decisions that you're going to make at this point. Even
05:25though this is the basic structure, you might say to yourself, okay, are there
05:29other ways that we might group and arrange this content?
05:32Header, you know, sort of by itself, Navigation, I can see that being by itself.
05:36These two items, the Article and the Sidebar, those two items definitely relate
05:40to each other in a very significant way.
05:42And in most pages we're not going to have this banner content, but in the
05:47homepage at least, I can see the banner content also being related to those
05:51two elements as well.
05:52So on the secondary pages, even though the banner isn't going to be there,
05:56I think it's still part of this main content on the page. And so, because
06:00of that, what I'm going to do is I'm going to wrap those three elements
06:03inside another div tag.
06:05So what I'm going to do is I'm just going to go above that banner and create
06:08another sort of generic div tag, and then just above the footer, I'll close it.
06:14So these three elements are going to be grouped inside of another div tag. And
06:19a lot of times when you're authoring a page, you need to make this kind of judgments,
06:22thinking about how that's going to affect the entire site. Does this grouping
06:25make sense for my secondary pages, is this something that's going to allow me
06:28some flexibility a little bit later on, and how does that impact the layout? How
06:31does grouping those elements together, is it going to make it more difficult to
06:35display this as a two-column layout?
06:36So those are all things you need to think about.
06:38Okay, so that completes the basic structure of our page.
06:41Now as you can tell, it's really nondescriptive.
06:43I mean without the text inside those div tags,
06:45we wouldn't know what was supposed to go inside of them.
06:48So because of that, the page really, at this point, isn't making any sense,
06:51semantically, at all.
06:53So next, we'll explore how we can add a little bit more meaning to our content
06:56through IDs and classes, and then we'll continue to move on to learning more
07:00about HTML5's semantic elements.
Collapse this transcript
Adding meaning with classes and IDs
00:00Although we've created the initial structure of our page, our content currently
00:04has no semantic meaning at all.
00:06In fact, without actually examining the content itself, there's no way to
00:10establish the role of the content regions or how they relate to each other.
00:14Now, actually from a layout view, that doesn't really matter.
00:17Layouts can be created using whatever type of elements and whatever organization
00:22that you want. Semantic value doesn't really play into CSS page layout at all.
00:28However, leaving out this information would not be a very good practice, and
00:32it would make it very difficult for anybody parsing our code to figure out what's going on.
00:37So with that in mind, we're going to add some organization and meaning to our
00:40page by using the class and ID attributes.
00:43Now if you've watched any of my other titles, you've probably heard me say
00:47that I often see designers make the mistake of simply using class and ID
00:51values for styling.
00:52Now, while they are certainly acceptable for that--you can use them as
00:56styling hooks if you want--
00:57their real role is to help organize and identify contents.
01:01So, let's see what they can do for us in our page and towards that end, I have
01:04the index.htm file open from the 02_06 directory. And it really is just sort of
01:10picking up right where we left off in our last movie. Okay.
01:14So, the first thing I need to do as I'm beginning to organize this is figure
01:18out, number one, whether I need a class or an ID and then what I want the name
01:23of that class or ID attribute to be. All right!
01:25So, the first thing we're going to do is we're going to go ahead and add a
01:28little bit more information about our header content.
01:30So, I'm going to go right inside the opening div tag there and I'm just going to
01:34create an ID attribute, and I'm going to give it an ID attribute of pageHeader.
01:39Okay, so let's talk about the difference between IDs and classes.
01:43Now IDs are unique, and that doesn't mean you're only allowed one ID per page,
01:48but I can only have, at this point, one element on my page named pageHeader.
01:52I cannot use that ID attribute again on this page.
01:56I can use it throughout my site to be consistent and say, okay, this is the page
02:00header on this page, and this is the page header on the next page, but on this
02:03particular page, I can only have one page header.
02:05So, when I'm trying to decide whether something should be a class attribute,
02:09which you can use classes over and over and over again on the page, or whether
02:13it should be an ID attribute, the question that I pose is, is the only time that
02:17I'm going to have this type of element on the page? And I'll only have one page header.
02:21Now, the article on the page might have its own header, the sidebar might have
02:24its own header, but in terms of the header for the entire page, this is the only one.
02:28So in that sense, it makes sense to use an ID for that.
02:32Using the same type of reasoning, I'll go into the next div tag and I'm going to add another ID.
02:37This ID is going to be mainNav.
02:39Now, the name that you use for your different elements in your regions is
02:43really up to you. And if you're working in a team environment, it's a really
02:47good idea to come up with some naming conventions throughout your site, so that
02:50everybody is using the same names, or is at least coming up with names using the same formula.
02:55You'll notice, for example, that I'm using what we call CamelCase syntax to
02:58name these, which is if I add more than one word, first word is the lowercase,
03:02every other word after that will be upper case, and they're all sort of jammed together.
03:04You can certainly use underscores, but you can't use spaces.
03:07So whatever name you give your IDs and your classes, they need to be a single word.
03:13Okay. So, I'm going to keep going.
03:15I'm going to go to the div tag that's sort of surroundings this container div
03:18tag that's surrounding all of my content, and thinking of it that way gives me a
03:22really good idea for what the ID attribute for that's going to be.
03:25I'm just going to type in "ID," and the ID for this is going to be content. Perfect!
03:31Okay, now when I go to the banner, I'm faced with another decision:
03:35Is it possible in this site that I might have more than one banner on a page?
03:40I can think of times, for example, where I might want a banner that is more
03:43informational, like this one is where it just says information about the site, and
03:46I might have another banner that is promotional, and I might have an ad in it, or
03:49I might have more than one banner with ads.
03:51So in this sense, I can probably see this site having a page that has more
03:55than one banner on it.
03:56So, instead of using an ID for banner, I'm going to use a class, and my class is
04:01going to be just that, banner. Okay, perfect!
04:03All right! I'm going to finish this up by going into each of the additional elements here.
04:07The div for my article, I'm going to go ahead and give that an ID.
04:11The ID that I'm going to give that is home.
04:12It's the homepage article.
04:14So I'm just going to call it home.
04:15On other pages I might give the article a specific name specific to that
04:19particular article or the subject of the article.
04:21In this case, it's going to be home.
04:22For the sidebar, I'll go ahead and give it an ID of sidebar. And then finally, footer,
04:28you can probably imagine what that's going to be.
04:29I'm going to go ahead and give that an ID of pageFooter.
04:32So, very much like the header, the header's page header, this is going to be page footer.
04:35That's the other thing.
04:36Other elements, other articles might have their own individual footer;
04:39this is the footer for the entire page,
04:41so I'm going to go ahead and give it an ID of pageFooter.
04:44So, now, really anybody that reads through our code, even if I deleted the
04:47contents of the div tags themselves, would understand almost immediately the
04:51structure of our content and how it all relates to each other, so what the
04:55relationship of the content is.
04:57Now, we've used classes and ID values to extend the meaning of our code, and
05:01that's going to make our pages easier to author.
05:03It's certainly going to make them easier to organize. And consequently, all of
05:07that makes it easier to style our content.
05:10So, if you have really well-structured, well-organized pages, they're just a
05:13lot easier to style.
05:14Now, in our next movie we're going to take this a little bit further by seeing
05:18how HTML5 can help us add even more meaning to our code.
Collapse this transcript
Structuring content with HTML5
00:00In our last movie we extended the meaning of our content through using class and id attributes.
00:06Prior to HTML5, that was almost all we had in the way of adding semantic value to our code.
00:12However, HTML5 introduces a whole new set of elements that help us write more
00:16specific code, and we're going to check that out by updating our homepage to be HTML5.
00:21Now, to do that, I have the index.htm file open.
00:24It's actually found in the 02_07, but it's just continuing our last exercise, so
00:30if you still have that file open, you can just go ahead and modify that one as well.
00:33That's not a problem. Now, a little bit about HTML5 before we start.
00:37I know a lot of people are saying, okay, is HTML5 ready?
00:40Can we go ahead and use this as of this recording? I am recording this early in 2012.
00:44The answer is yes, we certainly can.
00:46We most certainly can use HTML5, especially the semantic elements.
00:50Maybe some of the more complicated API stuff, like canvas and things like that,
00:54you might want to wait until it matures a little bit more within the browsers.
00:57But the structural elements, absolutely, because there's an easy way to support
01:01the structural elements in older browsers. And as we start working in some of
01:04the labs, I will discuss that.
01:06But for this movie, just focus on the fact that the HTML5 elements that we're
01:08going to be using help us organize and add meaning to our code. Okay.
01:13So the first thing I am going to do, I am just going to go through each one of
01:16these elements, and I am going to talk about whether or not an HTML5 element
01:19would be better choice for this.
01:20And the first element, when I look at this, it's pageHeader.
01:23It says, "Header content goes here," and we do have a header element in HTML5,
01:28so I am going to change this from a div tag to a header--and I have to remember
01:31to do that for both the opening and the closing tag.
01:35Now, we also are faced with the choice of should we remove the id and class
01:39attribute, because this is descriptive?
01:41It says, hey, this is header content.
01:42And in some cases the answer to that is going to be, yes, and in some cases
01:45it is going to be no.
01:47In this case, the answer is going to be no, because elements may have headers.
01:51So, for example, you might have an article or a section that also has a header.
01:55So in that sense this might not be the only header on the page, and so because
01:59of that, I do still want to identify this as being the page's header, so I will
02:03keep the id for this particular element.
02:05I will do the same thing for nav.
02:07We do have a navigational element, n-a-v, so Nav. And I have to remember to
02:12change both the opening and the closing tag and, again, I'm going to leave the
02:16id in there as well.
02:17Now, the two elements that we've used so far, header and nav, they're very
02:21descriptive in terms of what type of content that is.
02:24This is the header of the container that I am inside of.
02:26Navigation says this is a navigational aid, some type of menu, some type of
02:30construct like that.
02:31But we also have some more general-purpose content tags that we can use.
02:36So, for example, the content region right here that's sort of surrounding all
02:40three of these elements,
02:42I am going to change that from being a div tag to being a section.
02:46Now, a section of content basically says that this is grouped content, it
02:51all belongs together, but it's not something that I want to stand by itself, not freestanding.
02:57Think about it this way: Would you syndicate this entire section?
03:00In this case, no, I would not.
03:01Now, I'm looking at my banner and I'm thinking, are there any of the HTML5
03:05structural elements that can help me out here?
03:07And the answer to that is no. Sometimes div is exactly the right choice.
03:11In this case, I just need a generic grouping of content to say, hey, this is a banner.
03:15It doesn't need to be any specific section.
03:17It doesn't need to be an article, that sort of thing.
03:19But that's the other thing that I need to point out about these HTML5 structural
03:23elements is that if you look at the page as a table of contents, as maybe an
03:27outline that you could create, each of those particular tags creates a node
03:31within that outline, or an item in the table of contents.
03:34The banner, I don't really-- it's not important enough for that.
03:37I don't want it to do that.
03:38So in this case I'm using sort of a generic div tag for that.
03:41Now, for the home article, Article content goes here, that sort of gives away
03:45the tag I want to use for that.
03:47I am just going to use article.
03:48Now, the article element, what that does is it basically surrounds a grouping of
03:52content that can stand on its own, that if it was read all by itself or
03:56syndicated by itself or exported out by itself would make sense and would just
04:00live on its own outside of this page.
04:02In this case, this article certainly would do that, so I am going to choose article.
04:06Now, I am going to make another change here now.
04:09I am going to keep that id, but the element below it, the sidebar, we have a
04:13brand-new element that isn't necessarily called sidebar;
04:16it's called an aside. And I am going to replace, not only the div tag itself,
04:20but the id as well with just a generic aside tag. All right!
04:23So what is an aside?
04:24Well, an aside tag is related content.
04:27It's content that relates to siblings within its container.
04:30So in this case, because this article and the aside are inside this section,
04:35it's basically saying that this content and the aside relates to this article
04:39content without actually being a part of the article.
04:42Now, we could, if we wanted to, give it an id. It wouldn't hurt to say sidebar or
04:47related content or something like that, but in this case I think the aside works
04:51all by itself without having to necessarily need an identifier for it. And as
04:55you can tell, a lot of these are judgment calls.
04:58Now, the next thing I am going to do is I am going to go down to my very last
05:00div tag and I am going to replace it with a footer.
05:02Once again, because the different articles and sections can have footers, so
05:06we might end up with, depending upon how complex our page gets, we might end
05:09up with multiple footers, so I am going to keep that id on there as well, all right.
05:12I am going to go ahead and save that page.
05:15So now we have our basic page structure, and it's a lot richer in terms of its
05:19organization and semantic content.
05:21Now, I know one movie really isn't enough to really appreciate what HTML5 can do for your site,
05:27so if you're intrigued by this, I recommend going out and reading the HTML5
05:31specification, which you can find on w3.org. Or you can go to the WHATWG homepage
05:37which is whatwg.org, WHATWG, if you want to read through the specification. And
05:43if you're interested in looking at a few more titles of this in the lynda.com
05:47Online Training Library,
05:48I have an HTML5 First Look title and an HTML5 Structure and Semantics course
05:53that might be worth checking out as well. All right!
05:56So in our next movie, we're going to finish up by taking a closer look at how
05:59page structure and layout relate to each other as we explore building the
06:03internal structure of our page.
Collapse this transcript
Building internal structure
00:00I want to finish this chapter by taking some time to discuss many of the
00:04decisions that you'll be faced with, regarding page layout and page structure.
00:08So we're still working with the index.htm file, but this time I've opened it
00:12from the 02_08 directory. And a quick glance at the code shows us that we have a
00:20lot more content here than the last time we looked at this file.
00:23Well, the goal of this movie isn't simply to add all the remaining content;
00:27rather, I want to focus on how certain layout decisions will affect the page
00:31structure, and how you're going to need to have a strategy in place to help you
00:35make those decisions consistently across your site.
00:38Now, to help us with this, I also have the homepage mockup open, and I'm going to
00:42switch over to that.
00:43Now, this is the Illustrator file that you'll find in the Assets directory.
00:46It's the mockup_home.ai.
00:49If you don't have Illustrator, there's also a PDF file you can open up
00:53that's the same exact file, and at the end of the day, you don't really need to
00:56open them up at all, because we're not going to do anything to the mockup;
00:59we're just using it to compare the design to the actual structure of the page
01:02that we're building. Okay.
01:04The first thing I want to do is I want to zoom up on our menu so we can talk
01:08about the structure of our menu based on the design that we've created here. Okay.
01:12So each of these links has link text.
01:15In the case of our first one here, Galleries, it has an icon right beside it,
01:19and then below that we have a little tagline that is descriptive of the link. In
01:23this case, this one says, "explore our photos."
01:24Now, when we design something like this, one of the first things we have to
01:28think about in terms of creating this page is how we're going to structure and
01:31code these menu items.
01:33For example, does the icon get its own element?
01:37The tagline, "explore our photos," is that going to need to be a separate element
01:41in order for us to style it the way that we want?
01:43And this is what I was talking about when I said you need to have a consistent
01:46strategy in place, so that whenever you are faced with something like this, you
01:49kind of have an idea already of how you're going to structure this.
01:52And the rule of thumb that I used for this site, and it's one that I
01:55actually recommend that everybody uses, which is, you write the cleanest
01:59markup you possibly can.
02:01You want your markup, above anything else, to be semantic, meaning the tags
02:05actually mean something, and you only want to represent the content that needs
02:09to be represented or passed off onto any other device.
02:12As designers, we think so much about presentation and design that we forget the
02:16fact that HTML is a markup language that is designed to essentially identify
02:22content within a document and structure that.
02:24So in this case, I'm going to switch back over to our code, and I'm going to
02:28scroll down to about line 19 or so, so we can find that menu.
02:32Now, you can see that I've used an unordered list to structure the menu, and
02:37that is extremely common. Most people do that. Most menus you'll find are
02:40structured within an unordered list, for a couple of reasons.
02:43It groups all of our links together and it indicates that they're related, and
02:47it also gives us some styling hooks.
02:49We have the unordered list tag.
02:50We have the list item tags.
02:51We have the anchor or a tags, and all three of those are styling hooks for us.
02:55Now, it terms of those icons, a policy that I use for images is that if the
02:59image is purely decorative in nature, meaning it doesn't really need to be
03:03passed on to any other user agent that doesn't understand styles,
03:07if it's just for decoration, then it shouldn't really be represented by an
03:10element if you can avoid it.
03:11In this case we don't have to.
03:13We have the list item tags.
03:14We have the a tags.
03:15That icon could be a background graphic for either of those, and we could
03:18use that to style it.
03:20Now, the taglines, on the hand, that's a totally different story.
03:23We have to have a different styling hook on those;
03:25we have to have some way of separating them visually from the rest of our links.
03:30So I'm going to highlight the first tagline right here, "explore our photos," and
03:34we're going to wrap this in a tag.
03:36Now, what tag we're going to wrap it in, that matters as well.
03:39When you're faced with the decision like this where you have content that you
03:41need to structure in order to style it the way that you want to style it, if
03:45you can do that in a way that still uses semantic markup, then really it's the
03:48best of both worlds.
03:49So I'm going to go ahead and wrap that in an em tag, or an emphasis tag.
03:53And again, that does two things for us.
03:54It gives us a styling hook necessary to move the tagline down and format it the
03:58way I want to format it, but it also emphasizes the text, and within that larger
04:02link, it lets other user agents know that this particular section of text stands
04:06apart and is different than the actual link text itself. All right!
04:11So I'm going to save the file and I'm going to switch back over to my mockup.
04:15Now, next I want to focus on the area right below the menu, and that's where we
04:19have this headline, "WE LOVE URBAN PHOTOGRAPHY." We have some descriptive text to
04:23the right of that, and behind all that we have this big skyline image, and the
04:27text that's sitting on top of a box is sort of semi-translucent.
04:30So from a design perspective, it almost looks like we have four separate elements:
04:33we have the image; we have the headline; we have the text; and we have the blue box.
04:37A lot of people that are new to web design, when they have a mockup that they
04:41design or they grab a mockup from somebody else, they tend to try to put every
04:46single element within their design into an element within code, and it doesn't
04:50really have to work that way.
04:52So if you look at this, it is a distinct section--and I'm going to call this
04:56banner, so it is a distinct banner section--but really there's only two
04:59elements that really need to be represented here, and that would be the
05:02headline and the paragraph.
05:04We'll talk about how we're going to deal with the image in just a moment.
05:06I'm going to switch back over to the code, and if I scroll down, I can see that
05:11we have our text here, "We love urban photography," and then the copy right below
05:14that, but it is wrapped in a div tag.
05:17Now, a div tag is an anonymous block of content.
05:20So this one has a class of banner to identify as being the banner.
05:24We're not using a section or article tag here because it doesn't really rise to
05:27that level of importance.
05:29Now, the text, "We love urban photography," that's clearly a headline, but
05:32regarding the use of headings and paragraphs and things like that, you also need
05:37to have policies in place that determine when it's appropriate to use one
05:40heading over another.
05:41Now, the policy that I created for this site basically says, if you're the first
05:46or main headline within a distinct section of content, you're allowed to be an
05:51h1 tag, but it has to be an important section of content.
05:54In this case, remember we're using an anonymous container here, the div tag, so
05:58that tells us that the section, while distinct, really isn't that important, in
06:02terms of the actual content of the page.
06:04So in that case I'm going to go ahead and wrap that in an h2 tag instead of an h1.
06:09Now, I'm sure there's some of you out there that are like, "Oh, well, you know, I
06:12read a thing on Google that says you can only use one h1 per page."
06:14That is not the way the HTML5 specification puts it, and in fact, it's not even
06:19the way Google does their algorithm anymore, so that is not altogether accurate. Okay.
06:23The next thing I'm going to do is move down to our text, "We're betting you do
06:27to." So that is a single block of text, and since it's not a heading, it just
06:31make sense to go ahead and wrap that in a paragraph tag.
06:34Now, you may have not noticed this in the mockup, so let me switch back real quick.
06:37You can see the word Welcome right there looks a little bit bolder than
06:40the rest of the text.
06:41So we want to emphasize that as well.
06:43So I'm going to highlight the word Welcome there and I'm going to wrap that in a strong tag.
06:48Perfect! We have well-structured semantic code that is identifying this content exactly
06:53the way that we want it to, based on the strategy that we have for the site.
06:56Now, you might be saying to yourself, "Yes, yes, yes, but what about that image,
07:00and what about that blue box?"
07:01Well, again, go back to the box model properties that we've been working on.
07:05This div tag is a wrapper that surrounds all of that content, so it would be
07:10very easy to place that image as a background graphic, give it a width and
07:13height, and then just have the content sit on top of it.
07:16As for the paragraph itself, it's very easy to also give that a width and a
07:20height, assign it a color, and make the color semi-transparent, and then using
07:24some techniques that we're going to learn in upcoming chapters, move it over to
07:27the right-hand side so that it's sitting to the right of the image.
07:30So we don't really need any extraneous markup here whatsoever.
07:33Now, that would be lovely if we could structure the entire site the same way,
07:37without any extraneous markup, and when you put a strategy in place like that,
07:41you really should try your best to make sure that you follow it.
07:45However, you're always going to have to make exceptions to the rule.
07:48I'm going to go back into the mockup for just a moment and I'm going to scroll
07:51down, and I want to turn your attention to our gallery previews.
07:55So our gallery previews, we have three of them,
07:57Philadelphia, Chicago, and New York, and you can see they're structured exactly the same.
08:00We have a headline. We have the date. But what I want to really talk about here is this image.
08:05So we have an image here that lets us preview what's in the gallery.
08:08Now, I don't have it available in this particular mockup, but let's say we're
08:12going to have several different versions of this page.
08:15Eventually, later on in the title, we'll be creating what we call responsive
08:18designs, where we have one layout for desktop, another layout for tablet, and
08:22another layout for mobile devices.
08:24This image is going to change dramatically.
08:26In the tablet layout, for example, this image is going to be on one side and all
08:30the text is going to be lined up on the other side.
08:32So the practice that we've been using, which is to structure images as
08:36background images and just sort of use the semantic elements that we have
08:40already in place to put the image in, really doesn't work here.
08:43It's also decorative in nature here, so I don't really want to use an image tag,
08:47and there's actually several different reasons for that, a lot of which has to
08:49do with some responsive design stuff that we'll cover a little bit later on.
08:53So if I go back into our code, if I scroll down, I'm going to show you what
08:57we're doing in these sections.
08:59So here, you can see we have an empty div tag, and that div tag has a class of
09:03preview, and inside of it there's actually a link,
09:05so you can click on that image and go to that particular gallery.
09:08Now, that's non-semantic markup.
09:10It's extraneous markup.
09:11If you were to write really just clean code, that probably wouldn't be there.
09:16So it's one thing to have your policies in place, but every now and then you're
09:20going to have to deviate from those policies in order to get your layout to look
09:24exactly the way you want it to.
09:26You just want to make sure that that is the exception and not the rule.
09:30So when you have to resort to that, that's fine, but if you find yourself doing
09:33that over and over and over again, perhaps your page isn't structured quite as
09:37well as you need it to be.
09:39Now, more than anything else, I'm hoping that this exercise illustrates how your
09:43page structure is actually going to be the results of dozens of small decisions
09:47that you're going to have to make based on the coding standards that you've set
09:51for your page and the layout goals that your mockup is giving you.
09:54There is no one-size-fits-all, and if you gave the same exact mockup, if I
09:59took that Illustrator file and gave it to three different web designers, I
10:03would bet you money that we would get back three different page structures in
10:06terms of the HTML code.
10:08That is why it's so important to learn the rules for creating standards-
10:12compliant code and for learning as many different layout techniques as possible.
10:16That way you'll have the ability to make really good decisions regarding page
10:20structure while having several different options available to you to achieve
10:24the desired layout.
Collapse this transcript
3. Working with Floats
Floating elements
00:00Of all the different properties involved in CSS layout, perhaps the most
00:04important to learn and master is the float property.
00:08Floating allows us to alter the normal position of an element by moving it to
00:12the leftmost or rightmost edge of its parent.
00:15That may not sound like such a big deal, but floating forms the basis for the
00:20overwhelming majority of CSS site layouts.
00:23Of course there's a little more to it than just moving elements from side to
00:26side, and in this chapter we're going to focus on the ins and outs of floating.
00:31We're going to start by examining the property in action. So I have the float.htm
00:36file open from the 03_01 directory.
00:40I'm just going to give you a brief preview of the structure of the page.
00:44If I scroll down into the body of it, you'll notice that we've got a couple of
00:47introductory paragraphs, and then we have three empty div tags with the classes
00:52elements 1, 2 and 3, and that is who we're going to control.
00:56Now if I preview this in my browser, you can see kind of what's going on here.
01:02We've got normal document flow happening.
01:04We have element1, element2, and element3 just stacked right on top to each other,
01:08so we're using CSS to apply a background color and to give them a defined width
01:12and height, that sort of thing, but normal document flow is just stacking them
01:16one on top of each other.
01:16Now, when we use the float property, we have four possible values.
01:21You can see those right here.
01:22We've left, right, inherit, and none.
01:24Now left is going to take that object and float it to the far leftmost edge of
01:30its parent container.
01:31Float right is going to do the same thing in the opposite direction.
01:34It is just going to float it all the way over to the rightmost edge of
01:37its parent container.
01:39Inherit basically just says, hey, whatever my parents is doing, I want to do
01:42that as well. And none, will basically turn a float off.
01:45So none says, okay, no floating is occurring on this element.
01:49For the most part, left is kind of what happens to objects anyway.
01:53Now I say for the most part because the default for an object is that it is
01:57actually lined up with the leftmost edge of its parent.
02:00However, one of the things that floating does that normal document flow does not
02:05is it actually removes the object from normal document flow.
02:08Now this is can have some really interesting side effects, which we're
02:11going to see as we sort of experiment with the float property.
02:14All right, I'm going to go back into my code and if I scroll right up into my
02:18styles, I can see that I already have some selectors written for element1,
02:22element2, and element3, and that's who we want to focus on, so I'm going to go
02:26first up to element1 and I'll add a new line. And I'm just going to do a float
02:30property, so just float, and then I'm going to give it a value of right, so we
02:34can see it in action.
02:34I'm going to save that, go back to my browser, test it, and you can see what happens.
02:40Element1 floats all the way over here to the rightmost edge of its parent, but
02:45notice what happened to element2 and element3. Because element1 is removed from
02:51normal document flow, element2 and element3 move up to occupy the space where 1
02:58used to be, because they don't see 1 anymore. 1's removed from normal document
03:01flow, so they act as if 1 does not exist; they just move straight up to occupy
03:07that now-empty space.
03:09Now that can have some very interesting consequences, as we're going to see in just a moment.
03:12All right, so I'm going to go back into my code, and I'm going to change float:
03:16right to float: left.
03:18So I'm going to save that, go back to my browser.
03:22Now, remember, this element1, the brown one is element2, the orange one is
03:27element3, and if I update my page, something very curious happens.
03:31Now we floated element1 to the left, so it looks like its back where
03:34it used to be, so it's all the way up against the left edge of its container
03:37element, but it looks like element2 has disappeared.
03:41Well, in reality it hasn't disappeared. Remember, floating removes it from
03:45normal document flow, right, so element2 and element3 moved up to occupy that space.
03:51It's just that element1 happens to be there as well.
03:55So that gives us another little piece of the puzzle here.
03:57When we float an element and remove it from normal document flow, it is
04:00allowed to overlap other elements, and it's also going to be stacked on top of them initially.
04:05So in this case element2 is really just sort of hiding, if you will, just
04:10underneath element1.
04:11Okay let's go back in our code and let's take a look at what happens when we
04:16float multiple elements.
04:17I'm also going to float element2 to the left, and I'm going to float elements3 to left.
04:23Now what this is going to do, if I save this and test it, you can see that it
04:29basically stacks these elements one on top of another.
04:32So essentially, 1 is floating all the way as far left as it can get.
04:362 is also removed from normal document flow.
04:39It's floated as far left as it can go now, as far left as it can go is on
04:44the rightmost edge of the first element, and element3 follows suit.
04:48A lot of people actually use this technique to create horizontal menus or photo
04:54galleries or things like that. As a matter of fact, if I go back into the code
04:57and for each one of these go ahead and do a margin right, let's just do a
05:02margin right of 1em, right--so I'm going to save that and just copy it and paste
05:08it for each one of these guy.
05:09So, essentially what I am going to do is I'm giving each one of these guys a
05:11right margin, so I'm going to save this, preview it in the browser, and you can
05:16see, you can control the spacing between them through the use of margins. So
05:19essentially you can just sort of create this nice horizontal menu or horizontal
05:23list or photo galleries or things like that.
05:26Now I don't know if you caught this or not, but this white background that's
05:32part of the body tag used to go underneath these guys and now it does not.
05:37So what's happening here is something that's known as container collapse.
05:41So the container, in this case the body tag, that was containing these floats,
05:45so the parent-containing element that was containing these floats, no longer is containing them.
05:50The reason for that is because remember, each of these items are floated.
05:54That means that each one of those is removed from normal document flow, so the
05:57parent doesn't see them. It doesn't think that it contains anymore, so it
06:01collapses upwards until it finds content again, and sometimes this will result
06:06in a parent element collapsing entirely so you can't even see it.
06:10So parent container collapse is definitely something we have to deal with, and
06:14we're going to talk about a few different ways that we can deal with it in this chapter.
06:18There's one more thing I want to talk about here in regards to floated element,
06:21before we get into the individual specifics of them.
06:24That's basically the rules that govern how many of these sort of floated elements
06:28will fit on one line.
06:29It all has to do with the available width of those elements.
06:33So I'm going to go back into my code for a second, and you can see, we have our
06:37body element here, and our body element has a restricted width of 600 pixels.
06:43So if I go in and I just increase the size of my div tag, so let's say I make
06:47their width 200 pixels. Okay, so obviously two times three, there are three of them,
06:52should be 600 pixels, but remember, we have some margins between them as well.
06:56So if I save this and preview that in the browser, you can see they no longer
07:00fit on that one line.
07:01So what happens is is once they run out available space, the next floated element
07:06just breaks down and resumes floating on another line.
07:10In terms of layouts, that's something that can really cause problems.
07:14We call that column collapse. Because if each of these were a column in a three-
07:18column layout, right now we wouldn't have a three-column layout; we would have one
07:21column breaking over here.
07:22So that's another issue that we're going to discuss how to deal with it as we
07:26get deeper into floats as this chapter goes on.
07:29So in terms of just you know overall concept, floating is pretty easy to grasp;
07:33however, there's a lot more involved in creating float-based layouts than just
07:37utilizing the float property.
07:39Now for the rest of the chapter, we're going to focus on float-related concepts
07:42that will help take the mystery out of controlling floated elements, and we're
07:46going to start by introducing the clear property in our next movie.
Collapse this transcript
Clearing floats
00:00As important as the float property is to creating layouts, it would be
00:03practically worthless without the use of the clear property.
00:07The clear property allows us to turn floats off and restore normal document flow
00:11and together, floating and clearing allows us to exercise precise control over
00:16the flow of our layouts.
00:18To demonstrate that, I have the clearing.htm file open, and you can find that
00:22in 03_02 directory.
00:26Now, structurally it's very similar to the file we were using in our last
00:30exercise. As a matter of fact, if I scroll down, I can see that the only big
00:33difference is that instead of only having three elements, we now have six.
00:37For styling purposes, you'll notice we've got a lot going on just in the regular
00:41div rather than installed individually.
00:44All of the divs are being floated to the left, and they all have a little bit of
00:47margin to the right to give us a little bit of spacing. Okay.
00:50So I'm going to save this, and I want to preview this in my browser.
00:55Currently, you can see all the elements are floated to the left.
00:58Now, they would all appear on one line except for the fact that we have a
01:02constraint going on right here with the page.
01:04So if the page were a little bit wider, they would all show up on the same line,
01:07but because it's constrained at 600 pixels, when we run out of room, the next
01:11floated element breaks down to the line and floats resume.
01:15So how does clearing work?
01:16Well, let's say that we wanted to have two rows of equal items, so we wanted two
01:21rows of three, right?
01:23We have a couple of values available to us here when we do clearing:
01:27we have left, right, both, inherit, and none.
01:30Now, none is turning clearing off.
01:31Inherit basically says whatever my parent has, that's what I want, so it's these
01:35three that really matter for what we're doing in this exercise. So clear:
01:39left, essentially it tells an element nothing can float to my left; clear:
01:44right says, all right, nothing is allowed to float to my right; clear:
01:47both says nothing is allowed to float to my right or my left.
01:50So if something is cleared, it essentially breaks down to the next line and
01:54reestablishes normal document flow.
01:57So currently, for example, number two, this two element right here, has one
02:01floating to its left, three has two floating to its left, so forth and so on.
02:06So if I go back into my code and I go right down here to element number four and
02:12I just say, okay, let's go ahead and clear that element to the left.
02:17So if I save this, go back in my browser and preview, that is exactly what
02:21happens. Four breaks down on the next line and reestablishes normal document flow.
02:26Now, just because normal document flow is reestablished doesn't mean that you
02:29can't then at that very moment go ahead and resume floating.
02:32We're certainly doing that here.
02:34So four is still floated to the left, five and six are still floated to the
02:37left, but for just that moment we're clearing those floats and reestablishing
02:42that normal document flow on another line.
02:44Well, that works great. Right works the same way.
02:47I'm sure you can envision and imagine how that works.
02:49But what about both? Shouldn't we know whether an element is going to going to
02:52float to the right or left; why would we need both?
02:55To demonstrate that, I'm going to go back into our code and just modify it slightly.
03:00I'm going to remove the clear that we just applied to 4.
03:02Then I'm going to go up to my just default divs.
03:05I'm going to remove float to the left, so go ahead and get rid of that.
03:09I'm going to remove the margin-right, and then I'm going to change the width to 150 pixels.
03:15So now all of my elements are just stacked, normal document flow, they're 150
03:20pixels wide, and we no longer have any right margin on them. Okay.
03:24Now, let me go down to the first two elements, and we're going to modify
03:27those slightly as well.
03:28We're going to take element number one and I'm going to float that to the right.
03:33I'm also going to set its width to 50 pixels.
03:37For element number two, I'm going to float that to the left.
03:41So we've got one element floated to the right, we've got another element floated
03:44to the left, and I'm going to give it a width of 50 pixels as well. Okay.
03:48Now, if I save that and go in and refresh this, it looks weird, but bear with me here.
03:53So element number 1 is floating all the way over to the right-hand side.
03:56element2 is floating over to the left-hand side.
03:593, 4, 5, and 6 are in normal document flow.
04:01Remember, when I float 1 and 2, I removed them from normal document flow, so the
04:05remaining elements move up to occupy that space.
04:08So essentially, two is just sort of overlapping three, and that's one of
04:12the reasons why we made those other elements wider, so we could sort of see that overlap.
04:17So if I go back into my code--and element3 is who we want to focus on right now.
04:21If I tell element3 clear to the right, and check that,
04:28now what happens is it says, okay, number one, you can't float to my right, so
04:31it's going to go down onto the next line,
04:33it's going to reestablish normal document flow, and the page layout just sort of continues.
04:38But what if I said instead of clear: right, clear:
04:42left, what would happen then?
04:43So if I save this and preview it, no change. Okay.
04:47So in this case clear: right and clear:
04:49left do exactly the same thing, because three basically had one floating to its
04:53right and two floating to its left, so really they're both doing the same thing,
04:56so why in the world would I need clear: both?
04:59Right now we're working with a very static layout in terms of, we know exactly
05:03the element dimensions of both of these, but it isn't always that simple.
05:07Now, one thing I want to point out here is, do you notice how we've kind of
05:10messed with source order a little bit?
05:12The first element that any sort of user agent would encounter in the code would
05:15be element number one, but visually we're almost making two look like it comes
05:20before one, because we typically read from left to right.
05:25So let me go back into the code.
05:26Let's say we go up to element one and we change its height to 200 pixels. So I'm
05:32going to go ahead and save that and preview it.
05:36And what happens is is now element one, because it is 200 pixels tall, can float
05:43to the right of all these elements.
05:45These elements are simply going to move up and underneath floated elements, but
05:49since we have clear: left applied to three,
05:52it's breaking down here and reestablishing normal document flow, but we're not
05:55preventing elements from floating to its right, in this case, the number one.
06:00If I went back into my code and I told element three, no, no, no, no, clear to
06:04the right please, and save that and test it, okay,
06:08now it's clearing to the right, it's reestablishing normal document flow, and
06:11it's not letting either of those float left or right of it.
06:15But again, this all has to do about the height of the elements.
06:18Again, if I come back into element two and I change its height so that it's 250
06:24pixels, save that and test it, and you can see, we get overlap again.
06:30So to prevent overlap, when you really don't know, you're not really sure if
06:34elements are going to be floated to its left, you're not really sure if elements
06:36are going to be floated to the right, you're not really sure in terms of the
06:39height of the various elements,
06:41you can always come in and say instead clear: both.
06:46And now it doesn't matter who is taller.
06:49It's simply going to clear both of them.
06:51So right now two is taller than one, so it's clearing that and reestablishing
06:54normal document flow.
06:55But if I take element two, take its height back to, say, 100 pixels, you can see
07:01it's still not going to go above where one is either. That's clearing.
07:06It gives us a precise amount of control over which elements we allow to float
07:11and when we want to stop floating and resume normal document flow.
07:15Clearing also allows us to deal with another very tricky problem related to
07:19floats, and that's how containing elements will collapse when child elements are floated.
07:24We're going to take a closer look at that in the next movie.
Collapse this transcript
Containing floats
00:00Earlier in this chapter, we saw one of the unfortunate side effects of floating elements.
00:05Remember, floated elements are removed from normal flow.
00:08That means that the height of their parent elements then collapse as if the
00:12floated elements inside them simply weren't there.
00:16This can cause a whole host of undesirable results, such as disappearing
00:20backgrounds and overlapping content.
00:22Now, fortunately, we have a few ways to prevent this from happening.
00:26So we have the containing.htm file open, and you can find that in the 03_03 folder.
00:32If I scroll down just a little bit, you can see that we have a section. Inside
00:37that we have three divs, with the classes One, Two, and Three.
00:40If I were to preview just the default styles in a browser, you can see these
00:44elements are just stacking, normal document flow, one on top of each other:
00:47One, Two, and Three.
00:49Well, currently the height of the section is being controlled by the
00:53elements inside of it.
00:54That's actually quite common.
00:56Height is generally considered unless it's set explicitly to be auto.
00:59That means the contents of an element define its height.
01:03So once we float these elements and remove them from normal flow, then section
01:07really doesn't have any height and it's just going to collapse.
01:09So I am going to go back into my code, and what I am going to do is, each one of
01:14these div tags, One, Two, and Three, I am just going to go ahead and float those to the left.
01:19So I am going to do that for each of them, so that we can observe the parent collapsing.
01:26So I am going to go back into the browser, refresh the page, and you can see
01:30that's exactly what's happening now:
01:32One, Two, and Three are floating.
01:33They're removed from normal document flow. Section doesn't see it inside of it
01:37anymore, and so it collapses.
01:38And in fact, the only thing we're seeing here right now, guys, is the padding
01:42that's set inside the section.
01:44So let's take a look at a couple of ways that we can help contain those floated
01:48elements inside the section. Okay.
01:51So if I go back into my code, well, one of the ways to do this is if you have
01:55an element that's underneath your floated elements and you clear that to
02:00restore normal document flow, then the parent container will go all the way
02:03down to contain it as well.
02:05So if I go down here and right past the last div tag I add another
02:09paragraph, and inside of that paragraph I can just say "Clearing floats help to contain them,"
02:17so if I save this, and I can just come right up here and create another selector
02:23for the paragraph and just tell that to clear: both.
02:28We added a new element underneath our floated elements, and then we told that
02:32element to clear, meaning reestablish normal flow.
02:35So if I go back into my browser and refresh that, you can see what happens.
02:39Our paragraph shows up down here.
02:40It's cleared, meaning normal flow is reestablished, and our section is now
02:46extending down to contain that, so that's one way of containing it.
02:49Of course to do that we have to have another element below our floated elements,
02:53and that's not always going to be the case with your layouts.
02:56So there are a couple of other methods that you can use.
02:58Let's take a look at those.
03:00So if I come back into my code, I am going to get rid of that paragraph,
03:04selector and the paragraph itself, there we go. And what I am going to do is I
03:09am going to go up to the section selector, the one that's controlling its
03:12styling, and I am going to use the overflow property.
03:15Now, the overflow property is a property that basically tells the browser what
03:19to do when the contents of a box no longer fit inside of it.
03:24So we have a few options available to us. Hidden is one.
03:27Hidden just says, hey, clip off any content that overflows.
03:31Auto would create scrollbars, either horizontally or vertically, based upon the
03:35flow of the content.
03:36And Scroll will force scrollbars in.
03:39Visible, by the way, just causes that content to be visible and overlap.
03:42Well, there is a side effect to using overflow.
03:45So I am going to type in overflow: hidden.
03:47Now, anything other than Visible will work with it, so it could be Hidden, Auto, or Scroll.
03:51Now, obviously Scroll is going to force scrollbars to show up, and you don't want that.
03:55Auto will work, and it will work almost all the times with no side effects, but
04:00occasionally you're going to have a time when scrollbars show up for just weird
04:04reasons, because the floated elements inside of it perhaps are forcing a certain
04:08amount of auto flow.
04:09So by saying overflow:
04:10hidden, we're essentially saying, hey, we want to set overflow on this element,
04:14but we don't want any scrollbars to show up.
04:15So if I save this and test it, now even without the extra content in the div
04:21tag, the float is being contained and that's the overflow property that is doing that for us.
04:26That's going to work for you about say 90% of the time, but there are going to
04:31be certain times when that overflow:
04:33hidden property is going to come back to bite you.
04:35Let me show you what I mean.
04:37So if I go back into my code, let's say I take the third div tag here, and I
04:40apply something relatively new, like box-shadow.
04:43So if I do box-shadow--now, when you're doing a box-shadow, that's just really,
04:48it's sort of a drop-shadow for this div tag.
04:50So I am going to give 10-pixel offset to the X, 10-pixel Y offset, give it a
04:5610-pixel blur, 10-pixel spread, and then the color for the shadow I will just go
05:01ahead and make that black, so #000.
05:03If I save this--and those are really high offsets, but I really wanted you to
05:08be able to see this, if I go back into the browser and load that up, you can
05:11see what's happening.
05:13Our drop shadow is showing up, but because the section has overflow set to
05:17hidden, it's going to hide this portion of the box shadow.
05:21It's not going to expand to fill that.
05:23So there are times when overflow:
05:25hidden, even though it will contain floats for you, is going to give you some
05:28negative side effects.
05:30We do have one more technique that can work for us.
05:32So I am going to go ahead and get rid of the box-shadow.
05:34I am going to go ahead and get rid of overflow:
05:37hidden, and I want to modify one last little thing here.
05:42You will notice, for example, that body has a background color of white and a
05:45width of 600 pixels.
05:47So I am going to take the section and I am going to set its width to say 480 pixels.
05:52Now, why am I doing that?
05:53Well, if I save this, and if I preview this in my browser, you can see now the
05:57section is actually not as wide as body, and you can also see that body sort of
06:03has that white background color here.
06:05Why am I doing that?
06:06Well, another method that you can use to contain floats is that if you float the
06:10parent element, meaning the container element, if you float that one as well,
06:14all floats inside of that will also be contained.
06:17So if I take section and I apply float:
06:21left to it as well, save that, and preview that in the browser,
06:25you can see what happens.
06:25The float is now contained.
06:27But it sort of has a chain reaction.
06:30You'll notice, for example, that the body tag is now collapsing as well. We no
06:33longer see that white background because all of the content inside of it is now
06:37floated, and so it collapses.
06:39So a lot of times that works, but you end up having to go back through
06:44your layout and clear all these other items that you wouldn't have to clear previously.
06:48So it's not always the right answer either.
06:50So we've seen three ways that allow us to prevent container collapsing.
06:55We can clear child elements that appear below floated elements.
06:59We can set the overflow property of the parent container to either Hidden, Auto,
07:03or Scroll. Or we can set a float value on the containing element itself.
07:08Now, as we've seen, each one of these methods have their pros and their cons,
07:13and you're going to have to very carefully consider when they're appropriate
07:16within your own layouts.
07:17Now, personally, I use either one of two methods:
07:20I will either use the overflow:
07:22hidden method, which is going to contain my floats, or I am going to use a
07:27clearing method for containing all the elements that have floated children, so
07:30clearing an element below it.
07:31Now, specifically, I use a method called clearfix, because the method I showed
07:36you in this particular example requires there an element to be underneath it,
07:41but the clearfix method doesn't.
07:43Now, since the syntax of clearfix needs a little bit of explanation, we're going
07:47to go ahead and discuss that in a separate movie next.
Collapse this transcript
Clearfix technique
00:00By now, you might be a little tired of hearing me talk about containing
00:04and clearing floats.
00:05However, because it is so important to page layout, I want to take a closer look
00:10at a specific clearing technique called clearfix, and how it can help make
00:14containing the floats in your layouts a little bit easier.
00:17Open up the clearfix.htm file, which you can find in 03_04 directory. And if I
00:25scroll down, I can see that I have a section with a class of clearfix applied to
00:31it, and then I have three divs:
00:33One, Two, and Three, and all three of those divs are currently right now
00:37floating to the left.
00:38So we kind of know what to expect here;
00:40we expect the section, that parent-containing element, to collapse.
00:44So I am going to preview this in my browser, and that is exactly what happens.
00:48All three of those elements are floating and so the containing element collapses.
00:53So back in the day when we first started attempting to clear the floats, one of
00:57the things that we used to do is we would put in our CSS these class styles and
01:02we would just call them like clear: left, clear: right, clear:
01:06both, whatever we want to clear, and we would apply that property to them.
01:10And then whenever we would need clearing to apply, we would apply that class.
01:13Now, that still happens a good bit, and I usually have some of those classes in
01:18my CSS so that I can clear elements on the page when they exist.
01:22But in this instance where you've got a situation where you've got three
01:26elements floating inside of a container and nothing underneath it, what a lot of
01:30people would do is they would come in and they would do this.
01:32They would put in an element like this, like a line break, and then they would
01:36apply the class clear: left or clear: both or clear:
01:40right, just something sort of invisible, like a line break or a span tag or things like that.
01:45So if I save this now and preview it, you can see that now it's containing the floats.
01:50The problem with that approach is that you end up putting in a lot of extra
01:54markup, markup that just really should not be there and that's not required for
01:58the page to read the way that you want it to.
02:00So it was kind of a hack, if you will.
02:03Over the course of working with this type of clearing, some very bright people
02:08came up with the clearfix method, and we're just going to go through it
02:11piece by piece so you can kind of see what this does for us.
02:14So I am going to come up here to my clear:
02:16left style that I added and I am going to modify this a little bit.
02:19First off, I am going to change it to clearfix.
02:23Now, some people use CF, some people use clearfix, some people are using the
02:27class called group so that it has some semantic value to it. It's whatever you want to do.
02:33This takes advantage of not a relatively new CSS property but one that only over
02:38the last couple of years has been supported across the board by browsers.
02:41So this technique still has problems with older browsers, and we'll talk about
02:46how to deal with those in just a moment.
02:48But it uses something called generated content.
02:50So after clearfix, I am going to type in a colon, and then I am going to type in after.
02:55So what this is saying is it's saying that, hey, any element that this class is
02:59applied to, I want you to generate some content directly after this.
03:04What I am going to do is inside the selector, I am going to go ahead and
03:08create some content.
03:09So I am just going to use the content property and I am going to create a blank
03:13section of content here.
03:14So the empty quotation marks that you're seeing here, that's an empty literal string.
03:18So it means create some content, but don't create anything. Put nothing there.
03:22So it's just going to put sort of this empty literal string in there, if you will.
03:26Now, in older versions of Firefox, like versions 3.5 and below, you would need a
03:31period there, because trying to pass nothing in this generate content would cause
03:34Firefox to go a little squirrely, but since 3.5 and above, it's had no problem
03:39with that whatsoever.
03:40Now, the next thing I am going to do is change the display property to block.
03:44So this is basically saying that this empty bit of content here, this no text,
03:49go ahead and display that as a block-level element.
03:52And then finally, I am going to come in and set clear to both.
03:57It's generating content.
03:58Now it's still inside the closing tag, just so you know where generated content goes.
04:02It's till inside the closing tag, but it's at the very bottom, and it's the last
04:06thing in the element now.
04:07So we have the sort of empty piece of white space that's displaying as block, and
04:11it's being told to clear.
04:13So now all I need to do is go down and make sure the clearfix class has been
04:17applied, and indeed it has. And if I save this and now if I test this in my
04:21browser, you can see, once again, our floats are being contained, and now we're
04:27doing it without any extraneous markup. We've got no extra markup going on whatsoever.
04:31We're just using the generated content.
04:34It would be lovely if the story would end right here, but unfortunately,
04:38obviously this is a technique that has evolved over the years, because certain
04:42browsers have trouble with parts of this.
04:45One of the things that causes problems in Internet Explorer is the whole
04:49concept of generated content before and after.
04:51But essentially what happens is is you end up with some collapsing margins on
04:56the top of the element that you don't have towards the bottom of it.
04:59So we're going to modify this a little bit.
05:01I am going to change this by adding another group selector to this.
05:06I am going to do clearfix:before, clearfix:after.
05:11So I am modifying the selector itself so that it's generating content at the top
05:15of the element and the bottom of the element as well.
05:18I am also going to change the display from block to table.
05:22This is a recent addition, and I am going to show you where you can read more about that.
05:25But what that does for you, instead of generating block-level content, it
05:29generates sort of empty table cell. You won't see it.
05:32It's not visible, and you won't have the collapsing margin problem, which is awesome.
05:36Now, we don't need to clear the content at the top of the element.
05:38If we do that, it can really negatively impact our floating.
05:42So I am going to cut that out of that selector and then create a brand-new
05:46selector down here to do my clearings.
05:48So I will do clearfix:after, and we're going to do clear: both.
05:55So what this is doing is it's generating content at the top and the bottom of your element.
06:00It's generating an empty table cell, which will prevent any collapse of margins
06:04from happening, and then the one on the bottom is being cleared.
06:08So if I save this and preview and test it, we don't notice any difference.
06:13It really works the same, but now it's much more solid, in terms of the code involved.
06:17Now, I mentioned that this method still does cause problems with certain
06:21browsers, and Internet Explorer is one of those.
06:24Now, I am going to defend Internet Explorer here because it's not so much a bug
06:28in Internet Explorer;
06:29it's how Internet Explorer deals with elements on the page that has something
06:33that we call hasLayout.
06:35If you're really serious about page layout in CSS, you need to go up to
06:39Microsoft's developer blog and read the history of hasLayout and kind of what
06:45hasLayout is and how Internet Explorer deals with it.
06:48So what we're going to do is we're going to create another style tag.
06:51Now, this thing may seem kind of weird, but we're going to create one style tag
06:55inside of another one, and inside this one, I am going to do a little comment
06:58here that says For IE < 8 (trigger hasLayout).
07:06So essentially, what we're doing is for older versions of Internet Explorer, we
07:09need to go ahead and trigger hasLayout.
07:11Now, this needs to be a comment, so I really should have commented that out at
07:14the beginning of this.
07:15So bookend that statement with a /**/. And below that I am going to type in
07:21my clearfix selector, and I am going to do a zoom of 1, which is 100% zoom,
07:28which means don't zoom.
07:29But just by applying zoom on the element, you trigger, in Internet Explorer,
07:33hasLayout, and it will go ahead and do your clearing for you.
07:36Now, obviously I only want Internet Explorer to apply these styles,
07:41so what I'm going to do now is I am going to wrap that sort of style tag that
07:45we've got there in a conditional comment for us that is Internet-Explorer-specific.
07:50So I am going to open up a tag and do a little comment here, !--, and then in
07:56bracket notation, I am going to type in "if lt," less than, "IE 8."
08:02Internet Explorer is the only one that's going to respond to that. And then I'll
08:07go ahead and close that, and then I am going to go down to the very bottom of
08:11this, and once again, the explanation point, and in bracket notation I am going to
08:15do endif to end that conditional comment and that ends with -->. There we go!
08:23You can just copy and paste that from the finished file if you like, if you
08:26don't want to go to the trouble of typing a lot of stuff out.
08:28Now typically, what I would do, obviously most of your styles are going to be
08:31linked to pages, and typically I will have a link to a style sheet that is IE-
08:36specific, and these styles will just be sitting into that style sheet.
08:39I won't actually go to the trouble of doing that on every page.
08:41That doesn't seem very efficient, does it?
08:43So I am going to go ahead and save my file. And I know that seems a little
08:47complicated at first,
08:48but the beauty of this, you just write the code once in your styles, and you
08:52simply apply the clearfix class to any element that you need to contain the
08:56floats inside of it.
08:57It's very simple, and it may be slightly non-semantic in terms of the classes
09:02you are applying, but it gets the job done without a lot of extra markup, which is even better.
09:07If you want to learn more about the clearfix syntax, its history and
09:11understanding the concepts behind it, I have a few articles that I am going
09:15to recommend you read.
09:17Now, I would start with--and I know I am going to pronounce his name wrong,
09:20so I am terribly sorry about this--Thierry Koblentz.
09:24I hope that's close.
09:26But Thierry is, he wrote a series of articles on clearfix that is just wonderful.
09:31Now, the first one is "Everything you Know about Clearfix is Wrong" and this does
09:35a great job of explaining exactly how clearfix works and how those cross-browser
09:41issues evolved, specifically Internet Explorer.
09:43You can find this on his blog tjkdesign.com. Just search for everything you know
09:48about clearfix is wrong.
09:49He also wrote another article called "clearfix Reloaded + overflow:hidden
09:55Demystified," where he talks more on those two methods and sort of refines the
09:58syntax for clearfix, and you can find this on Yahoo!'s User Interface Blog, the
10:03yuiblog.com. Just search again for clearfix-reloaded.
10:07And then finally, I want to point out Nicolas Gallagher's really, really cool
10:11article, "A new micro clearfix hack."
10:14This goes ahead and takes what Thierry was doing and extends his work a little bit.
10:18It reduces the amount of code and is, for the most part, the syntax that we used in our movie.
10:23So if you check out this syntax, it's going to be very similar to what we used in our file.
10:26Now, I know this syntax can be a little bit hard to remember,
10:30so for the techniques like clearfix, I typically recommend saving the code as a
10:34snippet and then just simply reusing it for each project.
Collapse this transcript
Floating inline elements
00:00So far we've focused on floating large block-level elements, as they are the
00:05building blocks of all of your initial page layouts.
00:08Now in this movie, we're going to focus on floating inline-level elements and
00:12how they affect the elements around them.
00:14So I have inline.htm open from the 03_05.
00:19Just to give you some idea as to what we're going to be doing, I'm going to go
00:21ahead and preview this in a browser.
00:23So here I've opened up the page in Safari.
00:26Now essentially, I have three sections that we're going to work with.
00:29First, we're going to take a look at using floats to wrap text around an image,
00:32so we have an image right here.
00:34We have some text following it.
00:35And right now, we've got a bunch of empty space.
00:37It would look a lot better if we could just sort of wrap the text around that image.
00:41The next thing we're going to do is create a drop cap here, and then finally
00:44we're going to be styling more of a complex region, like a product description,
00:48and seeing how floating can help us with that as well.
00:50So this is more about how floating could be used to help tweak layouts and sort
00:54of improve them a little bit.
00:55So we have those three sections.
00:57So I'm going to go back into my code and just look at the actual structure of
01:00the first section that we're going to work with, right here.
01:03You can see we have a section here with a class called textWrap.
01:07Inside that, we have an image which is inside our first paragraph, and then we
01:11just follow that with some content.
01:14So what I'm going to do is I'm going to go up to about line 64 or 65 where I
01:20have this little comment.
01:21So the first thing we're going to do is we're going to target that specific
01:23image, and I'm going to do that by using the textWrap class selector, and then
01:28I'll write a descendent selector that adds the image to that.
01:30So it's saying, hey, find any image that is also in an element with the class of
01:34textWrap, so we're targeting that one specific element.
01:37Okay, so what I'm going to do here is I'm going to make it look a little different.
01:41I'm going to apply a border to it. I'm going to put a 1-pixel solid border on it,
01:45and we're going to make that gray, so #333.
01:48To wrap the text around it, we're just going to go ahead and float that to the
01:52left, just that simple.
01:53So I'm going to save this, go back into my browser, and refresh,
01:59and indeed the text does wrap around it.
02:00There is my outline, and as you can see, oh, that doesn't look great.
02:04When we float text which is inside of a paragraph or even outside of a paragraph
02:08and it has paragraph text next to it, the text itself is going to wrap around
02:12the image, but it goes right up to the edge of it.
02:14So how can we prevent that?
02:16Well, we can use margins to help create sort of a gap or a gutter, if you will,
02:20between the image and the text itself.
02:22So if I go back into my code, right after the float:
02:26left, I'm going to apply a margin to this.
02:28And the margin that I'm going to do here is 0 for top, 1em for the right-hand
02:34side, 1em for the bottom, and 0 for the left-hand side.
02:40So saving this and then previewing it again gives me that nice little sort of
02:45standoff if you will between the content and the image itself.
02:49So that's really nice.
02:50But what happens if you don't want a specific element to wrap around this?
02:55So, for example, this headline.
02:57This headline really should go down. Maybe it's the beginning of another section
03:00and it doesn't pertain to this image.
03:03The same rules to clearing floats apply to inline elements as they do to
03:07block-level elements.
03:08So if I go back into my code, I can just very quickly write a selector that
03:13says textWrap h3 to target the heading, and then just tell it to clear both.
03:22So if I save that, preview it, boom,
03:26now our headline drops right back down again.
03:28Now I want to clear up something, because typically, there is a little bit of a
03:31misconception with what's actually happening here.
03:33If you remember from our earlier demos, when we float something to the left, the
03:37content underneath it ignores it and then comes up under it.
03:41And now you might be asking yourself, how come the text just didn't go right
03:44up under the image?
03:46Well, that has everything to do with what we call a line box. What's a line box?
03:50Well, if I highlight this line of text, you can see it.
03:53That is a line box.
03:54So when line boxes encounter a floated element, what they do is they shorten
03:59themselves so that they can appear beside the floated element.
04:04The paragraph itself did come up under the image.
04:07That may take you a second to sort of process that, but I can show you that
04:11visually, and I'm just going to do this really quick.
04:12This is going to be a really quick little demo of this concept that I'm
04:15talking about here.
04:16If I come down, I can create another style called textWrap p, and I'm just
04:22going to give it a background color. And I'll just give it a background color of say c4d4cb.
04:31Why so complicated? I don't know.
04:33And I also want to add a little bit of left margin to my image so that you can
04:37really kind of see that background color floating up behind the image.
04:40So if I save this and again preview it, you can see exactly what's happening here.
04:45The paragraph itself is moving up to occupy the space the image used to, because
04:49it's removed from normal document flow.
04:51So there is paragraph one. There is paragraph two.
04:53They're floating up underneath it.
04:55The heading is clearing it, so it doesn't do that.
04:57Now the line boxes themselves, however, instead of going up underneath it, the
05:02line boxes shorten themselves so they will float beside it.
05:05So this is going to work perfectly as long as you don't have a background color
05:09behind your paragraph that might shine through like this.
05:12I'm going to get rid of that background color and I'm going to get rid of that
05:1610 pixels' worth of margin. All right!
05:18So let's go down and talk about our drop caps.
05:21I'm going to refresh this. There we go.
05:23So we want to create a little drop cap here in this paragraph.
05:26And if I go down into the actual code, you can see that I have another section
05:30with the class of drop cap, and I want it to affect that first paragraph.
05:34So I'm going to go up to my styles, and I'm going to start doing this in
05:38the drop cap styles.
05:40I'm going to write a pretty complex selector here.
05:42If you watched my previous course, the CSS: Core Concepts, I went into a lot of
05:48different selector types,
05:49so this will be sort of a refresher for that particular course.
05:52If you didn't take that course and you're curious about those types of
05:54selectors, you can go back and watch the section on targeting content.
05:58So I'm going to type in drop cap-- remember that's the class that's applied to the
06:02section--space, p, so that's targeting paragraphs inside the drop cap.
06:07Now here's where it gets complex.
06:08I'm going to do a colon and I'm going to do first-of-type.
06:15So what this is saying is it's saying, hey, when you find the first
06:18paragraph inside this class, go ahead and apply it, so with the first
06:21paragraph inside this drop cap.
06:23Now we're not done yet because right after first-of-type,
06:25I'm going to type in another colon, and I'm going to type in first-letter.
06:31That is incredibly specific.
06:33We're saying, hey, go find the first paragraph inside of an element with drop
06:37cap applied and then inside that, find the first letter.
06:41There are other ways to do this.
06:42You could wrap that letter in a span tag, for example, and target the span tag,
06:46but this is a way of doing it without adding any additional markup to your code.
06:50Now inside this, let's just go ahead and start styling it.
06:53We're going to float the letter to the left.
06:55That's going to wrap all the rest of the text around it.
06:58Then we're going to change things, like for example, I'm going to change the font-family.
07:01I'm going to change the font-family to Georgia, Times, and then serif.
07:07I'm going to set the font-size, obviously I want drop caps to be a little bit larger.
07:11I'm going to set font-size to 4ems.
07:12I'm going to control its spacing, or its placement, if you will, with margins.
07:18So top margin I'm going to do 0.
07:20Right margin I'm going to do 5 pixels--
07:22that'll hold the text away from the drop cap a little bit--and then 0 for the rest of them.
07:27So really all I'm trying to do there is give it a little offset.
07:29I'm going to have to do something here because of the way that we're doing this.
07:32And so instead of targeting an element in a span tag, we use this first letter selector.
07:39And that causes a little bit of problems with cross browsers and believe it or
07:43not, it has nothing to do with Internet Explorer.
07:44It has everything to do with WebKit-based browsers like Safari and Chrome that
07:48we're going to be using, and Firefox.
07:50Here's how this works.
07:51We're going to go ahead and do line-height.
07:53line-height is essentially the space between lines or the height of the box that
07:59the text is contained inside of.
08:01And I'm going to set that to 0.7.
08:04So here's essentially what happens.
08:06In Firefox, you can't put line-height on a first-letter pseudo-selector.
08:12What happens is, anytime you use this, the line-height is inherited from the
08:16paragraph that it's contained inside of, so I can't do that.
08:20But WebKit allows me to. So Safari and Chrome will allow me to do that.
08:24So Safari and Chrome compute the value on the letter itself, whereas Firefox
08:30gives it the computed value of the parent.
08:32So that means that in Firefox and inside Google Chrome, their line-heights won't
08:36match up, so they won't align to each other.
08:38If you were to test this page without doing that, in both of those browsers you
08:41would see that the drop cap doesn't line up.
08:44So essentially, you have to pass a value for line-height in there and kind
08:48of play around with that value so that Chrome and Safari will match up with Firefox.
08:53I typically start it at about 0.8 and kind of play around with it from there.
08:56That seems to be a good starting point.
08:58I'm going to save this, go back into my browser, and test it. And boom!
09:03There's our drop cap.
09:04So by taking that one letter and floating to the left and wrapping the text
09:08around it, all that's really required is just doing some additional styling.
09:12Do we want to change the font, do we want to change the color, how big do we
09:15want to make it, that sort of thing.
09:17Okay, now the final thing we're going to do here with floating inline elements
09:20is create a more complex region down here.
09:22So what if this were like a product description?
09:24And we have a little shot here, maybe it's a print that's for sale.
09:28We want to float the text next to it, and then we have this More information and $19.95.
09:31Maybe we want those to appear sort of side by side, but style them very differently.
09:38Floating can really help us with that.
09:40So I'm going to get back into our code, just so we can see the structure of what
09:44it is that we're going to be styling.
09:45We have a section with a class of complex.
09:47It has an image inside the paragraph. And then we have a paragraph for the class of more,
09:53that's our More information, and a paragraph for the class of price.
09:57So going back up to our complex styles, you can see already we have what's
10:02controlling the background of that and the border around it and that sort of thing.
10:06Well, we're going to need to add some things here.
10:09So after that one, I'm going to type in .complex img, so we're targeting any
10:13images inside the section with the class of complex.
10:18And for that, we're going to float images to the left.
10:20This will help wrap the text around it.
10:22And then I'm going to apply a margin to the right of 1em. And of course that's
10:26going to help us hold the text away from it.
10:28So if I save this and test it, it's something we've already done, so we're kind
10:31of expecting how that should look, and it looks exactly the way we expect it to.
10:36Our text is wrapping around it, but More information and $19.95,
10:39we're going to have to do some additional tweaking of that to get it to look the
10:43way that we want it to.
10:45So I'm going to come down and create another selector for more, so .more.
10:50I'm going to float that text to the left.
10:53I'm going to set its margin to 0, but I'm going to give it a margin to the left.
10:59So I'm basically clearing out any existing margins that that paragraph might
11:02have and then just coming in and setting a margin to the left.
11:05And I'm going to do 14ems. Now, that's a lot.
11:08I'm going to show you why I'm doing it in just a moment.
11:11Now for line-height, I'm going to go ahead and set line-height to 1.6ems.
11:19So essentially again, that's going to give it sort of a taller line box that
11:22it's going to be sitting inside of.
11:25For padding, I'm going to do 0 padding top and bottom, 5 pixels right and left,
11:29and then I'm going to give it a background,
11:31so that's one of the reasons why we needed padding because we're giving it
11:33a background color.
11:35And the background color is going to be #3b2f24. And then finally, the color of
11:40the text itself will be white.
11:43Okay, now if I save this and I test it, you can see More information.
11:47It's floated to the left, but it's got this really wide margin left
11:52that's pushing it away from these other elements.
11:54Now why did I do that?
11:55Because I want price to show up on the right-hand side of that, so we're going
11:58to need to style price a little bit differently.
12:00So I'm going to come in and I've got one more selector to make, and that's going
12:04to be the .price selector.
12:07So for that one, I'm going to float it to the right.
12:09I'm going to set its margin to 0, so stripping out any default margins that
12:14might exist for the paragraph.
12:15I'm going to change its font size to 1.6ems because we're really trying to
12:20emphasize that price.
12:21I'm going to set the font-weight to bold. And then finally, I'm going to set the
12:28margin-top to -.3ems.
12:33So that's going to move it up a little bit.
12:34So I'll save that, preview it.
12:37It looks great. Oh, but it doesn't.
12:41More information and $19.95 are showing up the way I want them to, lining up
12:45right side of each other, but there is that pesky container collapse showing up again.
12:49So I go back into my code, I'm going to go back up to where our complex is, and
12:54I'm just going to set overflow to hidden.
12:58Now you're probably wondering, hey, wait a minute.
13:00You went through that whole clearfix thing last time. Would that have worked? Sure!
13:04I've actually got the clearfix class right here.
13:08So I could have come down and applied clearfix to that, and it would have worked
13:12just fine as well, but this way I don't have to apply another class.
13:15And if this is static, meaning I know exactly the content that's going to do inside of it--
13:20I know it's not going to have any special effects like drop shadows or things
13:22like that that could get clipped off-- then overflow hidden works just fine.
13:26So I'm going to save this, preview that in my page, and then boom!
13:30There is my completed complex region.
13:32I want to point something out about the price and More information.
13:36You can see that More information comes first, price comes second.
13:40And I definitely wanted price over here on the right-hand side and I wanted More
13:44information on the left-hand side.
13:45I could've floated both of them to the right and then added the margin this way,
13:49or I could have done it the way I did it within here, which is floating More
13:52information to the left, applying a big margin to it to kind of move it over as
13:56far as I want, and then floating the price to the right.
13:59There's always more than one way to style a layout.
14:01So I guess from this, you can probably guess that the purpose of this exercise
14:06wasn't really how to teach you how to style drop caps or wrap text or even
14:10create a product detail region.
14:11That really wasn't the goal.
14:13What I wanted to illustrate was how flexible the float property is when styling
14:17various elements all the way throughout your design, and to show you how often
14:21you have to be creative when dealing with some of the details.
14:25Now keep in mind how important the use of margins are when floating inline
14:28elements and when to utilize the clear property to go ahead and reestablish
14:32normal document flow.
Collapse this transcript
Two-column floated layouts
00:00Now that we've had some time to become familiar with the concepts of using
00:03floats and how they affect surrounding elements, I want to show you a few common
00:08approaches for creating float-based layouts.
00:10I'm going to start with the most common layout, and that is a two-column layout.
00:14Now we're going to be doing three different versions of exactly the same layout.
00:19So the same HTML structure, we're just going to do it three different ways.
00:24And that's really to show you guys that often it's a personal choice as to how
00:29you control the layouts.
00:31Okay, so we have three files that I've opened up here.
00:35If you look in the 03_06 directory, you're going to find 2col_v1, 2col_v2, and 2col_v3.
00:42Just go ahead and open all three of them up, so that you know the structure of
00:46the page that we're going to be working with here.
00:47Very simple structure, just some basic building blocks if you will.
00:51Inside the body we have a header, which is typical. We have a section, which is
00:54going to be our first column.
00:56We have an aside, which is the sidebar.
00:58It's going to form our second column.
00:59And then finally, at the bottom we have a footer.
01:02So that's a pretty basic page layout: header at the top, two columns side to
01:06side, footer at the bottom.
01:08If I go back up to our body tag, I can see that the width of the page is set to
01:14a fixed value of 960 pixels.
01:18So what we're going to do is we're going to have a left column that is 640
01:23pixels wide and a right column that is 300 pixels wide,
01:27and that's going to leave us with 20 pixels' worth of space between the two
01:30columns, or a 20-pixel gutter for you designers out there.
01:35So the first method of doing this is what I call float one margin for the other.
01:40And so for our first column, column1, I'm going to come in and I'm going to go
01:45ahead and float column1 to the left.
01:49The next thing I'm going to do is give it a defined width, because we want our
01:52column to be a specific size.
01:53I'm going to give it a width of 600 pixels.
01:55Now I know I said, hey, the left column is going to be 640 pixels,
02:00so I'm also going to do some padding and the padding is going to be 20 pixels.
02:05So remember, 20 pixels to the left, 20 pixels to the right, of padding, adds up
02:09to a total of 640 pixels.
02:11Okay, if I save this and preview this in one of my browsers, you can see what happens here.
02:18This is where the really important stuff goes.
02:21This is where the related content goes.
02:22So here's my left column. Here's my sidebar.
02:25Now the problem is the sidebar is coming up underneath, so we're basically
02:29seeing both that and the footer slide up underneath the left column here.
02:34So I'm going to go back into my code, and I'm going to start styling my column2.
02:40So for column2, what I'm going to do here is I'm going to set padding to 20
02:43pixels as well, so a consistent padding all the way throughout.
02:47And instead of setting a defined width on column2, what I'm going to do is I'm
02:51going to do a margin-left.
02:54What margin-left is going to do for me-- think about what's happening here.
02:57column2 is right now sliding up underneath column1.
03:00By applying a wide enough margin on the left-hand side,
03:03I'm creating enough empty room for that left-hand column to display.
03:08So what I'm going to do for the margin- left is a really big value, 660 pixels.
03:12That's 640 pixels for the width of the column on the left-hand side and then
03:17another 20 pixels for gutter space right there in the middle.
03:20Now for our footer, one of the things we're always going to do for our footer is
03:24we're going to go ahead and clear that.
03:26So I'm just going to type in clear: both.
03:27Now the reason for that is you don't want the footer sliding up underneath the
03:31left column as well.
03:32So I'm going to save that, go back into my browser, refresh, and you can see,
03:37there's our two-column layout.
03:38Footer at the bottom, and then we have a ton of left margin over here for
03:42this column, and we give it just enough to create that extra 20 pixels' worth of space.
03:47So that's version 1.
03:48Now I'm going to go over to 2col_v2.htm, and we're going to start styling that one.
03:54Okay, now for this one, we're going to float both columns, and we're going to
03:58float both of them to the left.
04:00So if I go down into my code for column1, it's going look very similar.
04:05I'm going to go ahead and float that to the left.
04:07I'm going to go ahead and give it a width of 600 pixels and give it padding of
04:1520 pixels on both sides.
04:17So that gives us a total of 640 pixels, and we're floating that to the left.
04:21Now for column2, I'm going to come in also and float that column to the left.
04:26The next thing I'm going to do is I'm going to give it a padding of 20 pixels
04:30as well, and this time I'm going to give it a width of 260 pixels.
04:36Now if you think about this, 260, and we've got 20, so it gives me 300 pixels
04:40worth of space there.
04:42I've got 640 pixels here.
04:44But the one thing I don't have is the gap, the gutter, the space between them.
04:48And when you're dealing with two elements floating side by side, one of the best
04:51ways to get that is just use a margin.
04:53So for column1, I'm going to come up and apply a margin-right of exactly the
04:59value we need, which is 20 pixels, so there's our gutter.
05:02So if I save this and preview this in our browser as well, you can see it's
05:08working perfectly, except for the footer, which is now stretching all the way
05:11up underneath that.
05:12So remember, we always have to go down to that footer and clear it.
05:16And so I'm going to ahead and Clear Both.
05:18So if I save that and preview it again,
05:22now the footer is in its right place, and I've got my columns.
05:25Now the thing about using this approach where you're floating one to the left
05:28and then you float another one to the left, it works just fine, but the big
05:32problem here is going to be column drop.
05:34Column drop happens when the content no longer fits within that space.
05:38So that could be content overflowing out of one of the columns. It could be that
05:42we just got the math wrong.
05:43So if I go back into this, if I just tweaked it a little bit, for example, if I
05:47come into the column2 and I change the width to 270 pixels
05:51that's just 10 pixels' worth of difference here, and I preview that,
05:55you can see the second column drops because now there's no longer any space for it over here.
06:00So using this type of a layout, you need to be very, very precise with your
06:03measurements and make sure that your content inside of this is going to adhere
06:07to those boundaries as well.
06:09So let's do our last version, 2col_v3, and this happens to be my personal favorite.
06:14We're still going to float both of the columns, but we're going to float one to
06:17the left and we're going to float the other one to the right.
06:20So I'm going to scroll down and find column1.
06:25And for that, we're going to float that to the left, we're going to give it a
06:28defined width of 600 pixels, and we're going to give it a padding of 20 pixels.
06:36Now the thing I like about this is we're going to let the defined widths of the
06:40two columns determine how wide the gutter is.
06:42We don't need to worry about margins.
06:44We don't need to worry about it because we're going to take column 2 and instead
06:47of floating it to the left, we're going to float that to the right.
06:51Next thing, we're going to do is we're just going to go ahead and give it
06:5420 pixels' worth of padding, just as we did before, and we'll set the width to 260 pixels.
06:59But notice this time I'm not having to set any margin on it to sort of push it over in place.
07:04By floating it to the right, it's automatically going to snap to the right-hand side.
07:08And that of course means that if the body increases in size, the only thing
07:11that's really going to happen negatively is that my gutter is going to get
07:14a little bit wider. All right!
07:15The next thing I need to do is come down to my footer, just clear both, save that.
07:21And if I preview this in my browser as well, you can see there is my two-column layout.
07:28Here's the method we just tried, there's the method of floating both of them to
07:32the left, and there's the method of floating one to the left and using margins.
07:36Now visually, if I tab through these, can you tell the difference? No, of course not.
07:41They look exactly the same, but they're using three very different methods to
07:44achieve the same layout.
07:46As I mentioned, I really do prefer that last method, but really this lesson
07:50isn't about what my preferences are;
07:52it was about showing you multiple techniques that you can use.
07:55Now in many cases, the source order of your content, in terms of the order that
07:59it's encountered or the complexity of your page, might drive you to use one of
08:03these methods over another.
08:05Now most of the time, it's just personal preference, but by understanding
08:08exactly how these techniques work, you're going to be better prepared to react
08:12to circumstances where you have to be a little creative in order to achieve
08:16the correct results.
Collapse this transcript
Three-column floated layouts
00:00Let's take the techniques that we used in our last movie a little bit further
00:04by creating three-column layouts.
00:06Now, although the source order doesn't matter that much when creating two-column
00:10layouts, it does come into play a little bit when considering different
00:13techniques when building three-column layouts.
00:16So as with our two-column layouts, we're going to take a look at three different
00:19ways to create roughly the same layout.
00:22So I have three files open here,
00:23the 3col_v1.htm, 3col_v2.htm, and 3col_v3.htm, and you can find those in
00:29the 03_07 directory.
00:32Now, we're going to start with our version one first.
00:34Now, I just want to go down and show you the page structure really quickly here.
00:38So we have a header, just like we did last time, and then we have three sections.
00:42Notice that one just follows the other one, column1, column2, and column3, and
00:45then we have our footer where our copyright stuff goes.
00:48So just to show you what that looks like in normal document flow, we just have
00:53these three sections following one right after the other.
00:57So I'm going to go back up into my code, and the first thing I want to is just
01:02sort of explain the math that we're faced with here.
01:05The overall width of our page is 960, and what I want to have is a gap of 20
01:10pixels' worth of space between my columns.
01:13That leaves us with 940 pixels' worth of space for the columns themselves.
01:17If I divide that by 3, I get 306.6666666, and it just keeps going.
01:24Now, since we're using 20 pixels' worth of padding on both the left and right of
01:28each column to hold off the interior contents, that actually leaves us with
01:33266.6666666 for each column.
01:37Let's go to our column1 selector.
01:41What I'm going to do here is I'm just going to float column1 to the left.
01:44I'm going to give it that padding that we just talked about, 20 pixels' worth of
01:48padding, and then for width, we're going to go ahead and do 266.6 pixels.
01:54Now, browsers can handle a lot more decimals than that, so if you do
01:58complicated math and you get it out to, say, five or six digits out, your
02:03browsers can handle that.
02:04So the only reason I'm doing it right here is just for brevity's sake, if you will.
02:08And then finally, I'm going to do a margin-right here of 20 pixels.
02:15So that's the first one.
02:17Now, I'm just going to go ahead and copy these properties, the ones that I just
02:21entered in, because I'm going to do the exact same properties for column2 and
02:26the exact same properties for column3, except column3 doesn't need the margin to
02:31the right. Now the margin to the right really is only giving us spacing here on
02:36the first two columns.
02:37So if I save this, go back into my browser, and refresh my page, you can see,
02:43there is our three-column layout.
02:44Now, take a look. We have a little bit of a gap right there.
02:49The reason for that gap: all of our values are actually working out to 959.8 pixels.
02:57And this is one of the things that happens when you do float:
03:00left for all three columns, even if you're doing like a photo gallery and you
03:04float all your images to the left, a lot of times you're going to see just
03:07really weird little spacing issues like that.
03:10So let me show you a quick and easy way to fix this.
03:12I'm going to go to my column3 and I'm going to change it from float: left to float:
03:17right. And then what I'm going to do is I'm going to go up to column2 and I'm going to
03:21remove that margin-right for spacing.
03:23I don't need that anymore, because the widths of the two columns are going to
03:27give me the space, and remember, one is floating to the right and the other one
03:29is floating to the left.
03:31And by the way, notice that the footer has clearing:
03:34both already applied to it, so I saved us a little time there.
03:37So if I save this and preview that, now that snugs right up against that. And
03:42this gap happens to be 0.2 pixels off from this gap, but I don't think most
03:46people are going to notice that. So that works.
03:49Here we have a three-column layout.
03:51We're floating the first two columns to the left.
03:53We're floating the third column to the right, controlling the gap between
03:56the first two through a margin, controlling the gap between the second two
03:59there through just the available space leftover after the column widths have been calculated.
04:04Let's go in and do our second version now. Source order does come into
04:09play in this one, and I'm going to scroll down and show you the source order
04:12and how it differs.
04:14Our first column says, "This is where the really important stuff goes." Notice
04:17this time the second column says, "I have to be the far right column."
04:21So in terms of the way that we're doing this particular layout, this one has to
04:24be the far-right column.
04:25And then we have our third column which says, "I have to be the middle column."
04:30And all that has to do with the fact that I'm going to be floating column1 to
04:33the left, column2 to the right, allowing column3 to come up underneath them, and
04:38then control the width of column3 through the use of margins.
04:41So it's kind of a take on the very first two-column layout we did, but this
04:45time instead of having a column on just one side, we're going to have columns on both sides.
04:49It will probably get a little clearer as we do it.
04:52So I'm going to go up to column number 1, and what we're going to do for that is
04:56we're going to float this to the left,
04:59we're going to give it a padding of 20 pixels, and we're going to give it a
05:04width of 266 pixels.
05:07I'm sorry, I'm tired of dealing with all those decimals.
05:10For the second column, column2 here, we're going to float this one to the right,
05:15we're going to give it padding of 20 pixels as well, and again, we're going to
05:21give it a width of 266 pixels too.
05:24Now, if I save this and if I just preview this in the browser right now, you can
05:28see what's happening here: the first column is floating to the left; the second
05:32column is floating to the right; and then the third column is coming up
05:35underneath both of them.
05:36You can even see right here the height doesn't match anymore because it's just
05:39coming up between them.
05:41Now, our footer is clearing, so we don't need to really worry about that.
05:44So what we're going to do with this middle column is we're going to set its
05:46height equal to the other two, and we're going to control the width of it
05:50through margins, so we're going to do an equal amount margins on both sides.
05:53So I'm going to come into column3, and in column3 I'm going to go ahead and do a
05:58padding of 20 pixels, same as the other two;
06:00I'm going to go ahead and define its width same as the other two, 266 pixels;
06:05but for margin, here I'm going to do a technique that we learned a little bit earlier.
06:08I'm going to do a margin of 0 and then auto for left and right.
06:11If you remember what that does is it basically says, okay, how much space is
06:14left to my right, how much space is left to my left? Go ahead and even those
06:18values out, which really centers the content.
06:20So this is going to center this 266-pixel column in the middle of the two other 266 columns.
06:27So if I save this and preview it, there's our spacing.
06:31The margins on both sides are centering this column in the page.
06:34It's coming up and floating up underneath these two floated elements, and then
06:39our footer is clearing.
06:40So there's another three-column layout.
06:42It looks very similar to our first one, except for the fact that the source order
06:46is different, and in the second layout the source order matters. Okay.
06:50Let's do our final version, so I'm going to go to 3col_v3.
06:55We're going to take a look at creating a three-column layout when the structure
06:58of the page is may be a little bit more complex.
07:00Now, oftentimes when you have multiple columns, certain columns relate to each
07:04other while other columns don't.
07:06So in this case, if I scroll down and look at the code, I can see that the first
07:11two columns are actually contained with an article.
07:13Now, this is very, very common.
07:15You might have a main content section where you have two columns that sort of
07:19relate to each other and then you might have a sidebar that you still want to
07:23form a three-column layout, but these guys sort of belong together.
07:27In order to do this, we're actually going to create a two-column layout for the
07:32article in the aside and then add in two additional columns inside the article.
07:36So you can sort of nest these layouts one inside of each other to create
07:40more complex pages.
07:41We're going to scroll up, and we're going to need to add a couple of selectors here.
07:45So after heading1, I'm going to come in and do my article.
07:48So basically right now I'm just kind of doing the two-column layout.
07:52For article, I'm going to float that to the left, I'm going to give it a width
07:57of 634 pixels, and then I'm going to follow that with the aside.
08:03And with the aside, I'm going to float that to the right, give it a width of 266
08:09pixels, give it a height of 500 pixels.
08:13Now, normally, you want the content to form the height, but in this case we're
08:16just putting a fake height in there so we can really kind of see the layout.
08:19And then finally, padding of 20 pixels and a background of rgb(173, 169, 130).
08:32Okay. If I save this, and I test that in my browser, here is my article--
08:37it's over here in the left-hand side--and here's my aside sidebar.
08:42Now I need to form the two columns inside that article.
08:46And really, we already know how to do that, because we've done it several times.
08:49So underneath the aside, I'm going to create a .col1 selector.
08:53And inside that, I'm going to go ahead and assign it a background color.
08:57So that's going to be rgb(237, 228, 214).
09:06Underneath that, we're going to go ahead and give it the same fake height of 500 pixels.
09:10We're going to float this to the left.
09:13Now, remember, it's floating inside of our article.
09:15We're going to go ahead and give it a padding of 20 pixels, and we're going to
09:20give it a width of 266 pixels. Really the same measurements we've been using for
09:25all of our columns so far.
09:26col2 is going to be done in a very similar fashion.
09:30In this case, I'm going to go ahead and give this a background of rgb(219, 126, 64).
09:41I'm going to give it a height of 500 pixels, just the same as the other ones.
09:45And for this one, I'm going to float it to the right.
09:47Now, remember, its rightmost edge is going to go up against the rightmost edge
09:50of the article, which we've already given that a defined width, so this is going
09:53to not butt it up against the other column on the right-hand side,
09:56It's going to give us the space between that.
09:59and then I'm going to go ahead and give it a padding of 20 pixels, and a width,
10:04just like the other ones, 266 pixels.
10:07So if I save that and preview it, what do you know? There we go.
10:11So now we have a three-column layout, three versions of them.
10:15These two look almost exactly the same.
10:18These two look a little different, simply because the source order changes.
10:22So when you get to doing a three-column layout, they're a little bit more complex.
10:26Typically, source order does matter and you're going to have certain things
10:30grouped together, but you still have identical three-column layouts using three
10:34different techniques.
10:35These two columns are grouped within an article which is floated to the left.
10:38We have an aside that's floated to the right.
10:40In this one, we float one column to the right, another column to the left, and
10:43allow the middle column to have some margins which define it, and it gets to just
10:47float up in the middle of the two.
10:49And then finally, our initial layout, if you remember this one, we have one
10:52floating to the left, another one floating to the left, and we could have this
10:55one floating to the left, but in order to make it a little bit easier to get it
10:58snug up against the edge of this,
10:59we're floating this one to the right.
11:02I'm trying to stress the fact that if you just master floating and clearing, you
11:06can pretty much create the basic structure of any layout that you can dream up.
11:10Now, over time you're going to find that you begin to develop strategies around
11:14page structure and layout techniques that work with your projects.
11:17Just make sure, as you begin to create your own layouts and your own
11:20techniques, that you're focusing on writing the most efficient code possible
11:24and creating layouts that give you the most flexibility in presenting your
11:28content the way you want.
Collapse this transcript
Column height considerations
00:00In the previous layout examples we've been forcing our columns to be fixed height
00:04so that we can visualize our sample layouts a little bit better.
00:08Now, in practice, the height of columns is typically determined by the
00:11content inside of them.
00:12Now, most of the time designer simply factor this into their design and build
00:16layouts that work with a natural height of columns.
00:19But what happens if you need a column to stretch to match the height of another
00:23column, or if you want it to take up all the vertical space available?
00:27Well, in those cases you're going to have to carefully plan your layouts around
00:30those requirements, and you'll need to understand exactly how browsers calculate
00:34the height of elements.
00:35So before we can get into specific layout techniques around controlling element
00:39height, I want to take a closer look at how browsers calculate height itself.
00:44If I scroll down, I can see it's a very simple structure.
00:48So inside the body tag we have a section here, and we just have a
00:52paragraph inside of that.
00:53If I preview that in my browser, you can see that right now the body's width is
00:58set to 960 pixels and the section's width is set to 50%.
01:02Now, the reason for that is, once we start stretching these columns, I want you
01:06to be able to tell what's the body and what's the section.
01:09Now, most people's misunderstanding of height comes from them understanding what
01:14happens with width, and let me show you that really quickly here.
01:17So I go into the file and I remove the width of the body and I remove the
01:22width of section and preview that in a browser,
01:27what happens is both the body stretches to 100% and any block-level content
01:32inside of it, in this case the section, stretches to 100% as well.
01:35So the default for block-level elements inside the browsers if no width is
01:39defined, they're just going to stretch out to be 100% the width of their parent
01:43element, in this case the entire viewport.
01:45So a lot of times designers, when they set height to 100%, they're expecting the
01:49same behavior, that the column will just go ahead and stretch to the height of
01:53the window, and when that doesn't happen, it kind of confuses them,
01:56so let's go and take a look at that.
01:57So I am going to go back into my code here, and I am just going to undo those widths.
02:02I sort of need those so that we can see what's going on.
02:05I'm going to come down here to section and I am just going to set
02:07section's height to 100%.
02:11So I am going to save that, preview that in my browser, and bam, nothing.
02:16Well, that's not absolutely true.
02:19So what's happening here is that the height of an element is set to auto by default.
02:24What that means is is the contents of the element defines its height.
02:29So in this case, body's height is being defined by the content inside the section.
02:36So when I tell the section to be 100%, it's saying be just as tall as the body.
02:41The body is getting the height from the section,
02:43so in essence, we're saying be 100% of yourself.
02:49It might be fulfilling, but it doesn't work the way we anticipate it.
02:53So what we have to do is we have to go in and set explicit heights on every single
02:58parent element above the one that we are targeting.
03:00So if I come in for body and I say height 100%,
03:08unfortunately, that doesn't work either, because there is another tag above body
03:13and, yes, that's the HTML tag.
03:14So if I come here and say, okay, height 100% on the HTML tag as well, now if I
03:21save this and test it, yay, everything is stretching, including the body and
03:27including our section.
03:29Now, all seems well, but it just sort of seems well.
03:32Remember, it's stretching to the height of the viewport, which means the height of
03:34the available space, so what happens if the content is longer than that?
03:40So if I come down, for example, here to my section and I grab this paragraph and copy it.
03:46Let's say I am going to paste it a few times.
03:48One, two, three, four, five, six, seven should be enough.
03:54So if I save this and go back into my browser and test it, you can see it's
03:59still stretching to 100% of the height.
04:00But when happens when I scroll? Oh, hmm.
04:04So the initial column height and the initial body height is the height of the viewport.
04:10So when I begin to scroll, that height that was fixed at that size no
04:14longer stretches to fit the viewport, so now my column is just sort of overflowing here.
04:20So that is really not what I'm wanting.
04:24So if I go back into my section, however, and instead of section I say height
04:28100%, what if I said minimum height,
04:31meaning, is the shortest it can be as 100%.
04:34So if I save this, and go back in and test it, aha, now it stretches, but
04:39unfortunately, the body doesn't.
04:41If I go back into the body and tell its minimal height to be 100% as well,
04:45trying this method, you're almost always sort of chasing your own tail.
04:49Now, the other reason this works--and I am just going to scroll back up at
04:52the top and do that again--is because we don't really have any padding or
04:55anything like that.
04:56If I go back in and reduce this content-- and I am not going to get rid of all of it.
05:02I just want to get rid of about half of it. And if I go back up to my section
05:06here, and I add say some padding.
05:10So if come in and add padding of 10 pixels.
05:13Now, here's the thing about using percentages for both height and width.
05:17When you do a padding of 10 pixels, height is added to that.
05:20Those effects are cumulative.
05:22So the 10 pixels' worth of padding top and 10 pixels' worth of padding on the
05:26bottom are added to that 100% height; they're not factored into it. So if I save this,
05:31again, test it in my browser, what happens is is even though the content
05:35doesn't stretch all the way down, you see we've got a page that can scroll and
05:38this is still overlapping, and that's that extra spacing that we are getting
05:42through our padding.
05:44So it's really frustrating.
05:46Now, we have a really simple layout here.
05:48As our layouts get more and more complex, these issues that we are taking a look
05:53at right now, they magnify, because you're going to have to start dealing with
05:55the height of several elements above this one.
05:59So does this mean that you should never try to have elements' content stretch to
06:03fit the screen, or its parent?
06:05No, that's not what it means at all.
06:06But you really need to understand how height works in order to properly plan and
06:11execute that type of layout.
06:13Typically, for example, there is a lot of wrapping div tags and things like that
06:16involved to keep some of those negative side effects from happening.
06:19Now, I recommend checking out a few gallery sites online.
06:23A few of my favorites are cssbeauty.com, where you can look at some sample sites
06:27that designers have submitted.
06:29Pattern Tap, which is awesome.
06:31They have different patterns for footers and buttons and sidebars and headers
06:36and all sorts of really cool things.
06:38There is the bestcssvault.com, which is, again, user-submitted sites.
06:42And you could even go to like wordpress.org and check out some of their sample themes.
06:47Now, what you're going to find, almost overwhelmingly, is that most
06:50designers simply design their sites so that variable-height columns don't
06:54negatively impact it.
06:56Sometimes knowing the limitations of a technique is just as important to
07:00designing it as knowing its strengths.
Collapse this transcript
Creating equal-height columns
00:00In our last movie we talked a little about the limitations of specifically
00:04setting a height on a column.
00:06Now we also talked about how difficult it is to stretch the height of a column
00:10to match its parent.
00:11There are times, however, when crating layouts of equal-column heights is
00:15desirable, and for those times, you're going to need to be creative in how you
00:19plan and structure your layouts.
00:21Let's take a look at a couple of methods that you can use to do that.
00:24So I have columns.htm open and columns_faux.htm open.
00:30So I am going to show you what's going on here in the columns one first.
00:33If I look at the structure page, I have a header, followed by an article.
00:37So I have a wrapper that's going around these different sections.
00:40I have one section that has a dialog from Aristotle in it, and then I have
00:44another section, or in aside in this case, that has a dialog from Socrates, or as
00:50Bill and Ted used to say, Socrates.
00:51Then at the very bottom, I have a footer. Cool!
00:56So the technique we are going to use has actually been around for a very long
01:00time, at least in terms of the history of the web.
01:04If you go out to positioniseverything.net and search for One True Layout, you'll
01:08find the origins of this particular technique.
01:11So what we are going to do is we are going to kind of do a little bit of a hack.
01:15We are going to force those two columns, the section and the aside,
01:19we are going to force them way, way, way down, and when I say way down, I mean
01:24further than anyone will ever scroll, by using bottom padding.
01:29But then what we are going to do is we are going to assign the exact same
01:33negative value to a margin bottom for it, which is going to take that wrapper
01:38and it's going to move that wrapper back up, just like a window shade would.
01:42Then what we'll do is we'll clip off the content that overflows by using
01:47overflow hidden on the parent article.
01:49Trust me, it works great!
01:51Now if you're wondering how this looks in the browser right now, let me
01:53switch over to show you.
01:55You can see that we have a two-column layout here.
01:57Here's Aristotle and here is Socrates. And as I scroll down I can see that
02:01Socrates article is a little bit longer, based upon the width of the column.
02:05So I am going go back into my code and what I am going to do is scroll down into
02:09the styles where I have this little "styles go here," right on line 73 and I am
02:15going to go ahead and create a section, aside.
02:19So it's going to be one of my selectors. And inside this, I am going to go ahead
02:23and set that really huge padding.
02:24So I am going to do be padding bottom, and trust me, this will not look natural at first.
02:28I am going to 1000 ems. That's a lot.
02:33Now underneath that I'm going to do a margin-bottom, and I am going to do -1000 ems.
02:39So again, we are pushing the bottom of both of those columns down by 1000 ems by
02:45using the padding, but then we're giving a negative margin of the exact same
02:49value to the bottom of it.
02:50What that's going to do is the article that's wrapping around those two,
02:53it's going to basically window-shade, if you will, that article right back up.
02:58So in order to clip the content off at the bottom of the article, I am going to
03:02create another selector that's an article selector--
03:05now this is our wrapper--
03:06this is going around these two columns, and I am just going to go ahead and
03:09do overflow: hidden.
03:13If you want to really kind of see what's going on with this, you can
03:17apply a background color to it.
03:20In this case I am going to do just background rgb (219, 126, 64), because I want
03:27you to visualize the role that the article is having in this technique.
03:31So if I save this, go back out to my layout, and test it, there's the background
03:36color of the article now, showing it's containing all of this. And check out what it's doing.
03:40It's going all the way down to the longest one, because remember, I'm using
03:45the same amount of padding on both of them and the same negative margin on both of them.
03:49It's coming back up to the very bottom of the tallest one, and then the article
03:53is just clipping it off.
03:54And that works fantastic.
03:55Now just like any other technique, there are going to certain constraints that
03:59you have with page layout and design.
04:01So you're going to have certain times when this technique is not going to be
04:04effective, but nine times out of ten, it's an oldie but a goodie.
04:08It works, and it works really, really well.
04:10I am going to go back in and get rid of that background color,
04:14save the file, preview it again, and there is our two-column layout with
04:19equal-column heights.
04:20It doesn't matter which one is taller;
04:22they are always going to be exactly the same amount and they're always going to
04:25go all the way down to the bottom of the footer. Perfect!
04:28Now another technique that I want to show off is faux columns.
04:31Now faux columns has been around for quite some time too.
04:33When faux columns first came out, essentially we used background images to
04:38handle this technique.
04:39It's very similar in the fact that we are going to have a wrapper around our two sections.
04:43So if I scroll down in my code, once again, we have that article around the
04:47section and around the aside.
04:50So the structure is exactly the same.
04:52So that article that we have wrapping around the two of them, if we apply a
04:55background color to that article, or if we give it a background graphic, one
04:59of the things that we could do was create a background graphic that tiles vertically
05:04that's a solid color and is only as wide as one of the columns.
05:09And you could actually do two of them if you wanted to, and it would give the
05:12illusion that you had background colors for those columns, when in fact, the
05:17column height would stop at a certain point, but the background color for the
05:20wrapper would continue all the way to the bottom.
05:22Now if that's a little unclear, let me show you what I'm talking about.
05:25I am going to go ahead and preview this in my browser.
05:27You can see that we don't have any background colors on these guys at all right now.
05:31However, if I were to go in, and let's just say I go ahead and create an article
05:37and just put a background color on that article.
05:40Let's say I go rgb and then (125, 125, 125). That is a gray.
05:48Now one of the things we need to do, remember, we are floating both of these
05:50columns, so we need to go ahead and contain those floats.
05:54And luckily I thought ahead and I applied clear fix to our styles,
05:59so I can go right down here in my code and on that article
06:02I can class = clearfix.
06:07So if I save this now and preview this in my browser, I can see that gray background.
06:15Notice that it's going all the way down, because article is containing these
06:18floats and it's stretching all the way down.
06:20So in the past what we've done is instead of using a background color, we will
06:23use a background graphic, and that background graphic will tile vertically and be
06:27only as wide as we need to for the columns.
06:30You can do just one background graphic or you can do a background graphic that
06:34has two colors in it.
06:35So you can really be flexible with it.
06:37Now the downside of that of course is you're adding overhead.
06:40You're increasing a resource that needs be downloaded when the styles are loaded.
06:44What's nice about evolving CSS techniques is that now instead of using
06:48a background image,
06:50we can actually use a gradient to do the same thing for us.
06:53Now most of time people think of gradients as going from one color to another sort of
06:57gradually and sort of fading in.
06:59But if you want, gradient color stops can be very abrupt.
07:02It can go directly from one color to another one without fading between the two of them.
07:07So that's how we can great some of these fake or faux columns, if you will, in our file.
07:12So I am going to go back into our code, and I've got an asset that's going to
07:16help us out a little bit here.
07:17If you go into the 03_09 folder and open up assets, you'll see the
07:23background.txt file.
07:25Go ahead and just open that up. And what I've got is I've got some syntax in
07:28here that is a lot easier to copy and paste than it is to type.
07:33So I am going to highlight all of that text, just copy it, and then going into
07:39my faux columns, I am just going to paste it below my existing background.
07:44Just to give you a heads-up in terms of what's happening here, we are applying a
07:47linear gradient that's going from one color,
07:51it goes to 0, and then it goes to 62, changes to white, stays at 62, stays
07:59at white, goes to 65.
08:00So at 62% and 65% we have white, and then it switches to another color.
08:06So we've got one color on the left-hand side,
08:08we've got a gap of white in the middle, and then it switches to another solid
08:11color on the other side.
08:12Now where am I getting these percentage breaks for the gradient?
08:16Well, it has everything to do with the width of these guys right here.
08:21So I'm calculating those based on the width of those.
08:23And this is a flexible layout,
08:25so here, we are calculating percentages rather than pixels.
08:28So if I save this, go back in, and preview it now, there are our two columns
08:35looking exactly the way it did before.
08:37They look like they're equal heights, but we know they're not.
08:39This is actually just the background of our article wrapper that's stretching
08:44down all the way to the footer, because the float is being contained, and this
08:48is why it's called faux columns.
08:50Now this gradient syntax can be a little cumbersome, and I will be honest with you,
08:55this is a lightweight version of the syntax.
08:57This is not fully cross-browser compliant.
08:59I don't have anything in there for Internet Explorer.
09:02I don't have anything in there for Opera.
09:04So there's a lot more that you can do with the syntax.
09:07If you're curious about CSS gradients and you want to learn a little bit more
09:10about them and generate the syntax for them, I really recommend this site, The
09:14Ultimate CSS Gradient Generator, which I've actually recommended before. You can
09:19find this at colorzilla.com/gradient- editor, and if you're familiar with
09:24creating ingredients in, like, Photoshop, or Illustrator, it offers you a nice
09:29visual editor to create your gradient.
09:31You can even come in and double-click the colors and choose whatever you want.
09:35And I am going to make a really horrible gradient,
09:38so bear with me on that.
09:41Then if you're happy with the gradient--who wouldn't be happy with that?--
09:44you can someone just copy and paste the CSS that it generates for you.
09:48You can even go ahead and throw in support for Internet Explorer 9.
09:51So this is a really neat online tool that you can use to generate some pretty
09:55complex syntax, because the syntax for color gradients continues to evolve all the time.
10:00It's a handy tool to know about.
10:02Now, I've just shown you two techniques here, and those are certainly not the
10:06only two ways to create columns of equal height.
10:10Designers are extremely creative people by their nature, and they continue to
10:13find new and very creative way as to tackle all these problems that are inherent to CSS.
10:18If you go out and research it, you're bound to find a bunch of articles
10:21discussing this very problem.
10:23Now I want to encourage you to take techniques like the ones I've shown you
10:27here as starting points.
10:28Go ahead and experiment with them, try out variations on them
10:31that might be better suited for your personal projects, and as you gain
10:34experience working with them, try approaching those problems in new ways, and go
10:39ahead and develop your own solutions.
Collapse this transcript
Floats: Lab
00:00Welcome to your first lab.
00:02Now in our labs you get to put what you've learned up till this point to the test.
00:06They are little different than regular exercise files, because instead of me
00:09giving you step-by-step instructions, I am going to give you the overall
00:13goals for the project, and then it's up to you to decide how to solve the
00:17problems that I give you.
00:18After you're done working on your lab, be sure to check the solutions movie that
00:22follows this one to compare your solutions to mine.
00:25So for our lab on floating, go into the 03_10 directory and open up the
00:31index.htm and the main.css file.
00:34Now I want to preview this page in a browser and I am going to compare it to the
00:39finished version so you can see kind of what our goals are.
00:41So here's the page in its current state.
00:44If I scroll down, after the banner some things start to appear maybe not the way we want.
00:49We are still in a single-column layout.
00:51We've got these two boxes that are stacked vertically. Our Archived Galleries
00:55list is way too wide.
00:57So obviously here our layout is in a single column instead of the two-column
01:01layout that we are shooting for.
01:02If we look at the finished version of the file, if I scroll down, you can see that
01:06this content is now in a column on the left-hand side, our Archive Galleries are
01:11a column on the right-hand side.
01:12They look a lot narrower.
01:13They look a lot better like that.
01:14These two boxes are stacked horizontally now, and then there are our page footer.
01:19So that is what we are shooting for.
01:21Now I am going to go back into the code and talk about what explicitly we are
01:25going to need to do.
01:26Okay, so I am going to focus on the main.css file.
01:29At the very top of the CSS file, I have in a comment the goals of your lab, so
01:34as you're working through the lab, feel free to go back up to these and take a
01:37look at all of your goals in order.
01:40Now I am just going to go down through the code and sort of show you, in the
01:44code, where you are going to be writing certain selectors and which selectors you
01:47are going to need to modify to achieve what we want.
01:49All right, so I am going to go down to about right around line 160 or so.
01:53I have got a section on the styles right here
01:54that says, "Basic layout styles."
01:56Currently right now there are only two selectors in there,
01:59so we are going to have to add some stuff to this.
02:00Now the first thing that you need to do is you are going to need to write a
02:03selector for the body
02:04that's going to set the overall width of the page to 1280 pixels, and then you
02:09are going to need to use that selector to center the entire layout.
02:12Go back through. We didn't have a specific movie on centering, but in many of
02:16the movies in this chapter, I talked about using a technique with margins to
02:20center content on the page,
02:21so that's kind of what we are going for there.
02:23Now the next thing we will need to do is write a selector for the article tag, or
02:28the article element, that makes it the left column in our two-column layout.
02:32You need to give it 50 pixels' worth of space on the left-hand side, and it
02:37should take up a total of 900 pixels' worth of space.
02:40Now after you are done with the article, write a selector for the aside that makes
02:44it the right-hand column in our two-column layout.
02:47Now for it, it will need 50 pixels' worth of space on the right-hand side, and
02:51then I want its total width that when it's added to the left-hand column, leaves
02:55you with 50 pixels' worth of space between the columns.
02:58So you are going to have to do a little math here. Think about this.
03:01Your left-hand column is going to take up a total of 900 pixels' worth of space.
03:05Your page all total is 1280 pixels wide,
03:08so essentially, the right-hand column will need to have 50 pixels' worth of space
03:12on the right-hand side and then 50 pixels' worth of space between the left and
03:15the right-hand column.
03:16And you are going to need to decide exactly how wide that's going to need to be.
03:20The last thing here that I'll need you to do is the page footer.
03:23We want to ensure that the page footer always remains below our two columns, so
03:28you are going to need to modify that selector so that that occurs.
03:31Now I am going to scroll down to right around line 332 or so, there we go, and find the news selector.
03:41So this is those two boxes that are stacking currently right now, vertically on
03:45top of each other, but in the finished version, they are stacked horizontally.
03:48That's what we need to take care of.
03:49So we are going to modify the news selector so that the two news items are going
03:53to appear horizontally.
03:55We are going to use margins to separate the two so that there is 50 pixels' worth
03:58of space between them.
04:00Here's the tricky part though.
04:02You've got to make sure the spacing only appears between the two news items and
04:06doesn't create additional spacing on either side.
04:10Now what I mean by that is if you were to go ahead and apply just margin right
04:13to both of them, then you end up with instead of just 50 pixels between the two of them,
04:18you've got another 50 pixels on the other side.
04:20So you've got to figure out how to solve that.
04:22That could require an extra selector. Maybe you could do it with just a
04:26selector, but you might have to write an additional selector as well, and that's
04:29something that you're just going to have to really put on your thinking cap
04:31about and try to solve that particular problem.
04:34My advice is to take a moment before you start this, look through the HTML and
04:39the CSS--I know there's a lot of code here--and get familiar with the elements
04:43that you are going to be controlling.
04:44It's always more difficult to modify somebody else's style sheet rather than your own,
04:48so I realize I'm putting you in the middle of a difficult situation here.
04:52So think about how you can use floating to achieve the goals that I just talked
04:56about, and then carefully plan on how you are going to use padding and margins
05:00to achieve the desired spacing in your elements as well.
05:03So you should use padding in this instance. You should use margin; that's something you
05:06are really going to do need to think about.
05:07Now as I've stated throughout the chapter, there's going to be multiple ways to
05:12create the desired layout using floats,
05:14so don't get too caught up in trying to guess exactly how I did it.
05:18Simply decide for yourself what the most efficient way is to solve the problems
05:23that I have given you.
05:24Have fun!
Collapse this transcript
Floats: Solution
00:00Well, I hope you had a good time working on the lab and getting our two-column
00:03fixed layout to its finished state.
00:06In this movie, I am going to show you how I styled the elements that you were
00:09tasked to finish in the lab.
00:10Feel free to compare your solution to mine, and remember, especially with
00:14floats, there are multiple ways that you could have achieved the same layout.
00:18So I am just going to go over our main.css file, and I have got the
00:21index.html opened here as well.
00:23These are found in the finished files directory within the 03_10 folder.
00:28So I am just going to get right into each one of the steps of our lab.
00:31I am going to scroll down to about line 160 or so, where our basic layout styles are.
00:38So the first thing that we were asked to do was take the body and set its
00:41width to 1280 pixels,
00:43and then the next thing was to center that content as well.
00:47So to center the content, I gave it a margin of 0 for top and bottom and then
00:51auto for left and right.
00:52Remember, that splits the available space in the viewport equally to the left
00:56and right-hand sides, centering the content.
00:59Now the next thing was to create our two columns.
01:01So in this case, I wrote a selector for article,
01:04I floated article to the left, I gave it a width of 850 pixels, and gave it a
01:09padding left of 50 pixels.
01:12So the 850 pixels plus the additional 50 pixels gives us the 900 total pixels
01:17that we need for that column.
01:19I used padding instead of margin, and margins certainly would have worked and
01:24would've been preferable had I had a background color behind the article, but
01:28since I didn't, padding works just fine.
01:30And even though I'm not as worried about Internet Explorer 6 anymore, there's
01:35an older Internet Explorer 6 bug where if you apply the margin to the same
01:39direction that you have a float, it doubles that margin.
01:42There are ways around that, but one of the easiest ways around it is just not doing it,
01:46so I have sort of gotten into that habit over the years, and so I am using
01:50padding there instead of margins.
01:52Now if I look at the aside, it's very similar.
01:55I'm floating it to the right.
01:56Now remember, we could have floated it to the left and added a little bit of
01:59right margin to the article, but for me, it's much easier to go ahead and just
02:03float that to the right.
02:04So it's going to go all the way to the right edge of the containing
02:06element, which is the body.
02:08And I gave it up a padding-right of 50 pixels, so same thing here.
02:12I gave padding-left, here I used padding-right to hold the content of 50 pixels
02:16from the edge of the containing element.
02:18I had to calculate exactly how much space I had left and since I started with
02:22900 pixels, I can subtract 1280 by 900 and it
02:26let me know how much space I had remaining. And I wanted to have 50 pixels' worth
02:31of space on the right-hand side--that's my padding--and 50 pixels' worth of space
02:35between those two columns, which leaves me with 280 pixels of available space for
02:40my aside, and that becomes the assigned width for it.
02:43For the page footer, in order to make sure the page footer stays below both of
02:46those columns, I simply set the clear property to both. Remember, that's going to
02:50reestablish normal document flow and it guarantees that the page footer is going
02:53to be at the very bottom of the page.
02:56Now the last thing that we had to focus on were those two interior elements, the news sections.
03:02So I am going to scroll down and show you guys how I handled those.
03:06All right, so about line 351 in the finished file. So for news, essentially
03:11what I did is I floated both of them to the left, and then I gave them a
03:14margin-right of 50 pixels.
03:16So that's going to make both of them appear horizontally, side by side.
03:20It's also going to space them apart by 50 pixels.
03:22But there is a downside to doing it this way, and the downside is is that both
03:26of them now have a right margin of 50 pixels.
03:29So although it's going to space them 50 pixels apart, it's also going to push
03:33the right one maybe further away from the edge than I wanted it to be.
03:37So to deal with that, I created another selector here, and I used the sibling
03:41selector that says, hey, when a item with the class of news follows immediately
03:46after another item of the class of news, set its margin right to 0, and just sort
03:51of canceled that out.
03:52So I used a very specific selector to target that element.
03:55There are certainly other ways of doing it.
03:56There are probably more efficient ways of doing it, if I want to be honest with
04:00you guys, but that is certainly one way to achieve that.
04:03And I'm going to go ahead and preview the index file in a browser, so we can see
04:07our finished results.
04:08As I scroll down, here is our two-column layout.
04:11Here are the two interior elements with their two columns.
04:14Notice that we have 50 pixels' worth of space on either side of our article, and
04:18aside; we have 50 pixels' worth of space between these two guys; and then we have
04:22another 50 pixels' worth of space here for the gutter. So notice that right-hand
04:26margin is not negatively impacting that.
04:28And then our footer is forced down at the bottom of our columns, exactly the way
04:32that we wanted it to.
04:34You know you can see from doing this lab that it doesn't really take a lot of
04:37CSS to create floated layouts.
04:38I mean, I know there are other elements within the layout that we didn't
04:41style, and those certainly required a little bit of work, but we handled the
04:45main content regions:
04:47The article, the aside, the footer and then some of those interior elements as
04:51well, like these two news items.
04:53So hopefully, one of the things that you can see from this lab is not only how
04:57easy it is to use floats to create layouts, but how efficient it is to let
05:01normal float handle the bulk of the layout. The only thing we're really floating
05:05are these two main page elements.
05:07All of the other main structural elements are being handled through normal
05:10document flow. And that sort of describes the strategy that I use, which is to
05:14let normal flow handle the bulk of the layout, and then use floats to adjust and
05:19create columns when necessary.
Collapse this transcript
4. Positioning Elements
Relative positioning
00:00When people talk about controlling page layout with CSS, often the property
00:05that they are actually referencing is the position property.
00:08In fact, way back in the day when designers first started crafting layouts
00:13without tables, they even referred to the CSS involved as CSS-P, or CSS positioning.
00:21Now the positioning property is indeed an important one,
00:24and in this chapter, we are going to go over the various properties and values
00:28associated with positioning and how we can use those properties to control
00:32elements within our layout.
00:33Now we are going to start with what is perhaps the most commonly used
00:37positioning value, and that's relative positioning.
00:40To demonstrate this, I have the relative. htm file open from 04_01 folder. And in
00:47fact, if I scroll down you can see, we have a fairly simple page. I've got a
00:51little bit of text referencing the positioning property, and then I have these
00:54three elements right here that we are going to focus on.
00:57There are three div tags, and they have the class attributes of elements
01:01One, Two, and Three.
01:02And in fact, if I preview this in my browser, you can see there are elements
01:06One, Two, and Three, and they are just stacking one on top of another.
01:09Now that means that these elements are in normal document flow.
01:13I have been talking about normal flow for pretty much the whole title,
01:16but we are finally in a position where we can discuss the three different
01:21positioning schemes of CSS.
01:23And we are going to discuss them in the context of the positioning property.
01:27Now you can see the positioning property has five possible values, although we
01:31can--almost everybody has inherent.
01:33So let's focus on the four values here.
01:36Static and relative, both of those values are considered to be in normal flow.
01:41In fact, static is kind of the default positioning of all the elements on your
01:45page, if you want to look at it that way.
01:47Absolute and fixed, on the other hand, are in the second positioning
01:51scheme, which is absolute.
01:53Now we are going to talk about those in just a moment.
01:56The third positioning scheme is floats, and we covered floats in our last chapter.
02:01So, of the three position schemes we have, an element is considered to be either
02:05a normal flow, floated, or absolutely positioned.
02:09So it's considered to be one of those three things.
02:12Elements with positioning values of static or relative are considered to
02:15be within normal flow.
02:17And that not only controls the behavior of that element, but it also controls
02:21the behavior of the elements around it. And to demonstrate that, let's go ahead
02:24and experiment with relative positioning.
02:26I'm going to go back into my code, and I am going to scroll up until I find the
02:30selectors that are controlling elements 1, 2, and 3.
02:33We are going to start with element1, and I am just going to come up and I'm
02:37going to do position: relative.
02:40There we go.
02:41So if I save that, and refresh my browser, you can see, ah, nothing
02:47happened. Absolutely nothing.
02:50Well, that's not entirely true.
02:52Actually, element number one now has positioning, and if you put quotes around
02:57that when I said it--I did little air quotes, you can't see that obviously, but,
03:01element1 now air quotes has positioning.
03:04Now what that means is that element is considered to be positioned, which we will
03:08talk more about later on in the chapter as we look into absolute positioning,
03:12and also gives us the ability to offset this element now.
03:16So once you have an element as positioned, you can offset it from its normal
03:21position within the layout.
03:22So if I go back into my code, I am going to go right here into the element1
03:26selector and I am just going to do some offsets here.
03:28I will do top of 10 pixels and left of 10 pixels.
03:36So what this is going to do for us is it's going to go to the top edge and the
03:42left edge of our element.
03:43You could actually think of a point on the box here in the top left-hand corner,
03:47because that's the point it's going to move it from.
03:49An offset from the top pushes it down from the top. An offset from the left
03:54pushes it from the left-hand side, which actually moves it to the right.
03:57So if I save this, go back into my browser, and refresh, you can see what
04:02happens to our element.
04:03It moved over by 10 pixels and then down by 10 pixels, so if offset from this
04:09point over 10 and down 10.
04:12Now again, you can use all four sides.
04:14You can also use right and bottom.
04:16Positive values are going to move it away from that edge. Negative values are
04:20going to move it towards that edge. So, if instead of 10 pixels to the left, I
04:24did -10 pixels to the left--let me save that--
04:28you can see that it moves in the opposite direction.
04:30It moves it to the right.
04:31In fact, -10 pixels to the left is exactly the same as a positive 10 pixels
04:38to the right, so if I save that and preview it, see the element doesn't move at all.
04:43So these offsets allow us to sort of tweak the position of our element based on
04:48which sides we are going to move it from.
04:49You have to be really careful with this.
04:51I mean, what would happen if I told it to go 10 pixels to the right and then 10
04:55pixels to the left at the same time?
04:57Not much. And I'm also being very cruel to my browser. So don't do that.
05:02Usually you are just going to use one or two of the offsets, okay.
05:06You may have also noticed something pretty curious too.
05:09The other elements, elements 2 and 3, didn't move at all.
05:12To demonstrate that a little bit more, let's make these offsets a little bigger.
05:14So I am going to change the first offset to left, and I am going to give it a
05:18left offset of 200 pixels, so now I am really going to offset it.
05:22And then I am going to give it a top offset of 100 pixels. So that's going to
05:25move it 200 pixels to the right, 100 pixels down.
05:29If I save this and refresh it, you can see what it did.
05:32But more importantly, you can see what it didn't do.
05:36Elements 2 and 3 are not affected at all,
05:39and that's due to the fact that one is considered to still be part of normal
05:43flow, so even though it's offset from its normal position, elements 2 and 3
05:48think that 1 is still here.
05:50So they don't respond at all.
05:52That's not to say that these elements still aren't related to each other and
05:55they still won't respond to each other; they will.
05:57In fact, if we go back into our code, and we change something about element1,
06:02let's say we change element1's height to 200 pixels, so if I save this, and
06:09preview it, you can see what happens.
06:13Element1 gets twice as tall and then elements 2 and 3 move down as if
06:18element1 were still there.
06:21This is a very different behavior than what we saw in the last chapter with floats.
06:25Remember, if we floated something to the right, these elements would move up to
06:28occupy its space unless we cleared that float.
06:31So when people first see this, they are like, "Oh wait a minute. This is cool.
06:35I can move elements around and not have all that trouble that I had in the last
06:39chapter with floats with stuff moving all over the place."
06:42But there is a bigger problem.
06:44We have this empty space here where one would occupy, and in order to move
06:47something into space, I have to offset those values too.
06:50So if you try to do a layout based purely on relative positioning, you have a
06:56lot to keep track of.
06:57I have never seen it done.
06:58Somebody probably did it just as an exercise, but it would be so difficult to
07:03remember where everything was supposed to be normally, to understand how much
07:07you are supposed to offset it,
07:08it wouldn't be worth the trouble. And then if you change one element then the
07:13whole layout flexes and changes as well, because everybody is responding to
07:17everybody else, because they are assumed to still be in normal document flow.
07:21So obviously, relative positioning is not going to be used to create an entire
07:25layout, so what is it for?
07:27Well, overall relative positioning is used mainly to do two things.
07:32First, just provide slight tweaks for your layout, when you need to offset an
07:36element in a specific direction.
07:37So if you just need to move something up a little bit or over a little bit
07:41to make sure your layout is doing what you needed to do, relative positioning is great for that.
07:44And second, it provides a positioning value for container elements.
07:51So why in the world would you need to do that?
07:53Well, that has everything to do with absolute positioning, which is something we
07:57are going to explore next.
Collapse this transcript
Absolute positioning
00:00As we mentioned in our last movie, absolute positioning is one of the three
00:04positioning schemes in CSS.
00:07When you use it to position an element, that element is removed from normal flow
00:12and no longer affects the elements around it. Nor in turn is it affected by any
00:17changes to those elements.
00:18To illustrate how this works, let's experiment with absolute positioning using
00:23the absolute.htm file, which you can find in the 04_02 directory.
00:29Just a brief look at this page shows us that it really is sort of exactly the
00:33same page we were working on, but there is one minor change.
00:37Down here where we have our three div tags, we have a section now wrapping those
00:42div tags, and notice that the section has a class of container. And we will see
00:47the importance of that in just a moment.
00:49So I am going to scroll up to our styles. And before we do anything, let's just
00:54sort of preview the page, so that we can see what it looks like before we begin
01:02positioning our elements.
01:03So again, One, Two, and Three, just stacking right on top of each other the
01:06same way they did in the last exercise. Okay.
01:10If I go to element1, and this time I set position to absolute and save the
01:18file, if I go back in my browser and refresh it, you can see something odd just happens.
01:24Now, One is in the same exact spot, but Two looks like it's gone, and Three has moved up.
01:29Okay. So you probably figured out from going through the float lesson what's happened here.
01:35One has been removed from normal flow, so Two and Three, they don't think it's
01:39there anymore and they just move up to occupy that empty space.
01:43So what's really happening is we can't see element2 because it's exact same size as element1.
01:48In reality, it's just up underneath it;
01:50we just can't see it.
01:51So let's add some offsets and see what happens when we do that.
01:55So I'm going to go back into our code, go back to element1, and I am going to give it a
02:00top offset of 125 pixels and then a left offset of 125 pixels. All right!
02:09So I am going to go ahead and save that, go back into my browser, and refresh it.
02:13Now, I've got to ask you, is that where you were expecting to see the element offset?
02:20We offset it by a left value of 125 and we offset it by a top value of 125.
02:27Now, I assure you that the offsets work exactly the same with absolute
02:31positioning as they would with relative positioning.
02:33So it moved from the left edge,
02:34it moved to the right, if you will, by 125 pixels, and it moved down 125 pixels.
02:39What's really different here, however, is the origin point that is used to move the element.
02:46Now, if you were assuming that this was going to be the origin point from its
02:49normal document flow, that's not quite how absolute positioning works.
02:53The way absolute positioning works is it looks for the nearest containing
02:58element to have positioning, and it's just going to keep going up and up and up
03:03and up, until finally it just goes to the initial containing block, which in
03:06this case will be the HTML tag, and it says, okay, fine, I'll just do that.
03:10So in this case, really the viewport is the offset point for this
03:15particular element right now. We can change that.
03:17For example, remember, this white box here is the body tag.
03:21So if I go back in the code and I go all the way up to our selectors--let's go
03:26up to the body selector--I can give it a position attribute as well.
03:30So remember, in order for an element to be the offset point for an absolutely
03:34position element, it has to itself be positioned.
03:37So we're just going to give it relative positioning, and we know from our last
03:40exercise that that's not going to affect the body at all.
03:43Body still stays in normal document flow.
03:45We are not offsetting body, so it's not going to move anywhere.
03:48In fact, the only thing that we've just done to body is we've given it a position value,
03:53meaning it has position.
03:55So now, our element1, which is right here, is going to keep going up.
04:00It's going to say, hey, are you a positioned element? No, I am not.
04:04Then it's going to go up to its next containing element.
04:06It's going to say, hey, to the body, are you a positioned element?
04:09And this time body is going to say, well, yes, I am.
04:11So now it's going to use body as its starting point, and if we save this and test
04:16it, you can see that it does just that.
04:18It starts at this point, goes 125 pixels over and 125 pixels down.
04:24So that means that in order to really gain a fair amount of control over this
04:28element, we have to know which containing block we want to be positioned and how
04:33it's going to move in relation to that containing block.
04:35So if we go up, currently we are using body, but what we want to use is this container.
04:43So I am going to go to the container and I am going to choose position: relative.
04:48So if you've ever looked through anybody's code, and I know when I was first
04:52learning CSS--and I will be honest with you, I still do it now--
04:55I would go to sites that I really admired and I'd say, wow, that's a really neat
04:58layout, and I would take a look at their CSS, and I would just sort of go
05:01through it and see how they were doing different things and what kind of
05:04techniques they were using.
05:05And this is something I used to see a good bit.
05:07I would see position:
05:08relative, and then I wouldn't see any offsets, and I'd be like, what in the
05:10world are they doing with that?
05:12And it wasn't until I really learned about absolute positioning that the light
05:15bulb when off and I said, oh, oh, okay.
05:17So basically anything inside container that's going to be absolutely positioned
05:21is now going to move relative to this container.
05:24And that's almost like grouping elements and moving them around,
05:27so it's a really cool technique.
05:28So if I save this and go back up and refresh that, so that's now going to move
05:32over and down from the top-left edge of our containing element.
05:36So now that I know what point it's going to be moving relative to, I have a much
05:41finer degree of control over this element.
05:43So if I come back in, for example, and start changing some things--so like, let's
05:47say, for example, that I take my top value and I change that to 25 pixels, and I
05:52take the left value and I change that to, say, 150 pixels.
05:59So if I save this and preview it, you can see it sort of lines up, one
06:04right beside number two.
06:06In the last exercise we used relative positioning and we saw that how when
06:11you changed one element, because it was still in normal document flow, it
06:14affected the other elements.
06:16For absolutely positioned elements, we really don't have that.
06:19So, for example, if I come in and told element1 to now be, let's say, 300 pixels
06:27wide and 200 pixels tall, if I save this and preview it, you can see that
06:35although it changes this, it doesn't affect Two and Three in the slightest.
06:40So when you position something as an absolutely positioned element, it totally
06:44removes it from normal document flow.
06:45You can even think of it as sort of hovering or floating over the page, and the
06:49other elements on the page just go about their business as if that element
06:52never really existed.
06:54Now, of course this means that we have to be very careful with how we use
06:57absolute positioning, to avoid unwanted effects.
07:00For example, if I come back in and let's say that I take this left value and I
07:06only offset it by 25 pixels instead of 125.
07:10So if I save that and test it, you can see that it now covers those up.
07:16I mean, you can overlap them as well.
07:18If I up that value to, say, 75 pixels, you can see that now it's just sort
07:24of overlapping them.
07:25So if we are not careful about how we are positioning these elements within our
07:29containers, we have some unintended side effects of overlapping elements, and
07:33usually you see that more when an element changes unexpectedly.
07:37Like, for example, if element2 had a bunch of text in it and I positioned
07:40element1 over here and the initial text only came this far, there would be no overlap.
07:46But then if somebody came in and wrote a new blog post, for example, which had a
07:49really long string of text in it, we might get some unwanted overlapping, so we
07:53need to be very careful about how we structure our layouts when we use
07:56absolutely positioned elements.
07:58To be honest, I have seen some designers-- and I know, I told you I hadn't seen
08:02anybody create an entire layout with relative positioning--well, I have seen
08:05some designers create entire layouts using absolute positioning.
08:09While this does allow you to position the elements exactly where you want them,
08:13what you're left with when you do this as an entire layout, you're left with
08:17elements that don't relate or react to each other in any way.
08:21That means that unless the content stays exactly the same, you're probably going
08:25to end up with a really rigid layout that consists of all these overlapping
08:30elements that you really don't want overlapping.
08:32Now, it's better to think about absolute positioning and approach it as a way to
08:37reposition elements within a container when the conditions allow it.
08:42Keep in mind that the containing element needs to be positioned as well,
08:46usually with a relative value so that the containing element remains within the
08:50normal document flow.
08:51So next up, we are going to continue to explore absolute positioning as a
08:56scheme, by working with the fixed value.
Collapse this transcript
Fixed positioning
00:00Fixed positioning is part of the absolute positioning model, and it allows you to
00:05position element relative to the viewport.
00:08Unlike relative or absolute positioning, fixed positioning wasn't initially
00:12supported very well by browsers, which is why it's not quite as well known as
00:16absolute or relative positioning.
00:18However, the reason increase in support, especially among mobile devices, means
00:23that fixed positioning is beginning to see widespread use among designers. Now
00:28to demonstrate fixed positioning, I have the fixed.htm file open from the 04_03
00:35directory, and if I scroll down, I can see that we are pretty much using the
00:39same file that we've been using for the past couple of exercises.
00:42We have our three elements, elements 1, 2, and 3, and they are wrapped in our
00:47section, with the class attribute of container.
00:50Okay, so I am going to go up to element 1 here, and let's just go ahead and set
00:55position for that to fixed.
01:00Now since we understand the concept of offsets now, we will go ahead and apply
01:03some offsets to this. And I am going to do a top offset of 25 pixels and then a
01:08right offset of 25 pixels as well, and then I'm going to save the page now.
01:13Remember, those offsets mean that it's going to push it down from the top edge
01:16by 25 pixels and push it from its right edge over to the left by 25 pixels.
01:21But if I go out of my browser and refresh the page, you can see what happens to element 1.
01:27It's removed from normal document flow, so Two and Three move up, just because
01:32it's part of the absolute position model, but One positions itself right over
01:36here, relative to the viewport.
01:38That's pretty much the exact same thing we saw with absolute positioning, but
01:42there's a very big difference.
01:44Whereas, absolute positioning is always looking to the container elements to
01:47see who the nearest element, the nearest containing element has positioning is,
01:52fixed positioning does not care about that at all; in fact, if I go back into my
01:58code and I go to container, and I say, position relative, save that and refresh,
02:08it has no affect whatsoever.
02:11So elements that have fixed positioning don't care what type of positioning
02:15their containers have.
02:16They are always relative to the viewport.
02:18Now what's the viewport?
02:19Well, the viewport is the available viewing space within your browser.
02:23So if I take the browser and I resize that, you can see that it remains fixed to
02:28the viewport, 25 pixels over and 25 pixels down.
02:33Now this has a lot of implications for scrolling.
02:36Let me show you what I mean.
02:37I am going to go back into my page, and I am just going to modify it really quickly.
02:41I am going to take this paragraph, copy this, and I am just going to paste it a
02:45few times, just to make sure that we have enough text in here to force
02:49scrolling if you will.
02:51So if I save this now, go back up to my browser, and refresh it, the page is
02:54going to look a little weirder, but it's going to really reinforce its concept.
02:57You will notice that now if I scroll the page, that element remains fixed
03:02in position relative to the viewport, no matter what's happening to the page itself.
03:06Now on the one hand that's really cool, because I could put and ad over here, or
03:11a banner, or some navigational aids, and they're always going to stay in that
03:15position relative to the viewport and not really care what's going on the page.
03:19Of course, the down side of that is if the viewport size changes dramatically,
03:24you could end up with an element that's running right over the top of your
03:27content and getting in the way of something.
03:30So obviously, fixed positioning isn't going to be right for every single layout
03:34that you are working with.
03:35However, recently a lot of designers are using it to provide users with ever-
03:40present navigation, like I was just talking about, feedback opportunities or as a
03:44way to display some type of related content. Now if you do decide to use it, you
03:49need to plan for how those fixed elements might interact with other elements on
03:53the page, especially if the browsers resize, and then how content might be
03:58overlapping if scrolling or something like that occurs.
04:01Be aware also that fixed positioning isn't well supported in older browsers.
04:04Now by that I am talking about like Internet Explorer Version 7 and older,
04:08really old versions of Firefox, things like that.
04:11The support is even worse for older versions of mobile devices.
04:15So if you are going to use it, test it thoroughly based on your audience and
04:18then provide users with fallback options for when it's not supported.
Collapse this transcript
Controlling stacking order
00:00You may have noticed in several of our exercises so far that positioning
00:04elements can often result in them overlapping each other.
00:08To make sure that your layouts behave the way that you expect them to, you need
00:11to understand the rules that govern the stacking of these elements and how you
00:16can control stacking through CSS.
00:19So I have the stacking.htm file open, and there are a few changes. For the
00:24most part it's the same, but we have some text now that describes Z-index,
00:30which is the property that we are going to be controlling, and then we have
00:33our containing element and the three div tags inside of it, so that much is the same.
00:38But currently, now, all three of those elements have been positioned using
00:43absolute positioning. And if I preview the file in a browser, you can see these
00:48three elements are stacking one right on top of the other because their offsets
00:52aren't far enough to keep them from overlapping.
00:54So notice that by default right now we are not controlling the stacking in anyway.
00:59Notice that by default, they are overlapping one on top of the other, and what's
01:04controlling the order of the stacking right now is the source order of the code.
01:09So the last object encountered in the code, the last one rendered, is stacked on
01:14top of the other objects.
01:15You can think of them as sort of like the first one will be on the bottom, then
01:18the second, and then the third.
01:19In fact, if we change that source order, so if I go down here to our elements and
01:24if I take the first element here, and I am just going to cut that out and then
01:31move it down here below the third one,
01:33now it's not going to change their position in the layout at all, because
01:37they're being absolutely positioned.
01:38What it does change, however, is the order in which they're encountered by the
01:42browser in the code.
01:43So now, if I save that and go back into the browser and refresh, you can see that
01:50now One is actually on top of Two.
01:53It would be on top of Three actually as well, if they were overlapping, because
01:57now One is encountered last, so it is the topmost object.
02:01All right, I am going to undo that because I want us to work from the context of
02:07sort of the default one as we begin to control it.
02:09Now if we want to take control of the stacking process, the property that we use
02:14id does the Z-index properties.
02:16You can think of Z-index, like, you have an X axis and a Y axis that control
02:21horizontal and vertical positioning.
02:23You can think of the Z axis as controlling stacking, and Z-index allows us to
02:29tell an element where it fits along that Z axis, if you will.
02:33All right, so I am going to go to element 1, and I am just going to do Z-index.
02:39That's the name of the property, z- index, z-index, and I am going to go ahead
02:43and give it an order of one, or a value of 1, I should say.
02:47For element 2, I am going to give a z-index of 3, and for element 3 I am going to
02:53give it a z-index of 2.
02:57Now I am doing these in one-value increments, one, two, and three.
03:00The actual value doesn't matter; what matters is which value is higher.
03:05The higher the value, the higher it is in the stacking order.
03:08So I could have put element two at 400 and it wouldn't have mattered;
03:12it would still be on top of those other two.
03:15So how far away you space them in values is totally up to you, and is usually
03:19dependent upon how many objects you have stacking and how much control you need.
03:23So if I save this, go back to the browser and refresh, boom, you can see that
03:28now element number Two is on top because it has the highest z-index value.
03:33If I go back into the code, let's see what happens if two elements have exactly
03:38the same z-index value.
03:40So, if I go to element1 and give it a z-index of 3 and leave element 2's
03:45z-index of 3, so if I save that and preview it, you can see that it doesn't
03:52appear like anything else has happened, but what's actually happening here is to
03:55resolve the conflict between One and Two who have the same z-index value, source
03:59orders is then used, so it falls back source order and since Two comes after One, Two
04:04wins the battle, even though the two of them have exactly the same z-index.
04:08You can use negative values. If I go into element1 and change its z-index from
04:143 to -1, if I save this and go back to my browser, let's take a look at what happens.
04:19So that actually moves it down below elements that are considered in
04:23normal document flow.
04:24I mean one of the ways that I like to describe this to people is think about a
04:27piece of paper, just being two- dimensional, flat. All of your elements in normal
04:31document flow are kind of painted on the flat canvas. And then if you position
04:36elements using positioning, it pretty much takes them off of that flat canvas.
04:40It sort of hovers them, if you will, over the page.
04:42Well, negative values send it below the page. But what you have to understand is
04:47it's going to send it below everything except for what it calls the initial
04:52stacking context, which is really fancy and will impress people at the cocktail
04:57parties if you use that term.
04:58So our HTML tag, or this gray background back here, is the initial stacking context.
05:04It's the first element, so it establishes the initial stacking order.
05:07So negative values will move it below everything except for that initial
05:11stacking context, which is why it's below the body, this white box here, but not
05:15below the HTML tag, which is the gray box.
05:17Stacking contexts are another thing that you really need to understand, and it's
05:23not the easiest thing in the world to understand.
05:25Every element, once that element is given a z-index rating, it establishes a
05:30new stacking context.
05:33So initially the only stacking context you have on the page is the initial root
05:37tag, which is the HTML element.
05:39But once you apply a z-index rating to an element, that element begins a
05:43new stacking context.
05:45Now, if it contains child elements, they are now all considered part of that
05:50one stacking context, which is different.
05:53It's easier to see than it is to sort of understand it the first time you hear,
05:57so let me show you.
05:58So we know that our div tags are wrapped in this section, so that section, you
06:04can think of that as a containing element.
06:06So if go up to my container selector and I add a z-index value here of 1 and save that,
06:17if I go back in the browser and refresh, watch what happens to the number One.
06:21All of a sudden, even though it has a negative value, it comes up.
06:24Now it is still the bottom of our stacking order here, but the reason that it
06:29comes up is that now it belongs to the stacking context of the containing
06:34element, which is this sort of brown background here.
06:37So it no longer belongs to the stacking context of the HTML tag;
06:40it belongs to the stacking context of the container section, which means that
06:45now it's going to order itself based on that context.
06:49So now that we have seen this a little bit, let's go through this list that we
06:52have right here in the browser
06:53that talks about the rules behind stacking context and how stacking
06:57conflicts are resolved.
06:58So the first the first bullet point and the very first thing you see here is
07:01that the backgrounds and borders of the element forming the stacking context is painted first.
07:07In this case, you can think of it as the gray background to the browser window.
07:10And then next, any child-stacked elements with a negatives z-index. Now that would
07:14have been our element number 1, but it's in a new stacking context now.
07:18Next after that, elements in normal document flow.
07:21That will be the body, all the text that you see on the page, that sort of thing.
07:25Then any non-positioned floats, so floated elements do hover over elements in
07:30normal flow, but they're not quite as high as elements that are positioned with z-index.
07:35After that, it would paint any child- stacked elements with z- index of zero or
07:39auto. So if you position something, if you say, position absolute, for example,
07:44or position fixed, and you don't give it a z-index rating, the z-index is
07:48considered to be zero or auto. And then right after that, any child-stacked
07:53elements with a positive z- index value, lowest to highest.
07:57So that's how stacking order is determined as the page is being painted
08:01within the browsers.
08:02Now, in most cases you are not going to need to go into it that deep.
08:05Most of the time the default stacking orders are going to work just fine, but
08:09every now and then you are going to find yourself needing to tweak your layouts
08:12and use z-index property.
08:14Now it's especially true if you're working with heavily positioned layouts or
08:18for when you're building complex widgets or interface components, things like that.
08:22Now in those cases, understanding how stacking context and the z-index property,
08:27understanding how those work, can be extremely important.
Collapse this transcript
Clipping content
00:00One of the things that absolutely positioned elements allow us to do that we
00:04can't do with other element types is to use the clip property. Now the clip property
00:10allows us to essentially crop an element and its contents.
00:14Let's take a look at that by opening up clipping.htm, which you can find in the
00:1804_05 directory. And the page structure of this one has changed a little bit.
00:25Instead of our normal three div tags that we have been using, essentially we
00:29have a section with a class of container, and now inside of that we have a div
00:34that has a paragraph inside of it as well. So we want a little bit more content
00:37inside of this so that you can actually see the clipping taking place.
00:41So essentially we are just going to crop that content down a little bit.
00:43I am going to preview that in the browser for you, so you can see what's kind of
00:47going on here, and here is the content that we are going to be clipping.
00:50You can see it's being positioned using absolute positioning.
00:53So what values can we use for the clip property?
00:56Well, there are not too many.
00:58The clipping property has three values that we can use.
01:01We have Shape, Auto, and Inherit.
01:02Now Inherit and Auto are pretty much the same as doing nothing. Inherit is
01:06asking the parent what's your clipping property, whereas Auto is just nothing.
01:11Now Shape, on the other hand, that is something.
01:13Shape allows us to pass a shape and some offsets into it that describe the
01:18shape of the clipping.
01:19Now, currently the only shape that browsers will accept is a rectangle.
01:24You can see the syntax right here.
01:27So the only shape that it allow us to pass into it right now is rectangle.
01:30There's hope that in the future we will be able use different shapes, like
01:33ovals, or polygons, but for right now rectangle is it.
01:36Now that is followed by a series of offsets.
01:38Now these offsets basically start at the top, so the first offset is top, then
01:44we have right, bottom, and left.
01:50So if you're looking for a way to remember that, you can think of trouble,
01:54top, right, bottom, left. Or you can think of starting with top and going in a clockwise fashion.
01:58So what we want to do is we want to take this element and clip it using offset
02:02values such as these.
02:04So I am going to go back into my code and I want to go up and find element1, so
02:09this is who we are going to clip.
02:10Remember, this is only available for absolutely positioned elements.
02:14So I am going to go down on the next line here and I am going to type in clip,
02:19and then I am going to do the rectangular shape, and then inside the parentheses,
02:22I am going to give it an offset of 50 pixels for the top, comma, 200 pixels for the
02:29right-hand side, comma, 150 pixels for the bottom, and then after another comma,
02:36finally 50 pixels for the left-hand side.
02:39Then I am going to remember my semicolon.
02:41I will go ahead and save that.
02:43I am going to go back out to the browser and refresh the page, and now you can
02:48see our content is clipped.
02:49Given the values that I just gave you, 50, 200, 150 and 50, is that what you were expecting?
02:55I'm betting not. And most of the time people get really, really confused about
03:01clipping properties.
03:02Basically, let's just practice using these values right here.
03:05Most people would assume that a top offset of 10 pixels means that it would come
03:09down and start clipping here, and then a 10-pixel offset from the right,
03:12they assume it comes to the right- hand side, it comes 10 pixels in.
03:1525 pixels, 25 pixels up and then 10 pixels, 10 pixels over. But actually nothing
03:20could be further from the truth.
03:22Really what happens is top and bottom offsets start from the top. Right and left
03:29offsets start from the left-hand side. So a top offset of 10 would start here
03:35and go down 10, just like you would expect,
03:37but then a bottom offset of 25 would go down 25 pixels and start clipping here.
03:43So it creates a sort of inner frame from those edges, and although once you
03:49think about it, it makes sense,
03:51it's still kind of hard to calculate exactly what you're going to be clipping, to
03:55be quite honest with you.
03:56So in order to really control the clip property, you need to know what the width
04:01and height of the element that you are working with is; otherwise, you've got no
04:04real way of controlling what content gets clipped.
04:07So I am going to go back into my code, and I don't see any height or width on
04:12element1, but it's a div tag, so if I go up, I can see that in the div tag, I've
04:17got 400 pixels' worth of width.
04:20In most cases that's pretty much what your elements are going to be like, and
04:23the height is generally created by the content and the padding and that sort of
04:27thing. But in this case if you are going to do clipping, one thing is that it
04:31only happens on absolutely positioned elements.
04:32So for those, you generally are going to be setting element width and height
04:36probably more frequently than you would on elements that are on normal flow.
04:40But here we don't have a height, so I am going to go ahead and set one.
04:43So right after our width, I am going to go ahead and set a height value of 150 pixels.
04:49So let's think about this for a moment.
04:51We have 400 pixels' worth of width, 150 pixels' worth of height.
04:55We have 25 pixels' worth of padding all the way around, so the true height is 200
05:00pixels and the true width is 450 pixels.
05:04Then we have this 2-pixel border.
05:06So let's give ourselves a little exercise here.
05:09Let's say we want to take the clip property and just clip the 2-pixel border off.
05:14Why would you ever do that?
05:15Well, you could do something really cool.
05:16You could take the border off and then based on user interactivity or
05:20something like that,
05:21you could undo the clip property and make the border come back up.
05:23You could make elements sort of expand and contract.
05:26I mean there's all sorts of really kind of cool stuff you can do with clip if you're
05:29using it with absolutely positioned elements.
05:30So I am going to go down to element1 and I am going to change our clip here.
05:34I am going to change the top clip to 2 pixels.
05:37Remember, that's 2 pixels down from the top. And then I'm going to show you
05:40different way to write this syntax.
05:42Instead of using commas, I am just going to use white space between them, so no
05:45comma, but I do need a white space there.
05:47The next thing I am going to do is I am going to go ahead and give this
05:50offset at 452 pixels. Now, why 452?
05:55Remember, this is the right-hand offset.
05:57That means it's going go all the way over from the left edge to the right, 452 pixels.
06:03Think about the width of our element. 450 pixels of width, with 4 pixels of
06:09total border, so two on the left-hand side, 2 on the right-hand side, so this is
06:12going to clip off the two that's on the right-hand side.
06:14Now our bottom offset is going to be 200 pixels. Again, remember that the bottom
06:20offset starts at the top of the element and then comes down.
06:23I am going to get rid of the comma, make sure I have my white space, and then
06:27the very last one, which is the left- hand side, is also going to be just 2
06:33pixels, because it's going to start at left- hand side and go over 2 pixels and clip it.
06:37Again, I've got to remember the white space there.
06:39Okay, so what's the difference in the two syntaxes?
06:42The first syntax we used had commas; the second syntax does not.
06:45Well, the first syntax, the one with commas in it, that is the one that the W3C
06:50specification says is the proper syntax.
06:53The syntax you are seeing here is the one that older versions of Internet
06:56Explorer actually support.
06:58Now because all the modern browsers support this as well, just to be backwards
07:02compatible, because back in the day, Internet Explorer 8 and above now support
07:07the standard syntax, but older versions of IE, IE7 and 6, for example, in order
07:12to get it to work, designers would use this syntax. So all the other browsers,
07:16like Chrome and Firefox and browsers like that, said, okay, we will support that
07:20too, whatever. So if you want to support older versions of IE use this syntax.
07:24If you want to use it in the standards- compliant way, use the previous syntax.
07:28So if I save this, go back out to the browser, and refresh, you can see that it
07:33is doing exactly what we wanted it to do, it's clipping off just the border and nothing else.
07:38Remember, this clip property works for any element that is absolutely
07:42positioned, and that means that any element that's considered to be in the
07:46absolute positioning model.
07:48So if we go back to element1 and change its position from absolute to fixed,
07:53remember, fixed elements are also considered to be part of the absolute
07:57positioning positioning model. That sounds weird to say.
07:59But the absolute positioning model, if I save that, and preview it again, yes,
08:04it changes the position of that, but again, the clip property is still working.
08:08If I change the position relative, the clip property wouldn't work.
08:11Now I doubt that you are going to need to use clipping very often, but it is
08:14nice to know that you have yet another way to control absolutely positioned
08:18elements within your layouts.
Collapse this transcript
Controlling content overflow
00:00We briefly came in contact with the overflow property during the chapter on floats.
00:05In this movie I want to discuss the overflow property in a little bit more
00:08detail and how it can help us when dealing with positioned elements.
00:13To do that, I have the overflow.htm file opened up and if I look through the
00:17code of this file, after a section where we talk about the overflow property
00:22a little bit, we have this section with a class name of container and it has
00:26a lot of text in it.
00:28If I preview this in a browser, you can see that right now the text is just
00:33forming the height of the section itself, so we don't have a defined height on
00:37this section. So because of that, the height is actually being determined by the
00:40content, so there really isn't any overflow going on at all.
00:44Now this is typical for your elements. If you don't declare a specific height,
00:47for example, the contents of the element forms the height.
00:51Now I want to go right back up here for just a minute and talk about the
00:54different values of the overflow property and what it can do for us.
00:57First off, we have to understand what it means for an element to be in overflow.
01:02If you have an element that you have a defined width and height on it and the
01:06content no longer fits within that element,
01:09it is considered to be overflow. Our properties here, visible, hidden, scroll,
01:16auto, and inherit, all describe what should happen to the content of an element
01:21where we have overflow.
01:22Now visible basically says, okay, just display anyway. Even though it's going to
01:26overflow the content edge of the element, it doesn't matter; the content still
01:30needs to be visible.
01:31Hidden is going to basically clip the content so that any content that would
01:35normally overflow it is not visible.
01:38Scroll tells the browser to put scrollbars on the element, and that way if you
01:43do have overflow content, you can scroll through the content.
01:46It should be noted that scroll puts scrollbars on the content whether it needs
01:51it or not, which is where auto comes in.
01:53Auto is your way of basically saying, hey, give me scrollbars if I need them.
01:57If I don't, then please don't.
01:59Then inherit obviously says whatever my parent element is doing.
02:02So let's go back to our code and trigger overflow so that we can see these
02:05properties in action.
02:06So I am going to go back into my code, I am going to scroll up to my styles, and
02:11where it says container here, I am going to add a height to this. And I am just
02:15going to give it a height of 300 pixels.
02:17Now that is not nearly tall enough to display this content.
02:21So if I save this, go back into the browser, and refresh the page, now if I
02:27scroll down into the content, you can see, the container no longer is able to
02:31contain all the content--
02:32it's only 300 pixels tall--and the content is allowed to overflow outside the content width.
02:38Okay, so you can see by looking at this that visible is the default for
02:43elements, so anytime overflow is triggered, if that property is not set
02:47explicitly, visible is considered to be the value.
02:51So if we go back into our code and we go to the styles of container, if I
02:55set overflow to visible and save it and refresh the page, we don't see any difference.
03:05Now on the other hand, if I change that value to hidden, save it, and refresh
03:11the page, you can see what happens to our overflow content.
03:15It's trimmed off, and notice that the page is not even scrolling down that far anymore.
03:19That content is gone and will not affect content around it.
03:22Of course, the downside is you can't read that content anymore.
03:26Even if I go down and highlight and try to push it down, you can't see it.
03:30It's totally hidden. It's clipped off.
03:32Now if I go in and tell it to scroll, if I save that and preview, you can see that
03:40now over here on the right-hand side of this element, and this element only,
03:45I get scrollbars that allow me to scroll through the content.
03:48This is one of the coolest things that we can do for positioned elements.
03:51Let's say, for example, that you have some type of a notice that you're going
03:55to need to position using absolute positioning and floating over other elements, or to the side.
04:00You may not be able to have a flexible height on that element. Perhaps you
04:04need it to be a specific size. But you're not 100% sure how much content is
04:08going to go inside of it.
04:09Well, now the overflow property allows me to go ahead and put scrollbars on the
04:13element if I need it, and that way the users can still read through all the
04:16content, even though it's restricted in size.
04:19Now of course, there is a big difference between scroll and auto, and if I go
04:23back into the CSS, if I choose auto, notice that if I refreshed my page right now,
04:30we don't really see any change.
04:32We see a slight change, but really, scrollbars are still there.
04:35However, if I go and get rid of the height property, save that, and test it,
04:41notice that the scrollbars go away.
04:43They are no longer necessary, because the content is no longer overflowing.
04:47Of course, if you had scroll instead of auto, notice that you get the
04:54scrollbars, whether you need them or not.
04:56So that sort of negatively impacts my element.
04:59So if you're not 100% certain whether you're going to need the scrollbars or
05:04not, then just go ahead and use auto.
05:06Just be aware of the fact that adding the scrollbars is going to impact the
05:10width of the element slightly, so if you are sort of planning for whether
05:13the scrollbars are going to be there or not, you need to account for them
05:16within your layout.
05:18So I think you can see pretty quickly here how handy the overflow property is
05:22when creating layouts, especially for positioned elements.
05:25In some layouts, position elements will be restricted to specific widths and
05:29heights, and by using the overflow property, you can control exactly how the
05:34browser displays any content that doesn't fit within those regions.
Collapse this transcript
Positioning elements: Lab
00:00Controlling positioning is an incredibly important part of CSS layouts, but
00:05perhaps not in the way that all new web designers think. Rather than
00:09controlling the bulk of the page layout, positioning is typically used to
00:13control specific elements within it or to build complex interface components
00:17that other positioning methods really aren't well suited for.
00:20With that in mind, in this lab we are going to be focusing on tweaking a couple
00:24of areas on our desolved.org layout.
00:27Go ahead and go into the 04_07 directory, open up the index.htm file from that,
00:34and go inside the _css folder and open up main.css.
00:37Those are the files that we are going to be working on.
00:40So I only have two things in this lab that I need you to do.
00:43But before you go, oh wow, only two things,
00:45this is going to be a really quick lab,
00:47there are two things that are going to take a little bit of time on your part to
00:50think through and decide exactly how you want to solve the problems.
00:53So it may take a while, even though there are only two tasks.
00:56So before we get into the specifics of them, I am going to switch over to my
01:00browser, and I'm going to look at the page as it is now and then compare that to
01:04the finished version.
01:06So currently right now, if I scroll down a little bit, our banner does not look
01:10the way that it should.
01:11This paragraph which should be over here on the right-hand side is down below
01:16the banner, overlapping it in a weird way.
01:18So this is what it should look like.
01:20If I go over to desolve.org, in this layout you can see the paragraph is over on this hand side.
01:25It's overlaying the image, the height matches, and it's in a place where it
01:30lines up with the column below it.
01:31So that's one thing that you're going to need to take care of.
01:34The second thing, some of you have probably already noticed it.
01:37Look at this little bird over here, isn't that cool?
01:40As I scroll through the page, the bird stays right there, and of course, that
01:44is a link which allows people to follow me on Twitter. So if you want to follow
01:48me on Twitter at jameswillweb, you can simply click that link and follow me on Twitter.
01:52It's fun stuff. But it's right down there in position, and you will notice that always stays in
01:56exactly the same place.
01:58In the current version of the layout, if I go all the way down to the footer,
02:01the bird is right there, so we need to move that as well.
02:05So going into the specifics of what we need to do,
02:07let's go into the main.css and at the very top of the style sheet, I have got
02:12the Lab steps here for you.
02:13So the first step, in the banner styles section, which is around line 276,
02:18you need to modify the styles for the banner and banner paragraph so that its height
02:23now that will be the height of the paragraph, matches the banner, and its width
02:27matches that of the aside column, so you are going to need to look at those styles
02:30and figure out exactly how wide that is.
02:32Now remember to take their current padding into account when you are trying
02:36to match up widths.
02:37Then use positioning to line up the paragraph with the right edge of the banner
02:42so that it appears to line up with the aside column below it.
02:46Finally, ensure that the content is not allowed to flow outside of the banner
02:49div tags. So there is a couple of things we need to do here.
02:52It might be second nature to you after the floats chapter to look at the
02:56finished version of this and say to yourself, well, I'll just float this over here.
03:01But in this case, instead of floating, positioning is a little bit better suited
03:04for this task, and I want you to tackle it using positioning.
03:07Now the second task for you is to go down to the footer, locating the Twitter link.
03:11Now using the class attribute, write a selector at around line 691. So you are
03:16going to have to go into the HTML code, locate the Twitter link, and look at
03:20the class attribute.
03:21So using that, you are going to write a selector that fixes the position of the
03:24Twitter icon to always be 20% away from the bottom edge of the screen, and snug
03:30up against the left edge of the screen.
03:32Now, okay, so yeah, that's only two things, but trust me, you've got a lot to think about.
03:38Now determining how to size and positioning the banner paragraph may take a
03:42little while, and if you are having trouble with it, don't get frustrated.
03:45Just keep trying and feel free to sneak a peak at the finished files if you
03:49need a little help.
03:50And once you are finished, be sure to check out the solutions movie so that
03:54you can compare your code to mine and hear me discuss the solution in a little
03:58bit more detail.
Collapse this transcript
Positioning elements: Solution
00:00Hopefully, you were able to complete the positioning elements lab without too
00:04much trouble, and without getting too mad at me.
00:06Now, if you did get stuck, don't feel bad.
00:09It takes a while to fully understand how positioning works.
00:12So let's take a look at the finished files and how I coded those elements.
00:16So again, I have the index.htm and the main.css files open.
00:20Now, this time I went into the 04_07 folder and into the finished_file folder,
00:24so I'm opening from there.
00:27For the banner, I'm going to scroll down to about line 270 or so, and we go there.
00:34There is my banner style. And again, I'm previewing this in the browser, so we
00:38can sort of visualize kind of the solution that's happening here.
00:41Okay, now the banner paragraph is the one that I'm having to position.
00:45Now, I had to go ahead and take in the account of the padding of this when I
00:48set the width and the height values.
00:49So, taking the padding into account, I've set a height value that's equal to the
00:55height of the banner above it, and then I set the width value that again, along
00:59with the padding, gave it the same width as the aside below it.
01:02Now, in terms of positioning it, I'm using absolute positioning and I'm giving
01:07it a top and a right offset of 0 and 0.
01:10So, rather than trying to figure out exactly where the left or top-left offset
01:15would be, I just went ahead and gave it a top-right offset and what that's going
01:19to do is it's going to align the top right-hand edge of this paragraph up with
01:24the top right-hand edge of the banner, and then if the width is correct, then
01:29the position is going to be correct.
01:30Now, the next thing I needed to remember to do was to go up to banner and set
01:35its position to relative, and that is a step that a lot of people may have
01:38skipped or may have forgotten about.
01:41Remember, if you don't set the containing element's position to relative, what
01:45an absolutely positional element does is it positions itself relative to the
01:49nearest positioned ancestor. And in case of this file, it would probably be the HTML element.
01:54I also went ahead and set the overflow on banner to hidden so that if there was
01:59any overflow content it'd go ahead and trim that off.
02:02It is worth pointing out and noting that since banner p, the paragraph itself, is
02:06absolutely positioned it's still kind of above and outside of the banner. It's just
02:11using it as a point of origin.
02:12So, if I expanded the size of the banner paragraph or made it bigger, that
02:17overflow property is not going to affect it at all.
02:19So, it's more to make sure that other content within the banner doesn't flow
02:22outside of it, not so much the banner paragraph.
02:25Now, I'm going to scroll all the way down towards the bottom of my styles, to
02:28about line 691 or so, and I'm going to take a look at the twitter selector that I wrote.
02:35So, this one was actually pretty simple once you got down to it.
02:38We're just going to set position to fixed.
02:40We know that that sets its position relative to the viewport and not any
02:44other containing elements. And then I set the top value to 80% and the left value to 0.
02:50Now, I'm sure that some of you went ahead and set the bottom to 20%.
02:54That's very similar to setting the top to 80%, but essentially, it's not exactly the same.
02:59That's based of where the offset is.
03:01Remember, the bottom offset would set the bottom of the element to 20%, whereas
03:07the top 80% is lining the top of it with 20% as well.
03:11So, there're a couple of ways that you could've approached it and gotten the same results.
03:15In the end, as long as you're happy with it, it doesn't really matter if your
03:18code matches mine or not.
03:19So, again, if I look at this in the browser, there's the twitter icon,
03:23scrolling down the page.
03:25Because it's positioned- fixed, it remains in place.
03:28So again, remember, there's always more than one way to accomplish something in CSS.
03:32So if your styles didn't exactly match mine, it's not a problem, as long as they
03:36still work and you got the desired results.
03:38However, if our styles are different, take a moment, examine the rules, and
03:42think about which method is more efficient, easier to update, and maybe easier to
03:47maintain over the long run. As always, those should be very important goals for
03:51your sites.
Collapse this transcript
5. Building Fixed Layouts
Design considerations for fixed layouts
00:00In this chapter, we're going to focus on building fixed layouts, and we've worked
00:04with fixed layouts already, and for the most part we've discussed all the
00:08concepts that you need to know in order to create them.
00:10So our focus here will be on defining exactly what a fixed layout is, and on
00:15the issues that you're going to need to consider when you're thinking about using them.
00:19So fixed layouts are layouts that are set to an overall fixed size that doesn't
00:25react to changes like browsers being resized or device-orientation changes.
00:30In fact, in their pure form, fixed layouts really don't care about things
00:34like the width of the browser window, what device they are on, or how the
00:38site is being viewed.
00:40Most fixed-width layouts start with an overall width value that's applied to the
00:44body tag or to a wrapper element that surrounds the site's content.
00:49The side is then centered or aligned based on personal preference.
00:53Interior spacing is then defined for container elements at values that divide up
00:58the available space.
00:59Element spacing is typically controlled through the use of margins or padding,
01:03and the resulting values will typically add up to the overall defined width.
01:08Most fixed-width sites are designed with the desktop in mind. As such, the
01:13average target size has increased over the years.
01:16Now, originally designers targeted 640x480 monitors, which increased to
01:211024x768, and has currently grown to occasionally targeting 1280x960.
01:28Regardless of which size you're targeting, most designers set the width to a
01:33value slightly smaller than the target size to allow for things like browser
01:37chrome or maybe floating browser windows that aren't maximized.
01:41Many layouts currently target around 960 pixels, which would still be visible on
01:46the 1024x768 monitor.
01:49Now, like any technique, there are pros and cons to using fixed layouts.
01:53Fixed layouts tend to be easier to create and require less code than
01:57other layout types.
01:59You're only targeting one size, so you have less to consider when designing
02:02and writing the layout.
02:04You also have a greater amount of control over the visual aspect of your design.
02:08Since elements are fixed in place, it's a little easier to create pixel-precise
02:13layouts that look exactly the way that you want them to.
02:16Because they don't change, they also provide a consistent look and feel for the user.
02:20There are of course some downsides to using fixed layouts.
02:24Their very nature means that they don't adapt to other devices or smaller
02:29browser windows, meaning that users sometimes have a less-than-optimal experience,
02:34due to increased scrolling or content being cut off.
02:38On larger monitors, smaller fixed layouts can look lost, with large amounts of
02:43empty space surrounding the layout.
02:45They are also tied to the very rigid nature.
02:48An element-spacing problem that might even go undetected on a flexible layout
02:53could potentially break a fixed layout, meaning that small changes to layouts
02:57have to be very thoroughly tested.
02:59Now, as a designer, you should keep all these points in mind when deciding on
03:03whether to use a fixed layout or not.
03:05The recent explosion of mobile devices means that fixed layouts to some degree
03:09ignore a significant part of a site's potential user base.
03:13Later on, we're going to explore creating flexible layouts that change when
03:17resized and responsive layouts that change based on factors such as screen size,
03:21orientation, and resolution.
03:23For now, let's take a closer look at building fixed layouts.
Collapse this transcript
Establishing the layout grid
00:00Because the planning stage of the design process is so critical to a site's
00:04success, I want to take a few movies to walk through my typical workflow.
00:08I want to start by illustrating the use of grids for planning layouts.
00:12Although I use a grid as the basis of all my layouts,
00:15fixed layouts lend themselves particularly well to grid-based designs.
00:20If you're already a graphic designer, I'm betting that you're pretty familiar
00:23designing around a grid; and if you are not, I think you'll soon see the
00:27advantages that grids give you when creating layouts.
00:30So I am going to be working with this file fixed_planning.ai, and here I am
00:35again, of course, in Illustrator.
00:37Now, I have saved the finished version of this file in the Assets folder,
00:42which is a located directly in the exercise files folder, and if you want, you can open it up.
00:46And of course you are going to open up the finished version of what we are
00:48going to be doing here.
00:49Actually, what I would most encourage you to do is to just grab a scrap piece of
00:53paper, a scrapbook, some graph paper, or something like that, because if I was
00:58doing this on my own and not filming it for this particular lesson, that's
01:02exactly what I would be doing.
01:03I'd have my sketchbook opened up.
01:04I would have a piece of graph paper or something like that, and that's what I
01:07would be using to do this planning session that we are about to go over.
01:11However, because I am filming it here as part of the title, I can't really
01:15show my sketchbook.
01:16It wouldn't film very well, and I'll be quite honest with you, nobody is going to
01:20be able to read my writing,
01:22so I am just going to use Illustrator.
01:24You do not have to do it with me, but if you're very comfortable in Illustrator
01:27or another layout program, feel free to use that instead.
01:30But I am a big fan of sketching these things out beforehand, actually.
01:34We are going to go through all these different steps that I use to plan layouts,
01:38and I just want to mention that even though we are planning a fixed layout, most
01:42of these steps are the same for whatever type of layout I am doing.
01:45Some of them are specific to fixed layouts, because that's our focus this
01:48chapter, but really this workflow process is what I used for almost all of my layouts.
01:53The first thing that you do with a fixed layout is you define the target resolution.
01:58That's job number one.
02:00Now, monitor sizes are trending upward a little bit, but we need to
02:04target something that's pretty much the most likely resolution for your target audience.
02:09For the most part, the average monitor size out there is 1024.
02:12Now 1280 is certainly a resolution that's gained in popularity, and there are a
02:17lot of people out there using them, but so many people are still using 1024x768,
02:21that's the resolution size or the target resolution that I'm going to choose.
02:26You don't want to make your layout exactly 1024, because you got the browser
02:30chrome, such as the scrollbars or toolbars. What if somebody is browsing with a
02:34window that's only half open?
02:36So my target resolution for this particular layout is going to be 960 pixels.
02:41That's what I am going to target.
02:41It's a little bit smaller than 1024, but it's not altogether going to look lost
02:46in a 1280-layout either.
02:49Now you may or may not have seen that 960 resolution.
02:53It's very, very popular with web sites, especially fixed sites, and there is a reason for that.
02:58There is a method behind why some people choose this size.
03:02960 is divisible by a lot of numbers. Just to give you an idea,
03:071, 2, 3, 4, 5, 6, 8, 10, 12 15, 16, 20, 24, 30, 32. You get the idea.
03:15It just keeps going.
03:16There are a lot of numbers that will divide into 960, and what that allows you
03:20to do is when you're creating a grid like we are going to be creating in just a
03:23moment, it allows you to create a grid that gives you a lot of flexible column creation.
03:28You can have 12 columns, 16 columns, 30 columns, 6 columns, however you want to
03:33do it, and still divide it by 960,
03:36so that is another reason why that is such a popular target resolution for layouts.
03:41So what I am going to do is I am just going to go over here and grab a
03:43rectangle and I am just going to draw a box that is 960 pixels by 1200 pixels.
03:51I am just going to click OK.
03:53There's my box. And I am just going to move this so that it's on the artboard. All right!
04:00So that's sort of the predefined space, if you will, and I will line it up in
04:03just a moment when I get my grid, but that's more or less sort of the predefined
04:06space that I am going to be designing inside of as I begin to do my layout.
04:11The next thing that I want to do is I want to define the base unit for my grid.
04:15As I mentioned before, all the layouts I do I design on a grid, and that really
04:20helps create very coherent compositions, so that everything sort of relate to
04:24each other and everything has a place to line up that is based on a similar
04:28unit of measurement.
04:29That base unit that you are going to use for your grid is going to very widely
04:33based on what you are trying to do.
04:35One of the things I would recommend doing is doing a number that your target
04:38resolution is divisible by.
04:40So that's one of the things that I would recommend doing.
04:42The other thing is it should have some meaning for content within the page.
04:47Let me give you an example.
04:48If I was doing a photo gallery, for example, the thumbnail size might be a
04:53really good place to start when I am talking about defining a base unit for my
04:57grid, or something that's going to be divisible into my thumbnail sizes.
05:01An image, it could be really anything you want.
05:03Now the page that we are going to be sort of crafting over the course of this
05:07chapter is very text heavy,
05:09so basing a layout off of your default text unit is not a bad idea.
05:14So what I am going to do is, my base unit for my grid in this case is going to be 16 pixels.
05:21That's the default font size for almost every browser out of the box.
05:25Now users can certainly change it, but you are never going to get to a situation
05:30where you craft a layout for people online that they can't change in some
05:34way through their browser, whether zooming up on it or changing the text size or
05:38telling images not to display.
05:39They have got all sorts of control over your layout.
05:42So what I am doing is I am basically establishing a baseline for my layout. The
05:46base unit for it is going to be 16 pixels.
05:48I will say I'm a glutton for punishment here because most examples I see of the
05:52people that are doing tutorials like this,
05:54they will define a grid that's based off of ten pixels because the math is
05:57whole lot easier to do.
05:59But I want this to be a very real-world example, and I frequently create grids
06:03that are based off of 16 pixels, especially for layouts that are going to be
06:07very text heavy, because that's essentially the size of 1em.
06:10So later on, when I am writing my styles and I make something 1em, I know it
06:14corresponds to the layout grid that I am using.
06:16So there is a rationale behind picking a weird number like 16.
06:19So I am going to go up and look at my Preferences here.
06:23I want to show you something about Illustrator.
06:25One of the nice things you can do is go ahead and define a grid, and I am going
06:28to put a gridline every 80 pixels, and I am going to subdivide that by 5.
06:32So of course, 16 goes in 80 5 times, is going to give me a gridline every 16
06:36pixels, but more importantly, by putting a grid line at every 80 pixels, that's
06:40going to give me a total of 12 columns within my 960 pixels.
06:45So essentially, I am going to have 12 columns that I can use to arrange all the
06:49elements on my page in my layout.
06:52Now, why 12 columns?
06:53It's kind of just an arbitrary number that I picked.
06:55I could do 16. I could do anything that's divisible by 960, but 12 gives me a
07:00nice sort of even number to work from.
07:02So I am going to click OK.
07:03I am going to turn on the visibility of my grid, and there is my grid.
07:06Now I am going to take this box that I created. I am going to sort of reposition
07:10that, so that it's lined up to the grid here, okay.
07:14So I am just going to save my file.
07:15Now, if you were sketching this out, obviously graph paper would come in really
07:18handy here, but you don't need to have that defined grid.
07:21You don't have to draw that grid on your page.
07:23You know, I typically will just do a box, draw a little box on a piece of paper,
07:27and then just write little notes to myself inside, 16 pixels, and then I'll make
07:31sure that everything that I am doing is divisible by those numbers so that I
07:35come out with a layout that's still based off of this grid.
07:38So now we know which resolution we are targeting, we know what our base unit of
07:42our grid is going to be, and we know how many columns we are going to be using
07:45within our grid, and that's 12 columns.
07:47So next up, we are going to need to finalize our layout planning by defining
07:51column spacing and then calculating our element dimensions, and we are going to
07:55do that in our next movie.
Collapse this transcript
Defining column spacing
00:00In the previous movie, we began the process of planning a sample
00:03two-column fixed layout.
00:05We established the target resolution, we decided which unit we were going
00:10to base the grid off of, and we defined our grid.
00:13So in this movie, we are going to complete that process by using the layout grid
00:17to determine how to space our columns and then calculate the element dimensions
00:22we will need for our CSS.
00:24So I am just going to keep working in the fixed_planning.ai.
00:27In the previous movie, if you opened up a layout program and worked with
00:31that, then you can just continue doing that. And if you grabbed a piece of
00:34sketch paper and you are sketching it out on that, then that's awesome. Just keep doing that.
00:38The first thing I want to focus on is the main column.
00:41Now I have already mentioned how this layout is going to be text heavy, so that
00:44means that wherever the body copy or the main text is, that's the most
00:48important content region on the page, and that is where I typically like to
00:51start with my layout.
00:53I am going to bring up something that as an art major
00:56I tried to avoid for years and years and years,
00:58so just give me a moment. Yes, there it is; the calculator.
01:02As an art major, I try to stay as far away from math as possible, but it finally
01:06caught up to me again.
01:08So in terms of spacing columns, we really have to think about readability.
01:13One of the things that you learn if you study typography is that the ideal
01:17column width for readability is about 66 characters wide.
01:22So if you can make a column that's around 66 characters, that's really the
01:26sweet spot, somewhere around in there.
01:28Now a character is typically one half of an em, so 0.5 ems.
01:33So if our base unit is 16 pixels, that would be the default size of our text,
01:37I am going to use 8,
01:39I will multiply that by 66, and that gives me 528.
01:45So that is the initial target for my main column, which is going to be on the
01:49left-hand side, for our particular layout, 528 pixels.
01:53So I am going to go back into my layout program, and I've sort of done myself
01:57a little favor here.
01:58I have given myself some starter blocks to work with.
02:01That's the magic of editing;
02:03it's sort of like owning your own cooking show.
02:05You can just put something in the oven and you are ready to go with it.
02:07So here is my first initial containing block, and I am just going to go ahead
02:12and make that 528 pixels wide, and I will just make it 1200 pixels tall to
02:18match the rest of our layout.
02:20So let me get control over this thing.
02:22I am going to line it up right here with our layout grid, and you can see it
02:26just sort of lines up.
02:27The thing that I have to think about here is, remember, when we set up our grid,
02:31we're basing this off of a 12-column layout.
02:35So you can see these heavy gridlines right here are those columns, and there are
02:3912 of them over the course of my layout.
02:42That means that my content regions really need to line up with those columns.
02:47So what I want to do here then is grab this and just move it out until it lines
02:53up with the nearest column, in this case which is right here, and if I'm
02:57counting that, that's 1, 2, 3, 4, 5, 6, 7 columns.
03:02If you are thinking to yourself, "Oh, but James, you had it perfect.
03:05It was going to be 66 characters long."
03:07Remember, number one, that's just an approximation.
03:10If you get close to that, that's fine.
03:12That's kind of a starting point for us.
03:14We have to have everything that we are doing based off of the grid that we are building,
03:17so it's okay.
03:18Plus, the other thing to remember is that there is going to be some padding
03:21inside this and there'll also be some margins between the two columns,
03:24so this isn't exactly the width of the content;
03:27this is the width of the column, and those are not always the same thing.
03:31Now what's really nice about this is since we are doing a two-column layout, the
03:35other column is already defined for us now.
03:37So I am just going to take little guy and move him over and then once that's lined up--
03:44let's go ahead and move this over as well to line that up--
03:46now just so I can keep who is who straight, I am going to go ahead and grab that
03:51color, so that I have two different color columns.
03:54So this is the very beginning of our two-column layout.
03:57We have one region that's based off of seven columns of our layout and we have
04:01another region on the right- hand side that's based off of five.
04:04Oddly enough--and I promise you I did not plan this, this way;
04:06it's just kind of worked out when I was doing this.
04:08I was like, huh, the 7:5 ratio is actually part of the golden mean.
04:13So if you are familiar with the golden mean as a ratio, 7:5 works out to that.
04:18So it should make a very pleasing layout once we are finished with this.
04:22Once I have sort of these regions blocked out, now I need to think about what is
04:25the actual width of these going to be?
04:28So if I click on this particular column, I can see that that column is 560
04:32pixels wide, which means of course that this one is 400 pixels wide.
04:37So I am just going to grab this text.
04:39I would normally make notes in my sketchbook, but here we are.
04:42So I am going to go 560 overall for this particular column and then this one of
04:49course is going to be 400.
04:50That's going to give us the overall container block width for these elements,
04:55but the container block width and the content width is not the same when you're
04:58talking about CSS, because we need to start factoring in spacing, such as padding
05:03and margin and things like that.
05:04So I am going to work with padding now, and I'm going to grab this little block right here--
05:10yellow is a good color for padding--and I am just going to move this out right
05:14here and start working with that.
05:16Again, I want my padding to be based off of my grid.
05:19A single gridline is 16 pixels, so I could use that for padding, but in the
05:23layout like this, 16 pixels is going to be really close to the edge.
05:26You are talking about, like, 1em.
05:28So I am going to go out to the very next gridline, and that gives me 32 pixels,
05:32and that's what I am going to use for my padding.
05:34So I am just going to change the height of this to 1200 to match everybody else,
05:38and that's going to shoot it way up there obviously.
05:39And then I am going to take it and align with the upper right-hand corner right there.
05:44Now I want the same padding on both sides,
05:46so I am going to place that over there, and then I have to think about the
05:50spacing in between these columns. How are these guys going to be spaced?
05:54I really like having a gutter that equals the padding on both sides,
05:58so I am going to grab this and then copy it over.
06:01Now, do I want 32 pixels between them, do I want the spacing on this side?
06:07Actually, in this case, what I am going to do is I am going to split the difference.
06:09I want to put 16 on one side and I am going to put 16 on the other side.
06:14What that's going to do for me is it's going to keep that sort of ratio, that
06:17nice 7:5 ratio that I have got here going on, and not skew it towards one side or the other.
06:23So now I have padding on this side, padding on this side, and then the gutter
06:27between the two of them, which can be controlled through margins,
06:30it can be controlled through padding, or it can just be controlled through
06:33setting the element widths and floating one to the left and one to the right,
06:35just like we did in the last chapter.
06:37But what I need to do now and what I will need to know for my CSS is okay, what
06:42is this going to do to the overall content widths?
06:44So if I grab these measurements, I am just going to come down here and say,
06:48okay, this is 32, this is 32, and that's 32.
06:55Now the rest of these obviously have to add up to 960 pixels, and we sort of
07:00have this overall width.
07:01So this 560, I would subtract 32 from it on this side, and I would subtract 16
07:07for this amount of spacing right here.
07:09So, if I bring this down, that's going to give me a total width right here of 512 pixels.
07:17I will do the same thing right over here.
07:21I have got 400 pixels, and I'm going to have to subtract 48 pixels from that, so
07:28that's going to give me a total width here of 352.
07:33Again, I am not great at math, but 352 pixels wide.
07:38So in essence, what we are doing right now is we're writing our CSS.
07:42We know we have a column on the left- hand side, we know we have a column on the
07:45right-hand side, we know the padding values of those columns on the left and
07:50right side of the respective columns, we know the width of the columns, and we
07:53know how much spacing we need between the two of them.
07:57Now if I wanted to, I could go ahead and throw some headers and footers in here
08:00and just as part of the exercise here,
08:03if I were to do that, I would want that header based off of the grid.
08:07So if I draw a header here and then let's just make it a little bit more
08:11palatable by making a little lighter, I would need to think about how tall I
08:16want this header to be. And because I have this snapping to the grid, I can just
08:20say okay, along which gridline do I think it's going to be appropriate for my
08:24header? And a lot of that of course is going to be what's up there.
08:27In this case, I am just going to go ahead and create a header of 128 pixels.
08:31Notice it's still based on that grid line and now for my header, at least I know
08:35what the height of the header is going to be, and the height of the header here
08:38is going to be 128 pixels.
08:40So I'm fond of saying, and you have heard me say this throughout the title,
08:44that a lot of times
08:45I know what my CSS is going to be before I ever open up my code editor, and I
08:50hope this gives you some idea as to kind of what I am talking about with that.
08:53I know that I am going to have a header, I know that I am going to have a left
08:56column and a right column, I know how tall the header is going to need to be, I
09:00know the amount of padding that's going to be on either column, and I know the
09:03amount of space that I want between the two of them.
09:05So that gives us a fantastic starting point for our fixed-width two-column layout.
09:11Now I am betting that based on everything we have covered up to this point,
09:14you probably have a pretty good idea of what our initial HTML structure of the
09:18file is going to look like, and what the initial styles are going to be as well,
09:22and we are going to test that out in our next movie as we write the styles for
09:26our layout based on the grid that we've just created.
Collapse this transcript
Applying the grid through CSS
00:00Now that we've gotten the hard part out of the way--and trust me, the planning
00:05is definitely the hard part--
00:07it's time to apply our grid to our layout using CSS.
00:11While we'll be using techniques like floats that we've already covered in
00:14previous chapters, we're going to see how our planning really pays off in
00:19making our CSS easier to write and how our typographic styles will tie in to our layout.
00:25So I've got the grid.htm file open here, and you can find that in the 05_04 directory.
00:33And there's just a couple of things that I need to do right off the bat.
00:36One is just sort of give you an overview of the structure of the page.
00:39If I scroll down past the styles here, you can see that we have a header,
00:45followed by a section with a class of col1.
00:48That has a lot of text in it. And as I mentioned before, this layout is very text focused.
00:53After that we have an aside with a class of col2, and at the very bottom we have a footer.
00:58So basic structure.
00:59And of course if I previewed the page in the browser right now, you can see
01:03design-wise, it is no great shakes, but the structure is there, and we know the
01:07CSS that we need to write for this. Okay.
01:09I am going to scroll back up into my styles, and I've got on about line 28 here
01:15a little comment that says layout, and we are going to write the layout styles
01:18directly underneath that.
01:19Before we do that, we're going to modify the body tag, because remember, step
01:23number 1 in our planning process was to define a target resolution for our page
01:28and then come up with a value for that.
01:31So in the body selector what I am going to do is I am going to assign a width to
01:35the page of 960 pixels.
01:38Remember, that's our target size.
01:40And then below that I am going to go ahead and center this content on the page.
01:45I am going to give it a margin of 0 for top, auto for left and right, and 2ems
01:50of margin on the bottom.
01:51Now, the only reason I am doing 2ems on the bottom is I kind of always like my
01:54pages to sort of float above the bottom edge instead of be snugged to it, in my
01:59opinion. It lets the reader know, hey, this is it, the scene and the content,
02:01nothing is being clipped off.
02:03So I am going to go down into our layout, and I am going to start coding.
02:07Now, as I mentioned before, we already know the measurements that we are going
02:10to be using, and I am going to flip back to the planning document that we were
02:13using in our previous movies, and here are all the measurements that we need for
02:17the elements that we are going to be styling on our page.
02:20So we'll come back here, grab the measurements, and go back into the code.
02:23So the first thing we are going to style is our header,
02:25so I am just going to write a selector for our header. And on the first line
02:29here I am going to give it a background color.
02:31Now, this is a decision that was made off-site, so we didn't see this one being planned out.
02:36If you don't like the colors I am using, please feel free to substitute your own.
02:39So rgb value of 76, 67, 65, and then remember the semicolon.
02:47Then I am going to go down to the next line and I am going to give this a margin-bottom.
02:50Now, that's going to separate the header from the content beneath it, and I am
02:54going to go ahead and give that a margin-bottom of 16 pixels.
02:57Since we are basing this off font- size, I could certainly give it a
02:59margin-bottom 1em, but again, I am just sort of reinforcing the fact that this
03:03is based off of 16 pixels, in terms of our grid.
03:07Now, for height, I am going to give it 96 pixels.
03:10And if you were thinking to yourself, hey, wait a minute, in the planning
03:14document, didn't you say 128?
03:15well, you've got to remember that padding plus height and/or width equals total
03:21height and total width.
03:22So back in our CSS, 96 is for the height, and that tells us that we're also going
03:26to add some padding. And the padding for this is going to be 32 pixels top
03:30padding, and then 0 pixels for the right, 0 pixels for the bottom, 32 pixels of
03:36padding on the left-hand side.
03:38So the 32 and the 96 give us the 128 pixels total height that we need, and then
03:43the 32 pixels of padding on the left- hand side matches the 32 pixels' worth of
03:48padding that we want everywhere.
03:49So now we've defined our header and we're going to move on to our next element,
03:53which is our first column.
03:54So I am going to write a selector for col1.
03:56Now, that's the class name that we used for this column.
04:00Since we only really have a section and an aside, I could use those elements
04:05that they are inside of as well, but since we have those class names available
04:07to us, I am going to take advantage of them.
04:09So this is col1, and I am going to float this to the left.
04:12Now, just like we learned in our last chapter, this is going to create a two-column
04:16layout, one column on the left-hand side, another column on our right-hand side.
04:19So this one is going to be floated to the left.
04:21I am going to give it a padding to the left of, you guessed it, 32 pixels
04:26and, again, if I am looking for our width, I can go back to the mockup and it
04:30tells me 512 pixels.
04:32So on the next line I am just going to type in width and I am going to do 512
04:39pixels and save that.
04:41I am going to scroll down a little bit more so you guys can see this a
04:43little bit better, and then we are going to go ahead and create the styles
04:47for our second column.
04:49So that one is .col2. That's a class selector for the second column.
04:54This one is going to be floated to the right.
04:55So again, this is going to help us create our two-column layout:
04:58one is going to be on the left-hand side, one is going to be on the right-hand side.
05:01Now, if I go back to our mockup, I can see that I need 32 pixels of padding on
05:04the right here and I need 352 pixels for its width.
05:09So if I go back into my code, I go down to the next line, and I'll just go
05:13ahead and give it that width of 352 pixels.
05:17I'll give it my padding-right of 32 pixels, and then I am just going to do a
05:25couple of things here for typography.
05:26I am going to give it a font-size of 90%.
05:28I want the text in the sidebar of the aside there to be a little bit smaller
05:33than our body copy text. And I'm going to give it a line-height of 1.6, so
05:38there's a good amount of vertical separation between the text there. All right!
05:41I am going to go ahead and save that, and then we have one more element left to
05:45do and that is the footer.
05:46So I am going to write a selector for my footer.
05:48I am going to scroll a little bit so you guys can see this a little bit better.
05:52Again, for our footer, we'll do a background color.
05:55Again, feel free to change this if you don't like the color that we are working with here.
05:58I am just going to do rgb. The value for this is going to be 100, 98, 102. Don't
06:06forget the semicolon.
06:07And then on the next line, we are going to go ahead and give that a height of 80 pixels.
06:12Now, I know we didn't put this in the mockup but, again, that 80 pixels, the
06:15height there is based off of that 16 pixels.
06:17We don't have any content in our footer right now.
06:19Later on, if we put content in it, we would probably add a little bit of padding,
06:23and then we would have to adjust that height value.
06:25And then, because we're doing a two- column layout with floats, what do we need
06:29to do to the footer that we learned in the last chapter?
06:31That's exactly right.
06:32We need to go ahead and clear that footer, so I am just going to do clear:
06:34both and save that.
06:37So I am going to go ahead and preview this in my browser again, and when I
06:42refresh the page, I can see my two-column layout.
06:45The layout looks okay, but it doesn't look particularly tied together, does it?
06:50And the reason for that is that even though these elements are based off of that
06:5416-pixel grid that we were working with, the text right now isn't.
06:59So there's really nothing to sort of tie it together visually, and even though
07:04the columns are spaced the way we want them to, we're really not getting that
07:07sensation that this is all tied together.
07:10So it's not just about defining element widths;
07:12you really have to think about the layout as a whole.
07:14I am going to go back into my code and I am going to uncomment out
07:18these typographic styles.
07:20So there's a little beginning comment right beside the h1 there, and then if I
07:24scroll down, there's an ending comment at the bottom of it.
07:26Now, if I save these and go back into my browser and refresh, you can see that
07:33now that looks a lot better, a lot more put together.
07:37It looks a lot more tied together, and that's because all of the typographic
07:41styles that I just enabled are also based off of that sort of 1em 16-pixel grid,
07:48so everything sort of ties together.
07:50Now, if you want to examine that in more detail, just go back into the code and
07:54go through all the styles that I created here.
07:56There's really not that many.
07:56There are just four styles.
07:58But go through, take a look at their font-size, take a look at the margins that
08:02we're using here, take a look at line-height and the spacing, run these
08:05calculations and sort of figure out how they tie in to the same 16-pixel grid.
08:10So the majority of our layout is now finished, and let me ask you: How much
08:15easier is it to have that out of the way during the planning stages, so that
08:19you just come in here and you just plug in your values and you know your layout is going to work?
08:24That's why it's so important to spend the time that you need in the planning
08:28stage to make sure that everything with your layout is working as you intend it
08:32to within that grid that you defined.
08:34So for the most part our layout is done, but even though our layout is primarily
08:38focused on text, it doesn't mean that we won't occasionally have images or other
08:43types of assets that we're going to need to add to this page context.
08:47So in our next movie, we're going to take a look at how to make sure our page
08:51assets are working with our grid that we've established as well.
Collapse this transcript
Creating grid-based assets
00:00If you are going to create a grid-based design, it's important that all page
00:04elements relate to the grid in some way.
00:06This will help your design achieve a more balanced composition and help you
00:10create consistent layouts from page to page, which is especially important for fixed layouts.
00:15To illustrate this, we will work with the addition of assets to our page and
00:19make sure that they follow the layout guides laid down by our initial grid. And
00:23to that end, I have a document here, the fixed_planning_elements, which is just
00:27a modified version of our earlier planning document.
00:30You can find the finished version of this in the Assets folder, directly in
00:33your exercise files.
00:34Again, it's not important that you have this open.
00:36We're just sort of going over some concepts here.
00:38This is another thing that I do when I plan sites.
00:41In addition to planning out the columns, I also plan out any type of assets that
00:46might go into my content and how it relates to that grid by creating some
00:50default measurements for it.
00:51As you can see in the left column here, I have several default sizes for assets,
00:55including assets that might go side by side.
00:58You are free to kind of play around with these and experiment with different
01:01sizes and sort of compare how they might relate to each other.
01:03For example, I could certainly work with the height of these, make a couple of
01:07them perhaps a little bit more vertical, but what I am doing is I am making sure
01:10that each one of these adheres to the grid in some way.
01:13So you can see they are all divisible by 16.
01:15They all fit specifically into the grid itself.
01:18Now, I've set other policies in place too.
01:20For example, in the right-hand column, the only way I want assets to display in
01:24the right-hand column is all by themselves and sort of floating in the middle of
01:29the column, so I developed a default size to do that.
01:32Whereas in the left-hand column, I have to really think about these assets and
01:35how they are going to relate to the content.
01:37Will the content flow around them?
01:39Will the content be on a line all by itself?
01:42Will it sit aligned left, or will it float in the middle of the layout?
01:47Those are all things that I have to consider, but I can use this to sort of
01:50gain some measurements.
01:51If I have two elements that are going to sit side by side like this, for
01:53example, I now know what their width is going to be, what the gap between them
01:57is going to be, that sort of thing.
01:59So this is something I want to plan out beforehand.
02:01These don't have to be just image assets.
02:03These could be any elements that you use in your page.
02:06They could be pull quotes.
02:07They could be asides within an article.
02:09So there are all sorts of different things that these could be.
02:12They could be video.
02:13So you start planning out where those assets are going to go and how they are
02:16going to adhere to the grid.
02:17So now I am going to go back into our code, and I have opened up the grid.htm
02:21file from the 05_05 directory.
02:25Now, you are going to notice if you look in the exercise files that there's also
02:27an images folder in that directory, and you can see we have several images here.
02:31Those images were sized based on the measurements that we determined within
02:35the planning documents.
02:36So the names of the images correspond to their widths and heights.
02:39If I go through the code of my document, I can see that those images have been
02:43added to the page so that we can see how they relate to the content and how they
02:47fit within the grid.
02:48Now, if I scroll through my code, I can also see that we have two brand-new
02:52sections right here within the main column, with the class of news, that we are
02:56going to have to style in order to fit within our layout grid as well.
03:00Now, if I go ahead and preview this page in our browser, I can see the images
03:04look more or less harmonious with the layout and with the grid, but of course,
03:09stylistically right now, they are not interacting with the content at all.
03:12So I am going to have to work on their styling.
03:15And if I scroll down, those two sections really aren't doing anything to
03:18differentiate themselves from the content, so we are going to need to work on
03:21the styling of those as well.
03:22So I am going to go back into my code, and I am just going to add these right
03:26here underneath the footer in our layout styles.
03:29So about line 53 or so, I am just going to create some empty space here.
03:33So the first thing I want to address is how my content is going to interact with images.
03:37So I am just going to go ahead and write a selector for my images on the page,
03:40and I am just going to float the images to the left. And I am going to set
03:45their margin to 0 for top, 1em for right-- and what that will do is it will just
03:51sort of hold the text off of the right edge of the image--0 for bottom and 0 for left.
03:57So just setting some images there.
03:59Now, if I save this and refresh my browser and go up, I can see now, for these
04:04images, the text is floating around it, and it's looking okay. But remember, we
04:09wanted this image in the right-hand column to be centered and sort of all by
04:13itself so that the text isn't wrapping around it.
04:16For this larger image, I don't think having a text around it is very
04:20advantageous either. I think, obviously, that's hard to read.
04:22It can be very confusing. People are going to wonder if that's a caption or if
04:25there was a mistake made there.
04:27So for both of these we really need those images to sort of stand alone and by
04:31themselves within the layout.
04:33So to do that, we are going to apply a class to them.
04:36So I am going to go ahead and write the class selector, and we'll just call it largeImg.
04:42So just large image, .largeImg.
04:45And then for the properties, we'll go ahead and set the display property to block.
04:49We'll turn floating off, so we'll say float: none, so it doesn't float.
04:53And then for margin, what we'll do for margin is we'll do 1em top and bottom,
04:57and then we'll auto-margin the left and right sides, and as we know, that's going
05:00to center that content within those columns.
05:02Well, that's only part of it.
05:03We've got to go down to those images and we've actually got to apply that.
05:06So what I am looking for is down on Line around 101,
05:09we'll find this 448_400 image, so we are going to go ahead and grab that, and
05:15I'll go ahead and apply that class to that, the largeImg class.
05:18And then scroll down a little bit further, find the 256_256 image, which is in
05:24the side, and we'll go ahead and apply the largeImg class to that as well.
05:30So we'll go ahead and save that.
05:31Now, if I go back into the browser and refresh this, you can see what that does to
05:35the image in the sidebar.
05:36It's now sort of on its own line, the text is not floating around it, and it's
05:40centered within the column.
05:41If we go down to our larger image, it's taking up its own space,
05:45it's centered within that column, and it's interacting with the layout in a
05:48much pleasing fashion.
05:50Then the last thing we need to do here is we are going to style these two
05:53elements so that they are sitting side by side each other.
05:56In our planning document we're able to sort of sit these side by side so we
06:00know exactly how much space we need between them, and we know what their width is going be.
06:04In this case, the width is going to be around 240 pixels based on our fixed layout.
06:08So I am going to go back into my code.
06:10I am going to scroll up to our CSS, and I'll stick with the layout styles.
06:16And what I am going to do is create a news selector, because you may have
06:19noticed the class attribute on both of those sections is news.
06:23We are going to go ahead and give it a width of 208.
06:26Remember, total width and width are not the same thing.
06:29So if I set some padding on this, I'll set 16 pixels' worth of padding, so 16 on
06:35one side, 16 on another side equals 32, add that to the 208, and then we get our
06:39240 pixels' worth of width.
06:42For font-size, I am going to go to 90%, which is going to just decrease the
06:46font-size inside those discreet sections just a little bit.
06:49For line-height, I am going to go to 1.8 to give a lot of space in lines, and
06:55that's just personal preference.
06:57And then for background, we are going to go ahead and give this a
06:59background color as well.
07:00I am going to do rgb (237, 226, 197), and don't forget your semicolon.
07:08And then finally, I have to handle the spacing and the positioning of these, so
07:12the margin-right for this is going to be 32 pixels, and then finally, we are
07:17also going to float them to the left.
07:18We are almost done. I know that seems like a lot, but it's really not too
07:21much styling on these.
07:22Remember, we are floating them side by side. The margin-right is going to give
07:25us 32 pixels' worth of space between them, and then we've just sort of qualified
07:28the dimensions of the elements as well.
07:30However, remember, we only want that spacing on one side,
07:33so I am going to come in here and write another selector.
07:35I am going to say, hey, anytime you find a news section following immediately
07:41after another news section, set its margin-right to 0.
07:45So I am going to go ahead and save that, and we are going to go back into our browser.
07:49I'll refresh the page, and you can see now we've got those two elements.
07:53The space in between them is even.
07:55And because they are based off our grid, they relate to the rest of the layout quite well.
07:59Now, if we wanted to, we can certainly polish this layout up a little bit, but
08:03the overall organization and structure of the layout is quite sound, thanks to
08:07the underlying grid that we are using.
08:09Now, I use some sort of grid on almost all my layouts, regardless of what
08:14type of layout it is.
08:15To help underscore the importance of grids in design, I want to give you some
08:19excellent resources on designing with grids, and we are going to do that in our
08:23next movie before we move on to the lab.
Collapse this transcript
Grid design resources
00:00Before we move on to the lab, I want to leave you with some of my favorite tools
00:04and resources for working with grid-based designs. And I've included this file
00:08for you in the 05_06 directory;
00:12it's the grid_resources.htm file.
00:15Now, the first site I want to take you to it is Design by Grid.
00:18Now, this is a great collection of articles, tutorials, and resources on
00:22designing with grids, and they also have a nice showcase of sites that focus
00:26on grid-based design.
00:28Perhaps my favorite site in dealing with grid-based layout is The Grid System,
00:32and you can see they have different articles.
00:34They have some tools that can help you design with grids.
00:37They have different books, links to grid-based templates, and a blog.
00:41Now, you'll notice from these dates that they haven't updated in a while,
00:45so I'm not really sure what the long- term prospects for the site is, but
00:49the articles and the information and the tools that you'll find on the site are invaluable.
00:54So even just because they're a couple of years old, it doesn't devalue what
00:58this site has in terms of resources, so this is a site you should definitely check out.
01:02Now, I also recommend reading through The Elements of Typographic Style
01:07Applied to the Web.
01:08It's a very long title, but this is a web adaptation of Robert Bringhurst's
01:13famous The Elements of Typographic Style book and again, it's being adapted
01:17for web typography.
01:18Now, it's slow going.
01:20They're going all the way through Robert's book, and right now they're just sort
01:24of beginning on Chapter 3 and work kind of comes and goes with this, but this is
01:29an absolutely fantastic resource.
01:32It's not just for grid-based design, but since typography is such an important
01:36part of your web site and understanding how to control typography correctly and
01:40how to style it correctly for the web is going to help you in great type into
01:44your grid-based design.
01:45So this is definitely a resource that you need to spend some time with.
01:49I'm also a really big fan of the Golden Ratio Calculator.
01:52I love designing with specific ratios, and there's a lot of different ratios you
01:56can use, but probably the Golden Mean is the most famous.
01:59And with this you would just enter a width or an important number.
02:02Let's say, for example, we're going to do a 12-column design.
02:05I can type in 12 and I get a couple of different sets of measurement.
02:09This one says, okay, if you're going to use a 12-column design, a 7:5 split
02:14fits that golden ratio.
02:16On the other side of it, it says, if one of your columns is based on 12
02:19units, the column on the other side should either be 7 or 19, or you could even do both.
02:24So this is a really neat tool for you to help understand what the ratios of your
02:29different page elements and grid- based design should be based off of.
02:33I'm not a huge fan of grid systems. I'll be honest with you,
02:35I don't use any of the grid systems that are out there.
02:38I prefer to make my own. But that doesn't lessen the impact that they've had on
02:42the design world, and probably the most famous grid system out there right now
02:45is the 960 Grid System.
02:46A ton of people are using this, but if you go out to Google and you search for
02:50CSS grid system, you're going to find dozens of these out there that people are using.
02:55So a grid system is essentially a collection of styles that are sort of
02:59preformatted to help you design on a grid, and they'll include classes that
03:04you can use in order to control the widths and the layout of the elements on your page.
03:09So they're very handy to have, but they do add a little bit of bloat to your CSS.
03:15But what I like about the 960 Grid System site is there are a lot of really
03:19cool things on here.
03:20They have a custom CSS generator.
03:22They have nice documentation that will help teach you a lot about grid-based
03:26design, not just using this particular system.
03:28They also have a Grid Overlay Bookmark, which will help you design a longer
03:32grid, even if it's not this one, and they have some neat resources like this demo.
03:36For example, if you were designing on a 960 grid, it helps you understand the
03:40different column breakdowns that are possible and some of the different
03:43measurements for columns so that they will fit within the system.
03:46Now, of course their spacing here is based off of 20 pixels.
03:49So if your base unit of measurement is going to be different, it's not going to
03:51fit exactly into this column spacing, but you get a good idea of what's
03:55possible within 960 pixels.
03:58They also have a nice gallery, or showcase of sites, that are using the 960 Grid,
04:02and one of the things that this will help you do as a designer is it's going to
04:05help inspire you and going to help you see that designing on a grid doesn't
04:08have to be restrictive.
04:09You can really have just an amazing variety of layouts created off of a single
04:15type of grid system.
04:16So the 960 Grid System, I would encourage you to go check that out and read a
04:20little bit more about it.
04:21Now, I also have another gallery site here, Grid Based, which has a gallery of
04:26grid-based designs. And again, this is a great place to go for inspiration or
04:31just seeing how certain people have designed on the grid.
04:33You can go into their CSS, break down the grid that they're working with,
04:37and sort of understand how you might want to adapt that grid to your
04:39particular workflow.
04:42Finally, I have a couple of little tools here that I really like.
04:44This one, #grid, it's a nice little, lightweight piece of JavaScript that you can
04:49start putting into your sites as you're developing them.
04:52And what this allows you to do is, for example, if I hit G on the keyboard, you
04:56can see here it gives me a grid overlay of my site.
04:59So as you're working, you can set up this grid to the measurements that you
05:03need, and then you can just toggle that grid on and off when you're testing
05:06your page to make sure that you're lining things up the way you're supposed to within your grid.
05:11A very similar tool to this is the 960 Gridder tool, and this 960 Gridder tool
05:16actually is just a bookmark.
05:18So if I take this and make a bookmarklet out of this particular file, then I
05:22have the ability, anytime I click on that bookmarklet, to have this overlay.
05:26And what's really nice about this one is you get a lot of control over the
05:31opacity of this, over the number of columns that you're going to be using within this grid,
05:35so you can just kind of modify this at any time.
05:38And then using the keyboard shortcut of Alt+Ctrl+C, I can just toggle that grid
05:42kind of on and off once it has been enabled.
05:44So again, that's another resource that will help you develop grid-based designs
05:48by allowing you to sort of test within the browser and overlay the grid that
05:51you've established right on top of it.
05:53I want you to keep in mind that grids are just tools.
05:57They can help you create harmonious designs and they certainly make the process
06:00of planning things like fixed layouts a lot easier.
06:03However, don't see them as just rigid structures that restrict your ability to design.
06:08Now, at the end of the day, you're the designer and your design has to work on
06:12a level that's acceptable to the client, and perhaps more importantly, to the audience.
06:16Incorporate them into your design process, but don't be afraid to go outside the
06:19grid if the demand warrants it.
Collapse this transcript
Building fixed layouts: Lab
00:00For this chapter's lab, I want to do something a little different.
00:04Instead of opening one of my partially completed sites and then finishing it, I
00:08want you to create a fixed-width layout from scratch. Well, almost from scratch.
00:14There's a little bit of a catch here.
00:15I actually want to find one of your favorite layouts and then re-create it. By
00:20examining an existing layout and then breaking down its grid, you'll gain a
00:24greater insight into the planning and structuring that goes into creating sites.
00:28Just as the way of showing you kind of the workflow that I want you to do for
00:32this lab, I have the lynda.com site open here.
00:35Pick one of your favorite layouts;
00:36it could be any site you want, or just a layout that you like.
00:38If it's a site that you can get into and kind of look through the CSS,
00:42that's even better.
00:43So in the case of lynda.com site, sometimes it's kind of hard to pick out
00:48exactly which CSS file I should be looking at.
00:52So you might want to try to find one that is a little bit easier to go into
00:57and look into the appropriate CSS, but that's not critical for what we are doing here.
01:01You don't absolutely have to be able to know what's going on with their CSS.
01:05So when you go to one of your favorite sites, just go ahead and take
01:07a screenshot of that.
01:08Now I have taken the screenshot of the lynda site, and I'm dropped this in to an
01:11Illustrator document. And I know, I am in Illustrator again, right, but it does
01:15not matter which page- layout program you're using here.
01:17You can even print out a copy of the site that you're working with and then just
01:22sort of draw the things that I'm talking about on the screen.
01:26So the first thing you should do is find out what the underlying grid is for
01:30this particular site, and the way that I did this here was just place guidelines
01:34on the page, where it's obvious that a column or a gridline is.
01:39I am looking and making sure that these columns are equal width
01:41so I can really determine what the grid structure of the page is, and once I do
01:45that, I then come in and overlay shapes where I think of the page regions.
01:50So obviously this is a two-column layout from the standpoint of the fact that
01:53I have a right column over here and left column over here, but the left column
01:57itself is broken into three separate columns as well. I needed to sort to know
02:02sort of the spacing requirements for those.
02:04So at this point, I would go in, I'd start taking some measurements,
02:07I'd start thinking about the initial structure of this page, and then I'll have
02:11what I need to create the initial HTML and CSS.
02:15Don't get me wrong.
02:16I'm not asking you to re-create these layouts pixel perfect;
02:19I'm asking you to recreate the structure of these layouts and then place your
02:22own content inside of them.
02:24To that end, if you go into the exercise files, if you look in the 05_07 folder,
02:31I have a bunch of images that you can use.
02:33You can resize those images any way that you want to fit within your layout.
02:38Again, just make sure they adhere to the grid that you have established for that
02:41layout. And then I have a couple of text files.
02:44As a matter of fact, these are all Mark Twain's, A Dog's Tale.
02:46There is a lot of text there that you can copy and paste and use for dummy text.
02:51I can't stand using Loren Ipsum text, so I am just giving you some old copyright-free,
02:55I might add, Mark Twain text, it's fun to read as well.
02:59So pay attention to the grid, what the grid is based on.
03:02You want to go ahead and establish a target resolution, which doesn't necessarily
03:05have to match the layout that you are kind of basing your site off of.
03:09And then when you get into placing assets in that, really concentrate on making
03:13the typography fit with that layout grid as well.
03:16Now when you're finished, go back to the site.
03:19If you can, find its CSS. Compare your layout styles to theirs.
03:23Again, on more complex sites like this one, they might be a little hard to
03:26find the CSS. Coming through the styles and isolate and layout might be a
03:30little difficult, but you should be able to at least compare the basic
03:33structure and styles.
03:34In fact, you may want to do that stage before you get started, just to make sure
03:39that comparing styles is possible once you're done.
03:41Just don't evaluate the styles too much before you begin.
03:44The whole point of this lab is to see how closely you can match the desired styles.
03:48Keep in mind that you are only comparing styles just to compare and evaluate
03:51your approach to layout.
03:53The overall goal of this lab is to gain experience in planning and
03:57executing desired layouts, not making sure that you're matching somebody
04:01else's files exactly.
04:02I know this is a big, broad, open-ended lab. Go have fun with it.
Collapse this transcript
6. Building Flexible Layouts
Designing for flexible layouts
00:00Flexible layouts allow you to create layouts that adjust based on the width of
00:04the browser or the viewport.
00:07If the browser is resized, flexible layouts will increase or decrease the
00:12available space for all content regions within a layout.
00:15This gives you the ability to design a layout that takes advantage of the space
00:19available to it, and can present a different look to viewers, based on the size
00:22of the browser window.
00:24Designing flexible layouts can be much more labor-intensive than designing fixed
00:28layouts, as you have to carefully plan out the relationship between all the
00:32elements within the layout.
00:34What looks good at one size doesn't necessarily work at another size.
00:39That means that just like fixed layouts, there are pros and cons to
00:42using flexible layouts.
00:44Now the biggest positive for you users is that you have a layout that conforms
00:48their current resolution, and they're not having to do unnecessary scrolling,
00:52zooming, or size adjustments.
00:55It also gives them the freedom to resize the window without the additional
00:59penalty of horizontal scrollbars or content being lost.
01:02As a designer, you're freed from the rigid constraints of fixed layouts.
01:07Instead of defining a pixel-perfect relationship between elements, you focus more
01:11on how those elements should relate and interact to each other in a generic
01:15way, and are less worried about exactly how much space a certain gutter or
01:20element is occupying.
01:21The flip side of that is that you do have to give up a certain amount of
01:25control over your design,
01:27and designers often balk at not being able to control exactly how their content is viewed.
01:32But with flexible layouts, you have to be content with losing some of that control.
01:37Flexible layouts also often have element-spacing issues, especially at the
01:42extremes of viewport width.
01:43Now, really large resolutions often end up with a tremendous amount of space
01:48between elements, while smaller resolutions often create layouts that don't
01:52have enough separation.
01:54That's why it's common to have minimum and maximum width values specified for
01:58flexible layouts, to create a range of acceptable resolutions.
02:02Now this also means the flexible layouts are a lot harder to design.
02:06They require more planning, and the code for them is generally more complex.
02:11Keep these factors in mind as you begin to plan your layouts and they'll help
02:14you determine whether a flexible layout is right for your project or not.
02:19For the rest of this chapter, we will break down what goes in to planning and
02:22building flexible layouts, and we are going to start by tackling one of the
02:25hardest aspects of it first, determining your layout dimensions.
Collapse this transcript
Calculating percentage values
00:00For the most part, you'll find that planning a flexible layout is almost exactly
00:04the same as planning a fixed layout.
00:07Now, the biggest difference, and honestly one that adds just another entire
00:12layer of difficulty, is calculating the percentage values that you'll need for your elements.
00:18Now, rather than setting a fixed-pixel values for widths, margins, and padding
00:23you'll need to determine the proper exchange values for the desired layout.
00:27Because of the way percentage values work, this can often be a little tricky, so
00:31I wanted to take some time to go over it here.
00:33Now, again I have a document open.
00:35I've got fluid_planning.ai, which is an Illustrator file opened up in Illustrator.
00:39If you want to do this a little along with me, feel free to grab another piece
00:43of sketch paper, or you can open this up in Illustrator if you have Illustrator.
00:46It doesn't really matter. What's the most important thing here is that we're
00:49focusing on how to properly calculate percentage values for elements within your layout.
00:55When I use the term fluid and flexible, it means that the web site is responding
00:59to the size of the browser change.
01:01So, really they're the same thing.
01:02I don't mean to cause any confusion, but I find myself sort of using those terms
01:06interchangeably, and I felt like I needed to go ahead and define them for you.
01:10Just like our fixed layout, we've got a two-column layout going on here.
01:13This time I based the grid off of 10 pixels, just again, makes a little bit
01:17easier for us to do the math.
01:19And I want to go over the steps that I have over here on the left-hand side of
01:22the page, steps 1, 2, 3, and 4, and I want you to compare them to what we were
01:26doing in the previous chapter with fixed layouts, in terms of planning.
01:30First, just like with fixed, go ahead and define a target resolution and a grid.
01:34Now, that sounds kind of odd because you're like, wait a minute, if I'm doing a
01:37fluid layout why am I establishing a target resolution?
01:40Well, essentially what you're doing when you establish a target resolution with
01:43a fluid layout is you're saying okay, this would be the "ideal size" for this,
01:49and what that's going to allow you to do is it's just going to help you pick out
01:51values for padding and margins and things like that.
01:55If you say okay, I'm targeting an ideal size for this at 960 pixels, for
01:59example, you can say I want my padding to be 20 pixels or 40 pixels, and then
02:03it's going to help you calculate what percentage of your overall width 20 or 40
02:07pixels is going to be.
02:09To make calculating percentages easier, you mock up that layout at an ideal
02:13size, just as we mentioned here with the target resolution. So we're going to
02:16go ahead and mock that up just like we have here with two columns and we've got
02:20some spacing in here.
02:22Then we use our ideal measurements to calculate percentage values.
02:24Now, what does that mean? Well, I'm going to turn on the measurements and show you.
02:27In this sense, we were targeting 960 pixels' worth of resolution.
02:31We were planning this as if we were doing a fixed layout.
02:34I've got 590 pixels total width for the left-hand column, 350 pixels of total
02:39width for the right-hand column, and then 20 pixels' worth of space between them.
02:43That means that we have got 20 pixels' worth of padding over here on this side,
02:4620 pixels' worth of padding on this side and then 330 pixels of width, 570 pixels
02:52of content width for those columns.
02:54So, we're sort of creating those numbers first as a starting point for our fluid layout.
03:00The most important thing that we're going to cover in this entire lesson is point number 4.
03:06Remember that margins and padding calculate their values as a percentage of
03:12the parent element.
03:13We'll talk about what that means as we go through the planning stages.
03:17Okay, so the first thing that I do is when I have these sort of top global
03:20values, I come up with just whatever the percentages of those are going to be.
03:25I'm going to bring up the calculator.
03:27This is pretty easy to do.
03:28I'm just going to say okay, 590 pixels divided by 960 pixels gives me .614, so
03:36forth and so on, so around 62% if you will.
03:38Now, browsers can handle decimal points just fine and if you look in certain
03:43people's layouts when they do fluid layouts, you'll find percentage values that
03:47go out to the 100ths or 1000ths or 10,000ths of a point.
03:51It's up to you to decide kind of how precise you want to get with this.
03:55In this case just to keep things simple, we're going to round our values up.
03:59Same thing for the 20 pixels. So if I take 20 pixels divide that by 960, I get around 2%.
04:07If I take 350 pixels and divide that by 960, I get around 36%. And again, I can
04:14round these values if I just want to make things a little bit simple for us,
04:16because remember, we're not expecting this to be a specific size.
04:19We're more interested in what the proper percentages is to get this ratio. All right!
04:24So, I'm going to go back into Illustrator.
04:26I'm just going to turn on those top- level percentages, and you can see we're
04:29rounding here: 62% for that, 2% for the space between them, and then 36% for the
04:35overall right-hand column width.
04:36Now, those values are helpful to know, but they're not as important as the
04:39values underneath them.
04:40Now, values underneath them, remember, are the padding for left-hand column; the
04:45content width for the left-hand column, which is 570 pixels; the spacing or the
04:50margin between those two columns, which is 20 pixels; and then the content width
04:53and the padding of the right-hand column.
04:55When I calculate these percentages--and I'll go ahead and turn those on--here is
04:59where people typically get confused when doing fluid layouts.
05:03Inside this particular element we have a width of 570 pixels and we have
05:09padding of 20 pixels.
05:10Now, when you're calculating this it's really, really tempting to say okay,
05:15what percentage of 590 is 20 pixels, and that's what I need for my padding, but that is wrong.
05:21Padding and margins are calculated based upon their parent elements.
05:25So, this element, this orange column right here, that element's parent element is
05:29the body tag, which is based off of the 960 pixels.
05:33So, in order to get the correct percentage for that padding, I have to say what
05:37percentage of 960 pixels is 20 pixels and in this case it's 2%.
05:42So, I break my percentages down.
05:432% for the padding, 60% for the content width, 2% for the spacing between them,
05:4834% for the content width here, and then 2% for the padding.
05:53Now, that's pretty easy to calculate on a top-level basis, but the more complex
05:57your layout gets, the harder it gets to calculate the proper percentage values,
06:01especially for margins and padding. And let's demonstrate that by taking a look
06:05at the possibility of adding interior shapes to our layout.
06:08So, if I just do the interior measurements of this particular element, we have
06:11250 pixels for the content width of this and we have 10 pixels for the padding
06:16all the way around it.
06:17Now, that's going to leave us with right around 20 to 30 pixels' worth of
06:23spacing in between that.
06:25If these interior shapes have padding more than 10 pixels, remember their parent
06:29element is this orange left-hand column.
06:33So, that 10 pixels needs to be calculated as a percentage of the width of this column.
06:39However, for padding and margins you don't use the total width;
06:44you use the content width.
06:47So, I need to know what the percentage 10 pixels would be of 570 and not 590.
06:53That is a really big distinction, because when you're calculating the content
06:58with itself, in this case 250 pixels, I have to calculate that based on the
07:03container width itself.
07:05To make this point again: when you're calculating percentages just for content
07:08width, you calculate that based on total width.
07:13When you calculate percentages for margin and padding you calculate that
07:17percentage based on just content width, and so because of that, when I turn on
07:22these values again, you can see that for the padding of this I'm at 1.75%.
07:28Again, I'm rounding here.
07:29For content width I'm at 44.5, but for spacing between these two, I'm actually
07:33at 4%. And again, those values have just been rounded.
07:38So, I can't just pass this 2% value here or here and get what I'm expecting.
07:44So, I can't just calculate the interior padding or margin between these based on this 2% value.
07:49I have to go back to the overall content width and container width from
07:52that column to do that.
07:54Now, we have the percentage values that we need to create our fluid layout.
07:58Now remember that above all else fluid layouts are really just about the
08:02relationship between elements.
08:04Don't get too caught up in finding the absolutely precise percentage points for
08:09a target measurement.
08:10I mean it's helpful to use this as a starting guide, but don't get too
08:14carried away with it.
08:15What you do need to remember, however-- and this is the most important thing from
08:19this movie--that margins and padding values are calculated based on the content
08:23width of their parent elements.
08:25In most cases when the fluid layouts fail it's these values that typically turn
08:30out to be the problem,
08:31so you want to pay extra attention to them when you're planning and
08:34writing your styles.
Collapse this transcript
Setting flexible width values
00:00In our last movie, we calculated the percentage values that we are going to need
00:04to create a two-column fluid layout.
00:06In this movie, we are going to put those values into practice by applying them
00:09to the appropriate elements.
00:11So I have the fluid.htm file open from the 06_03 directory, and if you remember
00:18from our fixed layout in the last chapter, we have a page structure that is
00:23pretty much almost exactly the same as we had before.
00:27I just want to preview this page in a browser really quickly to show you
00:31something about default styling. All right!
00:34So I have opened up our page in the browser, and you can see we don't really
00:36have any margins or padding goings on within the elements themselves. And as we
00:41look through this, obviously the elements are just sort of stacking one on top of each other,
00:44so we still need to position everything.
00:46But as I resize this browser, I want you to notice something about layouts.
00:51By default, layouts are fluid.
00:53If you have a block-level element, that block-level element, if a width is not
00:57defined on it, it will go out and stretch to fill the parent container, meaning
01:01its width is set by default to 100%.
01:04Even if you don't do anything at all, your layout is more or less fluid.
01:08So I am going to go back into our code, and the first thing that I'm going to do
01:12is I'm going to scroll down into my styles to the body selector. And we need to
01:17define a width for this.
01:18Now, unlike in the previous chapter when we were doing fixed layouts, instead of
01:22applying a fixed-target resolution to this, I'm simply just going to use a
01:26percentage value for the width.
01:28And in this case, I'm going to go ahead and give it a width of 80%.
01:31How wide you make the wrapper container, in this case the body selector,
01:35is entirely up to you.
01:38In some cases people like to do 90%, 95%, if you don't want a whole lot of space
01:42on the edges. It really boils down to how much space you want on the right and
01:46left side of this. If you're centering this at 80%,
01:48if I save my file and go back to the browser and refresh that, you can see it
01:52gives me a nice amount of space over here on either side, and based on the
01:57available browser space, it's going to give you nice column widths as well. Okay.
02:02I am going to keep scrolling down, and one of the things that most people forget
02:05to do in fluid layouts is set padding.
02:07It happens all the time.
02:09You concentrate so much on the columns of your layout and the various elements
02:13that you sort of forget that every single one of those elements that needs to
02:17have padding that's going to line elements up, should have the same padding.
02:20So this header, we need to go in and change this padding here so that it's
02:24getting a percentage for its left padding.
02:26So where right now I have 32 pixels--and remember that was our old fixed layout--
02:30I am going to highlight this and I am going to change that left padding to 2% and save that.
02:36So now it has 32 pixels' worth of top padding and then 2% for the left.
02:41And if I go out to the browser and refresh that, you can see the spacing here in
02:46the headline up in the header changed a little bit. Okay.
02:49Now, we are going to go down and do our columns.
02:51Notice we have column1 floating to the left. We have column2 floating to the right.
02:55Now, they don't have any defined widths right now, so we're really not getting
02:59the effect we want to with those floats.
03:01So with column1, I am going to go down in that selector,
03:04I am going to give it a width of 60%.
03:07Now, we remember those from our previous exercise.
03:09If you still have your notes you have taken, if you were sketching that in
03:12a sketchpad, you should be able to see those percentages and just plug those right in.
03:16Underneath that I am going to do padding to the left and again, I am going to
03:20do a padding-left of 2%, and will save that.
03:24Of course that means I am going to go down to column2 and do something very similar.
03:28In this case, column2's width is going to be 34% and its padding, this time to
03:35the right and not the left, is also going to be 2%.
03:37You will notice because of the layout method that I use of floating one to the
03:40left and floating the other one to the right,
03:43I don't need to specify any type of margins between the two of them. Their
03:46widths will create an absence of space in between them, which gives me the
03:50spacing, and I don't have to actually declare that, which, for me, is nicer. Okay.
03:54I am going to save this.
03:55Once again, I will go out to the browser, refresh the page, and there is
03:59our two-column layout.
04:01Again, if I resize the browser, you can see that this is now flexible.
04:05I am getting a flexible amount of space and a flexible amount of padding
04:08for both of those guys.
04:09Now, down a little bit lower, we still have these two elements, these
04:13two interior elements.
04:14So we're going to go in and set those percentage values on the interior elements
04:18that we were calculating in the last exercise as well.
04:21So going back into the code, I can scroll down and I can find those two
04:26news items right there.
04:28So in the main news one, what I am going to do here is set its width to 44.5%.
04:34Remember, they are both going to be exactly the same.
04:35So I am going to give them both the same width.
04:37And then for padding, well, I want to set padding all the way around, so I am
04:40just going to do 1.75% for the padding.
04:44I also need to set some spacing between these two of them, because I'm not
04:48floating one to left and one to the right.
04:50So I need to do margin-right on these, and the margin between them, if you
04:54remember from the last exercise, is going to be 4%.
04:58And then I have this other selector right down below that that is stripping the
05:02margin-right value from the second news, so we don't get an extra bit of spacing
05:06there on the right-hand side of that.
05:07So if I save this, go back out to the browser and refresh my page, now we get
05:12our interior spacing.
05:13And what I really want you to focus on is take a look at the space between these
05:15two columns, these two elements, and this element. And they're very close.
05:20Now, we had to give them all different percentage values.
05:23This is 2%, this is 4% based on where it's located, and this of course we are
05:27letting the actual columns create that space.
05:30But we are seeing sort of a consistent spacing between them because we took the
05:34time to plan that out and really calculate what those various percentages are
05:38going to be based upon their parent elements.
05:40So there you have it.
05:41It is a basic two-column fluid layout.
05:44Now, your designs are probably, over time, going to be a lot more complex
05:48than this, but as long as you remember the rules behind calculating
05:52percentage values, and especially how it pertains to margins and padding,
05:56you should be fine.
05:57Now, next up, we are going to explore how page assets like images and other
06:01assets can affect fluid layouts and how to make those assets flexible.
Collapse this transcript
Making images flexible
00:00As long as your page content is limited to a content that automatically reflows,
00:05like text, you'll be fine.
00:07However, once you start putting images and other fixed-width resources like
00:11video on your page, you are going to quickly find that your fluid layout
00:14has some limitations.
00:15So to demonstrate that, I have the fluid.htm file opened here from the 06_04
00:22folder. And if I just open this in a browser, you can see we've added an
00:26image to our layout.
00:27And right now everything looks just fine.
00:29It's fitting in there fine. The text is reflowing around it okay.
00:32However, if I begin to resize this image, watch what happens.
00:37Whereas the content and the columns are flexing, this image has a very fixed width.
00:42So at the end of the day, you just end up with this text running right over the top of it.
00:47It's colliding and it's tearing up your layout, and that's going to happen
00:50wherever you use fixed-width images within your fluid or flexible layouts.
00:56There are some things that we can do to help us with this.
00:59So I am going to go back into my code and I am going to scroll down, and let's
01:04take a look at the image, which is about on line 107 or so here.
01:09If I take this image and I change its width from 448 pixels to 100%, let's
01:15see what happens then.
01:16So I am using a percentage value rather than a fixed value for the image width.
01:20I am going to save this, go back to my browser, and refresh.
01:24You'll notice that it did a couple of things.
01:25It distorted the image, so it sort of stretched it, if you will, and it
01:29stretched it to fill the entire column.
01:30So that tells us a couple of things here.
01:33Number one, we don't want to leave the height at a fixed value.
01:37The other thing is that the width, just like when you're calculating the width
01:41of elements and padding and margin, it is relative to its parent element. And
01:46you're not saying 100% of what the normal width of this element would be;
01:49you are actually saying give me 100% of its parent element, which would be the
01:53entire width of the column.
01:54So we need to find what the appropriate percentage is to display this image
01:59in its natural state.
02:00And I've got to tell you something.
02:01That is not easy to do.
02:03You can't just say, "Okay, what's my target resolution? All right!
02:06Let me go ahead and factor that in," because it's not always quite that simple.
02:10Let me show you, though, a very easy way I've found to sort of work through this issue.
02:15So what I am going to do is I am going to revert back to the image the way it
02:18was before, and then I'm going to copy this image, and then I'm going to
02:23paste it right in there.
02:27Now, if I save this, if I preview this, this gives me two of these images
02:30stacked right on top of each other.
02:32And when I am looking at these images, one of the things I'm thinking about
02:35right now is, okay, what percentage of this column width is this image. What
02:39percentage is that taking up?
02:40If you had to guess, what would you say, around 70, 75, 80%, somewhere around in that range?
02:46That's typically where I start out.
02:48I sort of look at that and say, okay, what percentage is that?
02:50So if I flip over here and I take the second image and I get rid of its
02:54height, I am going to strip the height out, and I take its width value and I change that,
02:58so let's say I changed it to 70%, and if I save this, and then preview it, you
03:05can see the flexible image now is a little bit smaller. So it's close there, but
03:09it's not quite on the money.
03:11So if I go back in, I can start manipulating this value a little bit.
03:14If I go up to 78% and save it and preview it, I can see that it's a little bit larger.
03:20Now, one thing that you really want to try to avoid is you want to try to avoid
03:23scaling the image up.
03:25That's going to damage the image quality, and it's going to cause some issues.
03:28So if you can find a happy medium, then that's fine.
03:32And if I go back here, let's try 74%, save that, preview it, and that's really close.
03:40I mean, you are talking about a couple of pixels off. And since this image is
03:43going to be fluid anyway, that's not a bad place to start from.
03:47I am going to go back over and get rid of my fixed image and just sort of leave
03:54this one where the other one used to be.
03:56So I stripped the height value out.
03:58I gave it a width of 74%.
03:58If I save this now and preview it, I am left with my flexible image. And
04:04now you'll notice if I resize the browser, the image resizes with it, which is really cool.
04:09There are some downsides to this.
04:12Performance in mobile devices can be negatively impacted by the browser having
04:16to constantly scale images.
04:18You also have to think about image quality itself.
04:20The image quality can suffer.
04:22It's not suffering quite as much if you scale down instead of up.
04:24And notice if I had a really larger monitor than this and I kept expanding, I
04:29would actually be scaling up.
04:30So a good rule of thumb is to use maybe a slightly larger image and make sure
04:35that you're always sort of scaling it down a little bit, so that you're not
04:37losing a lot of image quality.
04:40There are other ways to make images or elements responsive as well.
04:43Now, the same technique that we just did here would also work on video, and you
04:47definitely want to make sure not to leave the height value in there for those
04:50video objects either, because you definitely want to make sure the aspect ratio
04:53remains the same on those.
04:54But you can do that for video. You can do it for canvas.
04:57You can do it for all sorts of assets that you might be placing here on the
05:00page if they need to be fluid as well.
05:03But there is another method that we can use that specifically targets images, and
05:08that's working with background graphics.
05:10So if I go back into our code and I scroll down, I can see that on 110 I have an
05:15empty div tag here with the class of banner.
05:17Now, typically I wouldn't use an empty div tag.
05:19I mean that's non-semantic markup, but if I'm wanting an image that's just
05:23purely decorative in nature and I want the image to be able to flex with the
05:26layout, then sometimes using this technique is actually a pretty nice solution.
05:30So I am going to scroll up to my CSS and we are going to see how we are going to use this.
05:35We have an empty banner selector already up there.
05:38So what I'm going to do is I am going to give it a height of 250 pixels.
05:43Now, how do I know the height?
05:44Well, the image that I am going to be using here within this banner, I know it's
05:47250 pixels tall, so I kind of have a little leg up on this.
05:51Speaking of that image, I am going to go ahead and do a background image here,
05:55and I am going to give it a url of _images/philly_banner.jpg. And then I am
06:05going to go ahead and just tell that no- repeat, so that it doesn't tile, in case
06:09the container stretches out wider or taller than the actual image itself.
06:13The next thing I am going to do is set some margins on this.
06:15I am going to do a margin of 2% for top and bottom--
06:18that will keep the text away from the top and bottom of the image--and then
06:210 from side to side.
06:23So if I save this and then preview this in the browser--let me just refresh this page--
06:29if I scroll down, now, here's my image coming in.
06:32Now, remember, I didn't give this a width at all.
06:35Knowing that the behavior of a div tag, for example, is to stretch to fill
06:39its parent container,
06:40I knew that by default that div tag is just going to expand to fill the
06:44column, so that's fine.
06:45I now have an image that stretches the entire width of the column.
06:48The height is determined by the image itself.
06:50And again, because we didn't specify a width, you'll notice that as we resize
06:53the browser, the div tag resizes as well.
06:56However, the image itself doesn't resize.
06:59Essentially what we're doing is we are almost cropping off a portion of that
07:02image as we resize this; it's sort of sliding.
07:05So when you're doing something like this, number one, it has to be a decorative image,
07:08and number two, you have to realize that at certain resolutions you are going to
07:12be losing part of that image.
07:13That way if I was creating an image where something had to be seen, I would make
07:17sure that that subject matter was on the left-hand side here rather than on the
07:20right-hand side, because I know that that has a chance of being hidden based
07:25upon the size of the screen.
07:27If you're going to be integrating images into your fluid layouts, you really have a
07:31lot of factors that you need to sit down and think through as you are
07:34planning your layout.
07:35You need to consider how scaling images might affect image quality, what type of
07:40performance hit you are going to take within the browser, especially for mobile
07:43devices, whether or not you can effectively control content reflow.
07:47So if you're making it flexible and the content is reflowing around it, what
07:51happens if a headline above this suddenly comes down below this image? Do I need
07:55to clear those within the float, that sort of thing.
07:58So in our next movie we are going to explore how we can use minimum- and maximum-
08:03width properties to give us greater control over not just image assets, but all
08:07parts of our fluid layouts.
Collapse this transcript
Setting minimum and maximum widths
00:00On every flexible layout, there comes a point at which the layout just no longer works.
00:05You know at the small enough size, columns become too small to hold the content,
00:09and the relationship between the elements is lost as they begin to collide and
00:13overlap each other.
00:14At larger widths, layouts are often not as effective either, because column
00:18widths can increase to a size that affects readability, and images and other
00:22assets begin to move away from the elements that they represent.
00:26So we have a couple of values, the minimum- and maximum-width properties, and
00:29those properties allow you to set a range of values for layouts that can
00:33still give you the flexibility you want, while ensuring that the layout stays
00:36within an effective size.
00:38So to demonstrate kind of how layouts fail at various ranges, I've got the
00:42fluid.htm file open from the 06_05 directory.
00:48If I open this in a browser, you can see it's pretty much the same layout
00:51we were working with.
00:52We do have a couple of extra elements down here that we are going to be talking
00:55about in a little bit, and a new image that's been added to the page,
00:58so we are going to have to account for that in just a moment.
01:01But at this maximum size, it doesn't look too bad.
01:03Of course, the monitor I am on is a 1280-pixel-wide monitor.
01:07If I was on something at 440, this might stretch even more, and this column would
01:11be a little bit difficult to read.
01:13If I begin to resize this, at a certain size, it just becomes kind of hard to
01:18read, just because the column widths are so small.
01:21But then, when it gets down to this size, it really and truly fails.
01:24You will notice it won't go any further than that size.
01:27And that's not because there's already a minimum width on it.
01:30If I scroll down, you can see that this image down here is just not allowed to go any further.
01:37So it's the fixed nature of that image that's stopping that scrolling.
01:40And this, obviously, if we were looking at this layout, even on really positive
01:45terms, it's a failure. It's really a failure by anybody's measure at that size.
01:48Now you might say to yourself, well, yeah, but who is going to view at that size.
01:52Well, phones, for example.
01:53So let's go ahead and examine the minimum-width and maximum-width properties and
01:58how they can help our layout.
01:59I am back in the code, and I am going to scroll down to my body selector.
02:05In my body selector, right now, the width is 80%, and that's pretty open ended.
02:09It's just like, hey, whatever the available space is, give me 80% of that.
02:13But what we can do is just below that we can set a max-width, so maximum-width property.
02:20And we can say okay, don't go beyond 1200 pixels.
02:24And then we can give it a minimum width, say min-width, and I can say hey, don't
02:28go below 480 pixels.
02:31So that it is, in essence, establishing a range.
02:34It's like take up 80% of the available space,, as long as you never go wider than
02:381200 pixels and narrower than 480 pixels.
02:42So how do you determine that range?
02:44Well, if I save this file, go back into my browser, and refresh it, you can see,
02:50especially at maximum size, it doesn't really have any affect to me there,
02:53because right now I am not even viewing it at a thousand pixels wide, but if I
02:57go down, that's 480.
02:59Is that enough, is it too much?
03:02No, layout is still failing.
03:04So one of the things that you'll need to do when you're doing things like this
03:08is to start to flex your layout and find those points of failures.
03:12And one of the things that I'll typically do when I do that is I'll take a
03:14screenshot and I'll just measure that.
03:17Now, there are plenty of tools that you can use to do this, like Snapz Pro, for
03:21example, when you take a screenshot. You can do a selection and it will actually
03:24give you the physical measurements.
03:26There are measurement tools that you can do to measure screens.
03:28There are JavaScript plug-ins you can get for your browsers that will let you
03:31know what the available browser space is.
03:34So there are a lot of different ways to do that.
03:35So I am going to go back into my code and I am going to modify this.
03:39So, after taking some screenshots and after looking at some things, I decided
03:42that I am going to restrict this between 1024 and 640.
03:47And now if I save this, go back in, and refresh it,
03:52now its maximum size is 1024, which we're still not really bumping into at this resolution.
03:58But as we go down, you can notice it freezes right there at 640 pixels.
04:04So we still have a readable right column.
04:06We still have a readable left column.
04:07As I scroll down, this image is still failing,
04:10so we are going to have to deal with that.
04:11But that has to do more to the fixed nature of that image than it has to do
04:15with the minimum or maximum range.
04:18So in this case, I am just saying, okay, can I still read these, do they
04:20still look okay, how is the column width? And at about 640 pixels here, we
04:26are at a fairly decent space, so you can still sort of read this, so I am happy with this.
04:31Maximum width can really help us out with these images as well.
04:34Now, one of the things that I wanted you to focus on first about this image is
04:37how it fits within its container box.
04:40You'll notice that there is nothing to the left of it.
04:42There is nothing to the right of it.
04:43We are not reflowing any content.
04:45I want this image to scale to fit the width of its parent element, which in this
04:48case is the new section.
04:50We could go into the code and set the image's width to 100%, but the problem
04:56there is that if I can have this image display at its native width and height, I
05:03would love to do that.
05:04That way I don't having any scaling involved at all.
05:06Well, there is a way to do that.
05:07So I am going to go back into my code and I am going to scroll down to the
05:12image, which is down here towards the bottom.
05:14It's in this news section, and it's under the Featured images section.
05:18So what I am going to do is I am just going to strip out width and height altogether.
05:20I am not going to set width to 100%.
05:21I am not going to set it to any percentage point at all.
05:24I am just going to get rid of it.
05:26Now if you leave off the width and the height, the HTML specification tells the
05:30browser to go ahead and use the native width and height of the image.
05:33So if no width and height is provided, the browser basically calculates it based
05:37on the image properties and then displays that.
05:39I am going to modify that just a little bit by coming in here and applying a class
05:44to this image, and the class that I am going to give this is flex.
05:47All right, so I am going to save that, and then I am going to scroll up into my
05:51CSS, and just underneath banner, I am going to write a selector for the class of flex.
05:57And in flex, I am going to use something very simple:
05:59I am going to type in max-width 100%.
06:03So what does that do?
06:04Well, it allows the image to display at its native width and height if it can.
06:08But it's also saying, however, your maximum that you can be is 100%.
06:15So the browser is going to calculate the width and height of the image based
06:18upon the available space, its native width and height, and whether or not it
06:23fits in within the element.
06:24So if I save this and preview it, you can see that even though the image
06:29naturally is larger than this, its maximum width is being set to 100%, meaning
06:33it can't be any wider than this particular element.
06:36So now we have an image that flexes within that space, but still when it can,
06:42will display at its normal or native resolution, which is a really neat solution
06:47to creating these flexible images within those container elements.
06:51So essentially here, our fluid layout is free to react to whatever screen
06:55size it's viewed in, while still being constrained within a range that
06:59enforces our layout's viability.
07:01Now we have also integrated images into our layout in a way that maintains
07:04the relationship between images and other page elements.
07:08Planning and executing flexible layouts requires a lot more effort than fixed layouts.
07:12I mean that's just a fact of life.
07:14However, the extra work gives you a layout that is not constrained to a single
07:18resolution, and it allows you to be a little bit more creative with how elements
07:22interact with each other.
Collapse this transcript
Building flexible layouts: Lab
00:00Okay, it is lab time.
00:02Now, for the flexible layouts lab, I'm not going to make you go through the
00:06whole planning process for the desolve.org site.
00:10That would be just cruel.
00:11Rather, I am going to give you the appropriate values for the main sections, and
00:16then let you go through the layout to determine which elements to style, to
00:19achieve the desired width and spacing.
00:21You might think I am being nice to you there, but probably not.
00:24Now you'll also be determining an appropriate overall size range for the layout.
00:29You are going to be styling some interior elements and you are going to be on your own there.
00:32And then you are going to be fixing an issue with some of the images on
00:35the homepage as well.
00:36So to starting working on a lab, go into the 06_06 directory, and open up
00:42index.htm and the main.css file, which is found in the _css folder of the 06_06. Okay.
00:51Now, I just want to go into the browser really quickly.
00:53I am going to preview where you are going to start versus where you are going to finish.
00:57So this is the starting layout.
00:58You can see our columns aren't working properly.
01:01We don't even have spacing going on there.
01:03It does flex, but it's not exactly what we think of, when we think of a flexible layout.
01:07Now this finished version, after you are done with it, it's going to look like this.
01:10We are going to be able flex the layout, but you need to set a defined range for
01:14it, so at a certain point layout is going to stop flexing and we are also going
01:17to have to deal with these two elements right here.
01:21You are going to have to come up with the appropriate measurements to get them
01:24to space properly and still flex, and then for these images in the sidebar, you
01:28are going to have make those flexible as well, but you want them to be able to
01:32display it at a native resolution if they can.
01:35So if I go back into the styles, you can see that the main.css that I have
01:39given you all of your labs steps, but right here some information you are going to need.
01:44You have got Left document padding, you have got the Main column Width for the
01:47left column, you got the Right column Width for the right column and you have
01:51the Right document padding on that side.
01:53So, here are steps.
01:55The first thing you are going to do is go to your Basic Layout styles at about line 156.
01:59You are going to set the body width to 90% and then find an appropriate range
02:05for minimum and maximum page width, so that's going to be left up to your own
02:09discretion, and it's going to be based on number of factors, but remember, you
02:12can take some screenshots, you can take measurements of the screening, and you
02:15can kind of figure out where the layout fails once you get everything set up
02:18and help plan you range from there, so, that's actually something in terms of
02:22the minimum and maximum page width, that you might want to do once everything
02:25else is finished, so you can really determine where you still have problems within the layout.
02:30And using the values that we have right up here, you again, want to go down into
02:34the Basic Layout styles, find the selectors for the article, in the aside
02:39selectors, find those and then set the appropriate width and padding values for
02:45both of those elements.
02:47After you are done with that, go into the Content Region styles, which you can
02:50find on about line 184.
02:53Now you are going to have to go and find the selectors that control the header,
02:56main navigation and banner regions, and then you are going to determine what
03:01property you need to modify and what value you need to set, so that it's content
03:05lines up with the rest of the page content.
03:07For the two news selectors which are on line 347, those are the two that are
03:12side by side, you need to determine an appropriate width padding and margin to
03:17get those elements to display side-by- side within acceptable amount of padding
03:21and spacing between the elements.
03:23Now there is a lot of judgment call that's going be going on there.
03:25So your values might not match with my finished values, and that's okay.
03:29So as long as you are comfortable with the spacing of it, that's fine.
03:32Now for the contest image selector which is found on line 635 in the css, you
03:37are going to need to add a property that allows the images to display at
03:40their normal size if that's available, and then still scaling it, if the
03:44screen size is reduced.
03:46Now, I said, you may, but that's a big hand that you are going to have to.
03:48You are going to have to go in and modify the image tags directly in the
03:52index.htm file as well.
03:54So if you go into the code and you scroll down, you are going to go into the
04:01aside and you are going to find those images right here, so about line 182 and line 77, okay.
04:09So I'm betting to that, it's probably enough to keep you guys busy for a while.
04:13Now for this lab, you may need to go back to some of the previous movies and
04:17refresh your memory about a few of the techniques that we covered. That's fine.
04:20You'll also need to make a few educated guesses and probably employ a little
04:24trial and error on a few of the elements.
04:26And to be honest, I did that to you on purpose.
04:29Now usually when you're working with fluid layouts, especially really complex
04:32ones, your initial planning works fine up to a point.
04:36Then there are always places within the layout that you are just going to have
04:39to test and tweak in order to get the layout looking the way you wanted to.
04:44And don't forget, once you've finished to watching the solutions movie that
04:47follows this one, to compare your code to mine, and hear my take on how I solved
04:52some of these problems.
Collapse this transcript
Building flexible layouts: Solution
00:00Welcome back! I hope you had fun working on the flexible version of the desolve.org layout.
00:05Now, flexible layouts are difficult, especially the more complex your pages get,
00:09so don't feel bad if a few of the steps didn't work out the way you wanted them to.
00:12Let's go through our finished code together, and then we can compare the results.
00:16So I have the index and the main.css files open,
00:19so index.htm, main.css, and you can find these in the finished_files directory
00:24of 06_06. And of course the CSS one is in its own little directory there, _css. Okay.
00:31So I am just going to start with the steps and work my way down.
00:33I'm first going to scroll down in the finished_code to about line 167 or so.
00:39I gave the body a width of 90%, and then I gave it a maximum width of 1180 and a
00:44minimum width of 860.
00:45I am guessing your values probably differ from mine a little bit.
00:50That's one of the things about setting a minimum- and a maximum-width value.
00:53Generally, a lot of times, it just goes down through personal preference.
00:57But I did find that below 860 my layout broke, for a number of different reasons.
01:02I am going to switch over to the browser here and I notice that once I started
01:06getting below this value, the navigation really started to run into each other.
01:10You can see that the centering of the navigation is still a little off right
01:14here, but it got even more off the further down I went.
01:16So at about 860 pixels, I decided to sort of freeze it at that point. All right!
01:21Now going back into the code, I am going to scroll down a little bit down to the
01:24article and the aside.
01:26I noticed that the widths are set to the values that I gave you up in the
01:29comments, 70.1 for the width for the article, 3.9% padding for both of them.
01:35The article of course is padding to left, because it's the left-hand column; and
01:38the aside has padding placed on the right, because it's the right-hand column.
01:41Remember, we don't really need to specify any space between the two of them
01:44because we're letting the layout itself sort of form that space.
01:48So that's something that we don't need to do.
01:49Now, I also told you to go down and in the mainNav banner and header elements,
01:54go ahead and line up their content with the content of these columns.
01:58And if I scroll down to line 199, you can see for the header, all I had to do
02:02was place padding-left of 3.9%, because they're all top-level elements.
02:06Same thing for mainNav. About line 237 or so,
02:09you can see that has a padding-left of 3.9%. And then the banner selector,
02:14which is a little bit further down here, around 295 or so, it has a
02:17padding-left of 3.9%.
02:19So because these were all top-level elements, meaning they're sitting right
02:22inside the body tag, I didn't need to do any further calculations on them.
02:26I could just go ahead and use the exact same padding values that the columns
02:30and cells were using.
02:31Had they been nested inside of them, I would have had to recalculate those values.
02:35Now, speaking of that, those two news items, if I go back into the browser
02:39of course, these two news items right here that are floating side by side each other,
02:43you were tasked also to find out the padding values, the margin values between
02:48them, and the width of them, again so that you can arrive at sort of equal
02:52spacing between all of those elements.
02:53Now, this is totally a judgment call.
02:55You probably did a lot of tweaking, and your values might not necessarily match
02:59mine, and that's okay as long as you're happy with the results.
03:02So if I scroll down, I can find, on line 360, the news selector for this.
03:07Now, you'll notice that for both of these, I used a width of 44%, and I used a
03:11padding on both sides of 2%, on the left- hand side of 2%, the right-hand side of 2%.
03:16Add up to a total width for both these elements of 48%, which of course is
03:20going to leave about 4% space between the two of them, which is why the
03:22margin-right is set to 4%.
03:24Now, it doesn't matter that your values match mine precisely.
03:28You could use some decimal points.
03:29Maybe you did 45% for the width, or 48%.
03:33It doesn't really matter. As long as you're comfortable with the spacing and you
03:36like the way that it looks, that's fine!
03:38What's important here, however, is that all those values add up to 100%, so that
03:43you're taking up all the available space within the column.
03:46Now, the contest images, the last task that I gave you, to make sure those images
03:51displayed at their natural size if they were able to and then scale down if the
03:55browser was scaled down,
03:56that was a little trickier.
03:58I played a little trick on you there.
03:59That's a little harder than it seems, because in the previous exercise where we
04:05did that, remember we set the maximum width to 100%.
04:08But the maximum width there, in order for it to be that size, is not 100%.
04:12Let me show you what I mean.
04:14If I go down to, all the way down, let's go down to about line 650 here in our
04:20code, I can find content image.
04:22Now, here is how I solved it.
04:23For these content images, I placed their maximum width at 82%. All right!
04:30How many people got 82%? Show of hands. A couple of people. Anybody, 82%?
04:33So how in the world did I get 82% and I didn't just give it 100%?
04:37Well remember, 100% would have made it fill the entire right-hand column, which
04:41is going to scale it up, and it's not the way I want that to look.
04:44So I placed two images side by side just like I did in the previous fluid images
04:49exercise, and I began playing around with the width until I got a width value
04:54that was equal to the 200 pixels of width that the image actually was, and that
04:58turns out to be 82%.
05:00So I am saying, okay, you can be your normal width, but any other time I want
05:04you to be 82% of that column, and that way it's going to maintain that
05:08sizing-spacing relationship with all the other elements.
05:11Now, of course I also had to modify the code, so if I went back to index and
05:15took a look at these images, you can see that I got rid of the height property.
05:18Now I left the width property in here. Some of you probably stripped it out.
05:21If you did, that's fine. That's no problem.
05:23So essentially, what this is doing now is it's telling the browser, hey!
05:26I need to be 200 pixels wide. And then the CSS is overwriting that, and saying
05:30okay, yeah, 200 pixels is fine as long as you're not above 82% of the column
05:37width. And that's how that's working.
05:38So it will appear at 200 pixels wide if it can.
05:42It's going to calculate its height based on the aspect ratio, and that it will
05:45never be wider than 82% of the column.
05:47So now, when we scale this, the images display at their natural size until
05:53you start scaling it down, and then they maintain that relationship at 82% the entire time.
06:00Okay. Of all the labs I've given you, I'll be honest with you,
06:03this one took the most time for me to actually put together.
06:07To convert the fixed version of the deslove.org site that I was working with
06:11before this to the fluid versions, I had to make significant changes to the
06:15overall layout, and in more than just one area.
06:18So if you want to take this lab a little bit further, if you really want to dig
06:21it apart, compare this layout to the finished layout in our floats lab, which is
06:26the previous lab where we used the fixed version of this layout.
06:29See if you can identify all of the formatting changes from the fixed layout to
06:33this one, and there a lot of them, and think about why they were necessary in order
06:38for this fluid layout to work.
06:39Now, that should give you some insight into some of the issues that you'll need
06:42to think about as you begin to create fluid layouts in your own work.
Collapse this transcript
7. Building Responsive Layouts
Responsive layout overview
00:00If there's one thing you need to embrace to become a successful web designer
00:04it's that the web is a constantly evolving place, and you're going to need to
00:08evolve right along with it.
00:09While a few years ago, we were arguing over the merits of fixed layouts versus
00:13flexible layouts, a very odd thing happened.
00:17People began using the web in a way that made this argument moot.
00:20Now over the past few years, the explosion of smartphones and tablets means that
00:25your content is now consumed by a wider array of screen sizes, resolutions, and
00:31browser types than ever before.
00:33As a designer, that means you have to decide whether you're going to target one
00:37view over any other possible views, design a flexible layout that has to
00:41have the ability to grow from one extreme to another, or whether you are going
00:44to build a layout that is responsive.
00:46So what's a responsible layout?
00:48Well, responsive layouts do just that;
00:50they respond to the environment in which they're viewed.
00:54On desktops, the design could be enhanced for the larger screen, with increased
00:57graphics size and layouts that target wider aspect ratios.
01:02On a tablet, they could change layout and functionality based on the device type
01:07or the orientation of the device.
01:10On smartphones, they could again change layout based on smaller size, hide some
01:15elements, and even take advantage of smartphone features like geolocation and
01:19messaging. And if you're wondering, all three of the layouts that I just showed
01:22you are the exact same page, just viewed at different sizes.
01:27Now if it sounds like I'm describing multiple layouts, you're not too far from the truth.
01:31Now responsive layouts typically use a combination of layout techniques and
01:35multiple layout types to serve the proper layout to the proper screen or device.
01:39Now with that in mind, here are few pros and cons to consider when thinking
01:42about using responsible layout.
01:43Now the biggest reason to use them of course is that it allows you to control
01:48how users experience your content, based on the device or screen size.
01:53This allows you to create unique experiences for every device type or screen
01:56size that consumes your content.
01:58You can also use this as a way of reorganizing or even eliminating content
02:03based on the context.
02:04Mobile users can experience a site that's tailored for the mobile experience,
02:08while desktop users can interact with a site designed around that context.
02:12It also means that in many cases you don't have to create separate sites for
02:16mobile devices or redirect them to mobile-specific pages.
02:20Now the downside to responsive layouts is that they require much more work, in
02:25terms of planning and executing the designs.
02:27You are essentially designing around multiple contexts all at the same time, and
02:31that requires you to be much more rigorous about how you build your site.
02:36Failure to plan one aspect properly can cascade into having to rework multiple
02:40areas of your site, as they are all tied into one another.
02:43You also have to rely on newer features. So in the short term you are going
02:46to need to expend a great deal of energy into providing fallback features
02:50for browsers or devices that don't have the support needed for your responsive layout.
02:55Also, to take advantage of things like certain mobile-specific features and to
02:58adequately serve alternate content, you're going to need to go beyond just using
03:02CSS into at least moderate JavaScripting and using JavaScript libraries like
03:07modernizer that are designed to help you create responsive designs.
03:11Now my own personal feelings are that as designers, if we don't design for all
03:15the ways that people can consume or interact with our content, we are failing to
03:19address a significant amount of our audience.
03:22In terms of smartphones and tablet, I feel that we've reached a point where
03:26those audiences are just too big to ignore anymore.
03:29So even if you want to take things slowly and not try to master responsive
03:32layouts right off the bat, at least understand the concepts and how the
03:36techniques of fixed and fluid layouts are used to help create more
03:39responsive designs.
03:41In a few years, my guess is we are going to be looking at layouts that aren't
03:44responsive, and wondering, how in the world we ever got away with it.
Collapse this transcript
Using media queries
00:00Media queries allow you to determine which styles are applied to pages based on
00:04specific media properties, such as screen width, color, or resolution.
00:08Now, if you're familiar with using the media attribute to apply styles for
00:12screen devices, print, projection, that sort of thing, then media queries are
00:15going to feel very natural to you, because they're just an extension of
00:19existing media capabilities.
00:21So to kind of show you what media queries are capable of, I have the
00:24media-queries.htm file open from the 07_02 folder. And I want to preview this
00:30page in the browser real quick, so we can do a quick rundown of the syntax
00:35before we actually start using them.
00:37So the basic syntax you can see in the first block of code here, basically after
00:41whatever media declaration you are doing, you can put before it, you can put two
00:44keywords, optional keywords: not or only.
00:46Not will negate everything that comes after it, so it's sort of testing to see
00:50what isn't true, and then only is designed basically just to hide the media
00:55query from any older user agents that don't support them.
00:58Then after that, you have the media type, like you would normally, so
01:01screen, projection, print, all of those that you are probably pretty
01:04familiar with using.
01:05And then after that, we have an optional and keyword, which is followed by an
01:09expression, and that's where the real power of media queries comes in.
01:12I am going to scroll down just a little bit.
01:14You can see, for example, in the second code block we have a sample media
01:18query, and this one is saying, for only screen devices that have a minimum
01:22width of 720 pixels,
01:24so 720 pixels and above, go ahead and apply these styles. So we are basically
01:29focusing exactly when and where our styles are applied.
01:31Now, below that I have a listing of all the different media features that you
01:34can test for, things like width, height, device-width, and device-height,
01:38orientation, device-aspect-ratio, color, so there's a lot of things that we can
01:43test for when we're choosing when to apply our styles or not.
01:48Anything that's a length value, like width or height, you can also expand what
01:52you are doing by adding a minimum and a maximum prefix to it. All right!
01:55I am going to go back into our code, and we are going to experiment with media
01:58queries by writing one of our own.
02:00So I am going to scroll down. And because we are using embedded styles and we
02:04are not linking out to an external style sheet,
02:06we are going to use a different form of syntax than what you saw there
02:09previewed on the page.
02:10We are going to use what's known as a @media block.
02:13So I am going to do @media. Then I am going to open up a curly brace and then have
02:17the closing curly brace.
02:18Now, essentially, this is a way of grouping styles within a larger style sheet.
02:23So anything that goes inside that media block is governed by the media query
02:27that's going to follow this.
02:28So right after @media, I am going to go ahead and do my media query.
02:31I am going to type in only screen, and then in parentheses max-width, and then
02:40a colon, of 920 pixels, so you don't need a semicolon after that, just the 920 pixels.
02:46So this is saying that, hey, for just screen devices, any screen device that
02:50has up to a maximum width of 920 pixels, I want you to apply these styles that follow.
02:55So any styles in those curly braces that follow that are going to be applied if
02:59they meet this criteria.
03:00So we are basically saying 920 and below is when this is going to apply.
03:04So we are going to go down into our @media block, and this is where it might seem
03:08a little bit strange if you hadn't seen the syntax before. Inside this I can
03:11just start writing additional rules.
03:13So I am going to type in body, and we will do width of 90%.
03:19So I am going to go ahead and save that.
03:20Now, I know that looks a little weird to have a closing curly brace here and a
03:24closing curly brace here, but you've have got to remember that this is a big
03:27containing media block. And you want to be very careful when you're writing
03:31these, because you don't want to leave off a curly brace.
03:33That's one of the reasons why, when I created that media block,
03:36I went ahead and created the curly braces first before I begin authoring any
03:39styles inside of it.
03:40So I have gone ahead and saved that.
03:42I am going to go back out to my browser and refresh.
03:44Now, right off the bat, we don't notice any difference, because currently my
03:47screen is above 920 pixels, but notice that as I begin to resize it, when I hit
03:51that trigger point, you can see when I do it.
03:53As soon as I hit that trigger point, the body width changes and I have a
03:56flexible layout below a certain screen size, so you can see exactly when that
04:01trigger is happening.
04:03And what's nice about this is as our screen size gets smaller, the layout is
04:06going to respond to it, but of course it still breaks at a smaller size.
04:09That's one of the great things about media queries.
04:11You can have media queries that target multiple conditions.
04:14Let's go back in and modify our existing media query and then write another one
04:17so that we are targeting three totally different situations.
04:20So I am going to go back into my code. And the existing media query, I am going
04:24to add some constraints to this.
04:26So essentially, right after the first and, I am going type in min-width this
04:31time. So not max-width, but you can also do min-width.
04:33Now, I am going to do a min-width of 600 pixels, and then between those two I am
04:38going to do another and. So you can just chain as many of these as you would like.
04:42So essentially what we are doing here is we are setting a range.
04:44We are saying, hey, anytime that the screen value is between 600 pixels and 920
04:50pixels, make the body width 90%.
04:52That means that when this condition isn't true the default styles will go ahead and apply.
04:57So I am just going to do @media, and I am going to type in only screen and
05:04max-width of 599 pixels.
05:10And if you look at the media query that we have above this, it's pretty obvious
05:14why I'm doing that, because as soon as the last condition has been met, which is
05:18min-width of 600 pixels, 599 pixels picks it up from that exact moment.
05:22So we are creating a media query that's going to be active the moment the one
05:26above it is not, in terms of screen size.
05:28And then inside that I'm going to do a body selector, and the body selector, I
05:33am going to go ahead and set a width on that, and I am going to do a width of 320 pixels.
05:38I also want to do one more selector, and that's going to be a pre selector. The
05:42pre is the element that's representing our code snippets on our page.
05:45And so for that I am going to take the font-size and I am going to reduce the
05:49font-size to .8ems, so I am going to make it a little bit smaller.
05:51I am going to give it some padding, so I am going to apply a padding of .5ems to it.
05:57And then I want to change the background color, just because we can.
06:00And I am going to do rgb(246, 247, 160), and don't forget your semicolon there. All right!
06:10So I am going to save this, go back out to my browser, and refresh, and now we
06:15have three different conditions.
06:17When the browser is above 920 pixels, we see the page in its default state.
06:22As we begin to scroll down a little bit, we get a flexible layout.
06:25And then when we hit the last trigger point, the screen size changes again and
06:29fixes again, and then our preformatted code also changes, in terms of the
06:34background color that's being used, the font-size, the padding, so all those
06:37things kind of change.
06:39Now, I will honest with you.
06:40These really aren't very well-thought-out media queries.
06:42I was being a lot more reactive to what I was seeing as I was styling rather
06:46than really thinking through and planning my styles the way I should be.
06:50Media queries are incredibly powerful, but if you don't have proper
06:54organization, your code can get really sloppy very quickly, and that makes it
06:59hard to maintain, and it's going to have a lot of unintended side effects when
07:02you have conflicts between media queries and other styles,
07:05so you have to be very, very careful about that.
07:07Now, to deal with that you are going to have to carefully plan and organize your
07:10styles when using media queries, and that is something that we are going to
07:13discuss in our next movie.
Collapse this transcript
Organizing styles
00:00Media queries are amazing. The ability to totally change your layout based on
00:05factors like screen size or orientation really opens up a lot of possibilities
00:10for us as designers.
00:11However, because of their selective nature in how they apply styles, media
00:16queries can create a huge headache for you if you don't properly plan and
00:20organize your styles.
00:22In this movie we will explore organizing your media queries in a way that allows
00:25you to write the most efficient styles possible and prevents styling conflicts
00:29when media queries are triggered.
00:31So I have the organizing.htm file open here, and for all intents and
00:34purposes, it's pretty much exactly the same file that we were working with in our last exercise.
00:40However, there have been some slight changes to the styles. And if I scroll down
00:45a little bit into the styles, I can see that I have a grouping of styles down
00:47here now that say "styles that change."
00:50So essentially, one of most important parts of planning media queries is to
00:54identify what I call global styles versus styles that change.
00:58Now, global styles--I am going to scroll up and we have our global styles right up here--
01:02these are styles that are consistent regardless of which media query is being
01:07triggered or which condition is currently present.
01:10And by identifying those styles at the very beginning of your planning process,
01:15you can group those styles together, keep them out of the media queries.
01:19This will help prevent conflicts, style conflicts from one media query to the other.
01:23It's also going to make your styles a little bit more efficient and a little
01:25bit more lightweight.
01:26So what I am going to do is right above the opening style tag, I am just going
01:30to go ahead and start a comment, and I am going to type in global styles and
01:34then sort of close that comment there.
01:37Now, there are two ways of approaching dealing with styles that change, and we
01:41are going to kind of tackle both of them.
01:43One would be to split selectors up.
01:45You can see here, for example, that from the body tag, I have pulled out the
01:48width value, because the width is going to change based upon which media query is active.
01:53The rest of the properties for the body tag doesn't change,
01:56so I've placed them in the global styles.
01:58Now, that's one way of doing it.
02:00The downside of organizing your style this way of course is that you have
02:03multiple selecto