Start learning with our library of video tutorials taught by experts. Get started
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.
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.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.