- View Offline
- 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
Skill Level Beginner
For the most part, you'll find that planning a flexible layout is almost exactly the same as planning a fixed layout. Now, the biggest difference, and honestly one that adds just another entire layer of difficulty, is calculating the percentage values that you'll need for your elements. Now, rather than setting a fixed-pixel values for widths, margins, and padding you'll need to determine the proper exchange values for the desired layout. Because of the way percentage values work, this can often be a little tricky, so I wanted to take some time to go over it here.
Now, again I have a document open. I've got fluid_planning.ai, which is an Illustrator file opened up in Illustrator. If you want to do this a little along with me, feel free to grab another piece of sketch paper, or you can open this up in Illustrator if you have Illustrator. It doesn't really matter. What's the most important thing here is that we're focusing on how to properly calculate percentage values for elements within your layout. When I use the term fluid and flexible, it means that the web site is responding to the size of the browser change. So, really they're the same thing. I don't mean to cause any confusion, but I find myself sort of using those terms interchangeably, and I felt like I needed to go ahead and define them for you.
Just like our fixed layout, we've got a two-column layout going on here. This time I based the grid off of 10 pixels, just again, makes a little bit easier for us to do the math. And I want to go over the steps that I have over here on the left-hand side of the page, steps 1, 2, 3, and 4, and I want you to compare them to what we were doing in the previous chapter with fixed layouts, in terms of planning. First, just like with fixed, go ahead and define a target resolution and a grid. Now, that sounds kind of odd because you're like, wait a minute, if I'm doing a fluid layout why am I establishing a target resolution? Well, essentially what you're doing when you establish a target resolution with a fluid layout is you're saying okay, this would be the "ideal size" for this, and what that's going to allow you to do is it's just going to help you pick out values for padding and margins and things like that.
If you say okay, I'm targeting an ideal size for this at 960 pixels, for example, you can say I want my padding to be 20 pixels or 40 pixels, and then it's going to help you calculate what percentage of your overall width 20 or 40 pixels is going to be. To make calculating percentages easier, you mock up that layout at an ideal size, just as we mentioned here with the target resolution. So we're going to go ahead and mock that up just like we have here with two columns and we've got some spacing in here. Then we use our ideal measurements to calculate percentage values. Now, what does that mean? Well, I'm going to turn on the measurements and show you.
In this sense, we were targeting 960 pixels' worth of resolution. We were planning this as if we were doing a fixed layout. I've got 590 pixels total width for the left-hand column, 350 pixels of total width for the right-hand column, and then 20 pixels' worth of space between them. That means that we have got 20 pixels' worth of padding over here on this side, 20 pixels' worth of padding on this side and then 330 pixels of width, 570 pixels of content width for those columns. So, we're sort of creating those numbers first as a starting point for our fluid layout.
The most important thing that we're going to cover in this entire lesson is point number 4. Remember that margins and padding calculate their values as a percentage of the parent element. We'll talk about what that means as we go through the planning stages. Okay, so the first thing that I do is when I have these sort of top global values, I come up with just whatever the percentages of those are going to be. I'm going to bring up the calculator. This is pretty easy to do. I'm just going to say okay, 590 pixels divided by 960 pixels gives me .614, so forth and so on, so around 62% if you will.
Now, browsers can handle decimal points just fine and if you look in certain people's layouts when they do fluid layouts, you'll find percentage values that go out to the 100ths or 1000ths or 10,000ths of a point. It's up to you to decide kind of how precise you want to get with this. In this case just to keep things simple, we're going to round our values up. Same thing for the 20 pixels. So if I take 20 pixels divide that by 960, I get around 2%. If I take 350 pixels and divide that by 960, I get around 36%. And again, I can round these values if I just want to make things a little bit simple for us, because remember, we're not expecting this to be a specific size.
We're more interested in what the proper percentages is to get this ratio. All right! So, I'm going to go back into Illustrator. I'm just going to turn on those top- level percentages, and you can see we're rounding here: 62% for that, 2% for the space between them, and then 36% for the overall right-hand column width. Now, those values are helpful to know, but they're not as important as the values underneath them. Now, values underneath them, remember, are the padding for left-hand column; the content width for the left-hand column, which is 570 pixels; the spacing or the margin between those two columns, which is 20 pixels; and then the content width and the padding of the right-hand column.
When I calculate these percentages--and I'll go ahead and turn those on--here is where people typically get confused when doing fluid layouts. Inside this particular element we have a width of 570 pixels and we have padding of 20 pixels. Now, when you're calculating this it's really, really tempting to say okay, what percentage of 590 is 20 pixels, and that's what I need for my padding, but that is wrong. Padding and margins are calculated based upon their parent elements. So, this element, this orange column right here, that element's parent element is the body tag, which is based off of the 960 pixels.
So, in order to get the correct percentage for that padding, I have to say what percentage of 960 pixels is 20 pixels and in this case it's 2%. So, I break my percentages down. 2% for the padding, 60% for the content width, 2% for the spacing between them, 34% for the content width here, and then 2% for the padding. Now, that's pretty easy to calculate on a top-level basis, but the more complex your layout gets, the harder it gets to calculate the proper percentage values, especially for margins and padding. And let's demonstrate that by taking a look at the possibility of adding interior shapes to our layout.
So, if I just do the interior measurements of this particular element, we have 250 pixels for the content width of this and we have 10 pixels for the padding all the way around it. Now, that's going to leave us with right around 20 to 30 pixels' worth of spacing in between that. If these interior shapes have padding more than 10 pixels, remember their parent element is this orange left-hand column. So, that 10 pixels needs to be calculated as a percentage of the width of this column.
However, for padding and margins you don't use the total width; you use the content width. So, I need to know what the percentage 10 pixels would be of 570 and not 590. That is a really big distinction, because when you're calculating the content with itself, in this case 250 pixels, I have to calculate that based on the container width itself. To make this point again: when you're calculating percentages just for content width, you calculate that based on total width.
When you calculate percentages for margin and padding you calculate that percentage based on just content width, and so because of that, when I turn on these values again, you can see that for the padding of this I'm at 1.75%. Again, I'm rounding here. For content width I'm at 44.5, but for spacing between these two, I'm actually at 4%. And again, those values have just been rounded. So, I can't just pass this 2% value here or here and get what I'm expecting.
So, I can't just calculate the interior padding or margin between these based on this 2% value. I have to go back to the overall content width and container width from that column to do that. Now, we have the percentage values that we need to create our fluid layout. Now remember that above all else fluid layouts are really just about the relationship between elements. Don't get too caught up in finding the absolutely precise percentage points for a target measurement. I mean it's helpful to use this as a starting guide, but don't get too carried away with it.
What you do need to remember, however-- and this is the most important thing from this movie--that margins and padding values are calculated based on the content width of their parent elements. In most cases when the fluid layouts fail it's these values that typically turn out to be the problem, so you want to pay extra attention to them when you're planning and writing your styles.
Sign up for a Premium Membership to download courses for Internet-free viewing.
Watch offline with your iOS, Android, or desktop app.Start Your Free Trial
After signing up, download the course here or from the iOS/Android App.