CSS: Core Concepts
Illustration by Richard Downs

Defining element size


CSS: Core Concepts

with James Williamson

Watch every course in the library with a lynda.com membership.
Each course includes high-quality videos taught by expert instructors.

Start Your Free Trial Now

Video: Defining element size

Over the last few movies we have explored the box model properties of margins, borders and padding and also along the way we've set an element's width and height. In this movie I want to formally introduce the width and height properties and review how all of the box model properties are used when calculating an element's size. To do this we are going to be working with the element-sizing.htm file, which you can find in 05_06 directory. I just want to preview this in a browser really quickly so you can see what's going on here.
Expand all | Collapse all
  1. 4m 54s
    1. Welcome
    2. Using the exercise files
      4m 2s
  2. 1h 8m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 35s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 44m 54s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      8m 26s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 19s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 50s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 11s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 56s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

please wait ...
Watch the Online Video Course CSS: Core Concepts
8h 52m Beginner Nov 22, 2011

Viewers: in countries Watching now:

In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.

Topics include:
  • Exploring default styling
  • Writing a selector
  • Setting properties
  • Working with common units of measurement, including ems and pixels
  • Structuring HTML correctly
  • Understanding the cascade and inheritance
  • Setting a font family, font size, text color, and more
  • Understanding the box model
  • Styling container elements
  • Working with RGB vs. HSL values
  • Styling drop shadows
James Williamson

Defining element size

Over the last few movies we have explored the box model properties of margins, borders and padding and also along the way we've set an element's width and height. In this movie I want to formally introduce the width and height properties and review how all of the box model properties are used when calculating an element's size. To do this we are going to be working with the element-sizing.htm file, which you can find in 05_06 directory. I just want to preview this in a browser really quickly so you can see what's going on here.

I want you to focus on the two elements that have background colors; essentially we are going to create a two column layout with these, one on the left, one on the right. And we are going to make sure as the text is telling us here, that the size of these two columns don't exceed 600 pixels, they have to fit within the sort of 600 pixel space. And to do that we are going to have to accurately be able to calculate the width of both of these columns and that's going to be based on all the different box model properties that we are applying. I am going to get back into my code. And just so we understand our structure a little bit, we have inside of our article we have two div tags, one with a class of one, the other one with a class of two and those are the elements that we are going to be working with.

We already have some selector started for these, one and two and currently all they are doing right now is applying background colors. And then we also have a float property applied to them. Now as I mentioned earlier we will definitely talk more about the float property in the CSS page layout course. However, just for this particular exercise, notice that we are floating one to the left and floating another one to the right. That's going to allow us to position these two elements side-by-side, one on the left-hand side and one on the right-hand side. So the first thing I want to do is introduce the width property. So for the .one selector, I am going to go ahead and type in the width property and I am going to give it a width of 300 pixels.

Now remember we know that the parent container is 600 pixels wide. So if I make both of these 300 pixels wide and save it and test it, I see that indeed I'm positioning them right side by side, they fit perfectly in there. As a matter of fact they split right down the middle 300 pixels on this side, 300 pixels on that side. So while it certainly does fit, it doesn't look very good because the text in both of these instances is just budding right up against the edge of their container, so right it begins the edge of each column wall right up against the edge of each other.

No, that doesn't look so good. So we know that one of ways that we can keep content inside of an element, away from the edge of that element is to use the padding property. So I am going to go back into my code and let's just add a little bit of padding here. So I am going to do a padding of 25 pixels for both of these. And we know that if we use this padding shorthand notation we are applying padding equally to all side, so top right bottom and left are all getting 25 pixels worth of padding. So if I say this and test this in the browser, uh oh, Okay so that one good thing is that the text is being held away from the edge, but the bad thing is, is that our columns or collapsing basically, we are having this sort of collapse happen.

Now the reason that that's happening is we made a very common mistake. We applied the width and the padding separate of each other and really didn't consider how one was going to affect the other. So if I go back in my code, I can see that I have a padding of 25 pixels to the left-hand side and a padding of 25 pixels to the right-hand side. I have a width of 300, remember those values are cumulative. So each one of these is now 350 pixels wide, which equals 700 pixels wide and that means that the containing element isn't wide enough to hold them anymore so that's why we have that for a column collapse.

So one of the biggest mistakes that I see new designers making is miss-interpreting this property of width as being sort of this catch all global width property for the entire element and it is definitely not. As I mentioned earlier you should really consider that property to be more of content width, you know if it wasn't longer to type out I would even you know support it being change to that, but that's exactly what it defines, it defines the width of the content and that's not the total width of the element. So in order for this to be able to fit we have to subtract a total of 50 pixels from both of these.

