Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So we've seen that both margin and padding can be set independently to the top, right, bottom and left of an element. Now as you can imagine, writing out separate properties for each one of those is quite inefficient, but thankfully margins and padding also have a shorthand notation that we can use when defining them, and to demonstrate this I've opened up the shorthand.htm file. You can find this in the 05_04 directory. Again, very simple file, will just have a couple of paragraphs on the page. One of them has a class of margin and other one has a class of padding and they both sort of explain what's going to happen with them.
One is going to be controlled through margins and the other one is going to be controlled through padding. So now here's what we're going to do. I'm just going to go right up and create another selector and I'm just going to type in .margin and inside of that we're going to apply the margin shorthand. So, instead of typing in margin- whatever, I just type in margin and I'm just going to type in margin:. Now after this I have several different versions of this shorthand notation that I can use depending upon how I wanted to apply margins and what values I want to apply to which side.
So let's go ahead and try out some of the different versions of this syntax. So the first thing I'm going to do here is 10px and then a space, not a comma but a space 15px and then a space, 20px, another space and then 30px and then a semi-colon. Okay, so what's happening here? Well, I'm applying margins independently to each side and you have to remember the order in which these are applied. We have top, right, bottom and left.
If you think about starting at the top of the clock and then going clockwise to the right, bottom and left sides, that's one way that you can remember this. Other people have used the mnemonic device TRBL top, right, bottom, left to remember that as well. So if I save this and test this in my browser, I'm getting 10 pixels worth of margin up top, 15 pixels worth on the right-hand side, 20 pixels worth of margin on the bottom and 30 pixels worth of margin here on the left-hand side.
Okay, so let's do the same thing for padding and check out its syntax. So I'm going to do .padding, and here I'm going to give it a padding property, so just like margin you don't have the dash, you just say the word padding and the same values that work for margin will also work for padding. So they both use the exact same shorthand notation. So what if I don't want to have to explicitly define all four sides? What if I have some sides or the top and the bottom, for example, that share a value? How would I do that? Well, instead of using four values I can also use three values.
So I can come in and say 10 pixels space, 20 pixels space, 15 pixels and then a semi-colon. Okay, so when you use three values, where are you applying these values? Well, the first value is still at the top, the last value is still at the bottom, but the middle value is going to be used for both the left and the right padding, so that middle value is applied to both left and the right. So if I save this and preview it, I get something very similar. I get 10 pixels on the top, I get 20 pixels of padding here on the right-hand side, I get 15 pixels at the bottom and I get another 20 pixels on the left-hand side.
So, so far we've seen the variation of syntax that allows us to pass four values for each individual side. We've seen a variation of the syntax that allows you to pass three values, one value for top, one value for the bottom and one value for the right and the left. But there is also a version of the syntax that allows you to pass two values. So if I go up and modify my margin and I change it to 10 pixels and 30 pixels, let's talk about what this does for us. Well, when you use two values, the first value is used for the top and the bottom, the second value is used for the right and the left.
So again, if I save this, preview it in my browser, now I have a top and bottom margin of 10 pixels and right and left of 30 pixels. Now in addition to having four values, three values or two values, you can also just go ahead and use one value. So for padding, I'm just going to come down here and say 30 pixels, and then I'm going to do the same thing for margins. So I'm just going to place both of them with 30 pixels. Now you've probably guessed what this is going to do for us. So when we have a single value that applies to all sides equally.
So in this case, I want to apply the same amount of margin all the way around the element or the same amount of padding all the way around the element too. So let's review. One value means the same value is applied everywhere, two values means that the first value is applied to the top and bottom, second value is applied to the right and left. If you have three values, the first one is applied to the top, the second one is applied to the right and left, and the third one is applied to the bottom. If you have four values, the first one applies to the top, and then you just go around clockwise to the right, left and bottom.
So if I save this and preview this in my browser, I see that I'm getting basically the same all the way around. You know what's interesting here is, when you look at it like this it doesn't really look like there's that much of a difference between the margin and a padding, and in fact, when there is no borders involved, when there is no background colors involved, it's almost impossible to tell the difference between margins and padding in terms of controlling element spacing. So just so we can see that there is a physical difference between these, what I'm going to do is go over to both of these selectors, and I'm just going to come in and apply a background color.
So I'm going to do background of #ccc, which is a gray and I'm going to go ahead and do the same color for both of them. #ccc, so the syntax of this is background: and then the # character is ccc; So I'm going to save that and preview this, and now you can definitely tell the difference between margins and padding. Padding is increasing the width and height of the element, whereas, margin is basically dealing with the spacing outside of the element. So the one thing I really like about this shorthand notation that we're using here for margin and padding is that it really is very, very flexible.
You can use it in a variety of instances. So I do want to point out, unlike the font shorthand notation, it's not going to pass any default values. So using shorthand notation for margins and padding are going to give you exactly what you expect when you use it.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 66493 Viewers
61 Video lessons · 96920 Viewers
56 Video lessons · 110602 Viewers
82 Video lessons · 105187 Viewers
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.