New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Setting flexible width values

From: CSS: Page Layouts

Video: Setting flexible width values

In our last movie, we calculated the percentage values that we are going to need to create a two-column fluid layout. In this movie, we are going to put those values into practice by applying them to the appropriate elements. So I have the fluid.htm file open from the 06_03 directory, and if you remember from our fixed layout in the last chapter, we have a page structure that is pretty much almost exactly the same as we had before. I just want to preview this page in a browser really quickly to show you something about default styling. All right! So I have opened up our page in the browser, and you can see we don't really have any margins or padding goings on within the elements themselves. And as we look through this, obviously the elements are just sort of stacking one on top of each other, so we still need to position everything.

Setting flexible width values

In our last movie, we calculated the percentage values that we are going to need to create a two-column fluid layout. In this movie, we are going to put those values into practice by applying them to the appropriate elements. So I have the fluid.htm file open from the 06_03 directory, and if you remember from our fixed layout in the last chapter, we have a page structure that is pretty much almost exactly the same as we had before. I just want to preview this page in a browser really quickly to show you something about default styling. All right! So I have opened up our page in the browser, and you can see we don't really have any margins or padding goings on within the elements themselves. And as we look through this, obviously the elements are just sort of stacking one on top of each other, so we still need to position everything.

But as I resize this browser, I want you to notice something about layouts. By default, layouts are fluid. If you have a block-level element, that block-level element, if a width is not defined on it, it will go out and stretch to fill the parent container, meaning its width is set by default to 100%. Even if you don't do anything at all, your layout is more or less fluid. So I am going to go back into our code, and the first thing that I'm going to do is I'm going to scroll down into my styles to the body selector. And we need to define a width for this.

Now, unlike in the previous chapter when we were doing fixed layouts, instead of applying a fixed-target resolution to this, I'm simply just going to use a percentage value for the width. And in this case, I'm going to go ahead and give it a width of 80%. How wide you make the wrapper container, in this case the body selector, is entirely up to you. In some cases people like to do 90%, 95%, if you don't want a whole lot of space on the edges. It really boils down to how much space you want on the right and left side of this. If you're centering this at 80%, if I save my file and go back to the browser and refresh that, you can see it gives me a nice amount of space over here on either side, and based on the available browser space, it's going to give you nice column widths as well. Okay.

I am going to keep scrolling down, and one of the things that most people forget to do in fluid layouts is set padding. It happens all the time. You concentrate so much on the columns of your layout and the various elements that you sort of forget that every single one of those elements that needs to have padding that's going to line elements up, should have the same padding. So this header, we need to go in and change this padding here so that it's getting a percentage for its left padding. So where right now I have 32 pixels--and remember that was our old fixed layout-- I am going to highlight this and I am going to change that left padding to 2% and save that.

So now it has 32 pixels' worth of top padding and then 2% for the left. And if I go out to the browser and refresh that, you can see the spacing here in the headline up in the header changed a little bit. Okay. Now, we are going to go down and do our columns. Notice we have column1 floating to the left. We have column2 floating to the right. Now, they don't have any defined widths right now, so we're really not getting the effect we want to with those floats. So with column1, I am going to go down in that selector, I am going to give it a width of 60%.

Now, we remember those from our previous exercise. If you still have your notes you have taken, if you were sketching that in a sketchpad, you should be able to see those percentages and just plug those right in. Underneath that I am going to do padding to the left and again, I am going to do a padding-left of 2%, and will save that. Of course that means I am going to go down to column2 and do something very similar. In this case, column2's width is going to be 34% and its padding, this time to the right and not the left, is also going to be 2%. You will notice because of the layout method that I use of floating one to the left and floating the other one to the right, I don't need to specify any type of margins between the two of them. Their widths will create an absence of space in between them, which gives me the spacing, and I don't have to actually declare that, which, for me, is nicer. Okay.

I am going to save this. Once again, I will go out to the browser, refresh the page, and there is our two-column layout. Again, if I resize the browser, you can see that this is now flexible. I am getting a flexible amount of space and a flexible amount of padding for both of those guys. Now, down a little bit lower, we still have these two elements, these two interior elements. So we're going to go in and set those percentage values on the interior elements that we were calculating in the last exercise as well. So going back into the code, I can scroll down and I can find those two news items right there.

So in the main news one, what I am going to do here is set its width to 44.5%. Remember, they are both going to be exactly the same. So I am going to give them both the same width. And then for padding, well, I want to set padding all the way around, so I am just going to do 1.75% for the padding. I also need to set some spacing between these two of them, because I'm not floating one to left and one to the right. So I need to do margin-right on these, and the margin between them, if you remember from the last exercise, is going to be 4%. And then I have this other selector right down below that that is stripping the margin-right value from the second news, so we don't get an extra bit of spacing there on the right-hand side of that.

So if I save this, go back out to the browser and refresh my page, now we get our interior spacing. And what I really want you to focus on is take a look at the space between these two columns, these two elements, and this element. And they're very close. Now, we had to give them all different percentage values. This is 2%, this is 4% based on where it's located, and this of course we are letting the actual columns create that space. But we are seeing sort of a consistent spacing between them because we took the time to plan that out and really calculate what those various percentages are going to be based upon their parent elements.

So there you have it. It is a basic two-column fluid layout. Now, your designs are probably, over time, going to be a lot more complex than this, but as long as you remember the rules behind calculating percentage values, and especially how it pertains to margins and padding, you should be fine. Now, next up, we are going to explore how page assets like images and other assets can affect fluid layouts and how to make those assets flexible.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40110 viewers

James Williamson
Author

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

Start learning today

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

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

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


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

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

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

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

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

   
submit Lightbox submit clicked
Terms and conditions of use

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