So I am going to go head and go 250 here, 250 here, I'll save that and preview it. And you can see now both of those columns fit inside their parent element because the value still now adds up to 600 pixels. You have to remember this every single time you're trying to fit elements within a container whether they are the same width or not. Let's say we wanted sort of an imbalanced column width here. What if I wanted the first column, for example, to be 400 pixels wide and the second column to be 200 pixels wide? Okay to do that I'd have to take the 50 pixels that have here for padding and I would add it to 350 pixels for a total width of 400 pixels.

For my second one here, if I wanted this to be a total of 200 pixels wide, I would remember that I have 50 pixels worth of padding and so I would make that 150 pixels wide. So again if I save this I go back to my browser, refresh it and now indeed I have columns of unequal width, awesome. So this is really good for a theoretical exercise, but what if we try to do something that was maybe a little bit more real-world, something that your going to run into time and time again when you're creating columns with in parent elements.

A lot of times you are going to have to make sure that you're keeping track of all of the box model properties that are being, that includes margins, padding, borders, width, height all of that to make sure that's all fitting the way it is supposed to. So what if I wanted to do two columns here, have a border down the middle separating the two columns and have equal amount of space on either side and equal amount of space here and here. Well I have 600 pixels to work with, so now that in how much room I have to work with I can begin thinking about how to make that happen.

So I am going to go back into my code and just so I can see this a little bit easier, I am going to copy this last paragraph text here, I will go ahead and copy that, and then I am going to create a new paragraph inside my first div tag, I am just going to paste that text right there. I do want to copy that paragraph tag because I don't want that class to come along with it, here it goes. And again just to make sure that I'm really going to see this sort of tall column I am going to paste in again, so that I three paragraphs in here now.

So I am going to back up to my styles. And let's take a look at creating the sort of uneven columns that I was talking about. First what I am going to do is I am going to get rid of the background color because I really want this to look the way it would normally look on the page. I'm not going to change the width, I will leave the width at 350px, but for padding I am going to change this, I am going to do a padding left of 25px, and then I am going to do a padding-right of 23px. Now why am I doing that? You remember I mentioned having a border right? So a border has a width as well in this case I am going to give the border 2 pixels worth over width.

So if I had not subtracted this by 2 pixels then we would still have that sort of column overflow and our columns would break. So now I am going to do a border- right as well 2px solid and black. I am also going to change the height so I am just going to do a height of 500px, and then I am going to do a text align of justify. And I want to do justify types so that I can really see where that padding is being applied. Now the height is optional, but I want to introduce the height property as well just like width, height is also the height of the content and it is cumulatively added just like width, height is based on content and any padding to the top and bottom any top and bottom borders and even top and bottom margins need to really be factored in to the overall height of the element.

Now remember margins aren't always factored into the height because sometimes margins go away and that's why we really talk about the container height and container with involving the border padding and width and height properties. Now I am going to go down to the second one here, I am going to get rid of its background color as well. Now for this one I am going to go ahead and just give it a single padding value but I am going to modify this a little bit, I am going to change the syntax to 0 25. You know when we took our padding up here and modified it, we took away all the padding to the top and the bottom of this particular element.

So by doing padding zero here remember that's passing a zero value for top and bottom, 25 for left and right. Since the second element here the one with the class two applied does not have a border on it we can use 25 pixels worth of space for the left and right and that's fine. Now I am going to make its height match, I am going to go ahead and do height here of 500 pixels and I am going to do text align-justify; Let me go ahead and save that, preview that in my browser and you can see we are getting exactly what I'm looking for here, I am getting two columns of text equal amount of space here on either side, it's actually not perfectly equal its two pixels off on the side, but for the most part you'll never catch that and again I have 25 pixels worth a padding here and here.

You know in this case with these columns we only used padding, borders and width to determine the overall element width, if we had decided to use margins instead on the left and right there, we would've had to account for those values as well. Now most of time as a designer you're just going to make a judgment call about when to use margins versus padding, but there are going to be other times when it's pretty clear as to which one you need to use. In this case padding works really well because we're using that border and we want to make sure that spacing stays on the inside of the element.

Now no matter the exact formula or which of the box model properties that you are using, you just need to make sure that you account for all properties when attempting to fit elements to a specific size and be prepared to spend some time figuring out what the values for each of those properties will need to be in order to achieve the desired size.

There are currently no FAQs about CSS: Core Concepts.


Learn to Code with Hands-On Practice

Code practice

In one click, you can now launch the beta Code Practice window to easily work with the files from the videos. No downloading or software required! Work along with your course in real time with this new in-browser coding environment

Practice environment

No, thanks!
Share a link to this course

What are exercise files?

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

Can I take this course without the exercise files?

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

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed CSS: Core Concepts.

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.