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

Controlling interior spacing

From: CSS: Core Concepts

Video: Controlling interior spacing

The space inside an element is controlled through three properties, an element's width, height, and padding. Now we'll discuss width and height in a more detail in just a moment, so for right now, I just want to focus on an element's padding. Padding is the space between an element's content and the inside edge of its border. It's primarily used to keep content away from its edge in much the same way that table cell content is often kept away from the border of a table cell. So here, I have the interior-spacing.htm file open and you can find that in the 05_03 folder.

Controlling interior spacing

The space inside an element is controlled through three properties, an element's width, height, and padding. Now we'll discuss width and height in a more detail in just a moment, so for right now, I just want to focus on an element's padding. Padding is the space between an element's content and the inside edge of its border. It's primarily used to keep content away from its edge in much the same way that table cell content is often kept away from the border of a table cell. So here, I have the interior-spacing.htm file open and you can find that in the 05_03 folder.

Not a lot going on in this file, structurally it's pretty simple. We have a heading one, a div tag below that which has a paragraph inside of that as well. If I preview this in my browser to just kind of show you what's happening, here is the heading one tag and here is the div tag below it and here is the paragraph inside the div tag. Okay, so I am going to jump back into my code, we have some selectors that are sort of applying some of the default styling that we have right now. So I want to go up to the h1 and we'll first start experimenting with padding a little bit on the h1 tag.

So, just after the height, I am going to go ahead and create a new line and I want to do padding-left. So if you watched the previous movie on margins, you can see that the syntax is extremely similar. So then we just say padding and dash and then the direction we want to apply the padding too. Much like margins, you can apply it to the top, right, bottom, and left independently of one another. So here I am just going to do a padding- left of 50px, and then I am going to do a padding-right of 50px as well.

If I save this and then preview this in my browser, I can definitely see the 50px worth of padding over here on the left-hand side, but I can't really see it over here on the right-hand side and that's just simply because there's no content over there to bump into it, but it is there. So the content were extended or maybe made a little bit larger, you'd be able to see that padding. So there is our padding. You may have also noticed that when we added this padding, our element didn't really get any wider. It's exactly the same width as it was before. Well, if I go back into the code, I can see that I don't have a defined width.

I do have a defined height. This is a block level element. So it's essentially taking up 100% of its parent element. What that means is, when you add padding to that, you're reducing the amount of content width that element has. So I am sort of subtracting 100 pixels from the available width for the content. We can see that illustrated a little bit more clearly if we just sort of increased the value here. So I am going to increase the value for the padding-left to 250 and increase the padding-right to 250 as well. If I save this and preview it, oh yeah, we can definitely see that.

Now what happens here is because we have a defined height, you know the height is set to 100 pixels and with a padding of 250 pixels on this side and a padding of 250 pixels on this side, there is no longer enough room for the content. So the content does exactly what it did in the last movie when we ran out of room for it, it overflows. So that's one of the reasons why it's so important to understand how these widths and heights are calculated, so that you don't end up with content overflowing that you don't want to overflow. Now I am going to go back to my code and I am going to reset those values to 100 pixels each and that's going to be a little bit nicer in terms of the way that they fit.

But I also want to address padding in terms of how it affects the height of an object as well. So if I go down on the next line and I type in padding-top and I make that 150 pixels, and then I do padding-bottom and I change its value to 50 pixels, what's that going to do for us? Well, remember, we have a predefined height of 100 pixels. When we didn't have a predefined width, it's simply reduced the amount of width by the left and right padding and reduced the amount of content width you had.

But what happens if you have a defined height here. So if I save this and preview it, you can see what happens to the height, the height grows. So content width and content height, the width and height properties are added to padding. They are cumulative. So in this case, the 100 pixels worth of height is being added to 150 pixels worth of padding on the top, 50 pixels worth of padding on the bottom and that leaves us with a 300 pixel tall heading 1. Now this cumulative nature can sometimes make it a little bit difficult to really accurately calculate the width and height of an element.

Let me show you what I mean by that. So again, going back down to the structure, remember we have a div tag that has a nested paragraph inside of that. So I am going to go up to the div tag here and underneath the background, I am going to go ahead and give it a predefined width of 400 pixels. I then want to give it a padding-right of 100 pixels and then a padding-left of 100 pixels as well. That gives us a total, obviously, of 600 pixels and if I go up and look at the body selector, I can see that it indeed has a width of 600 pixels also.

So, if I save this and preview this in the browser, you can definitely see the padding here on the left-hand side, but you can't really see it quite as much on the right-hand side. What we do know is that it hasn't really extended the width of the div tag at all, it's as same as it was before and that's because before the width was being determined automatically, it was just expanding to fit its parent element. Okay now, I want to go back into my code and now I want to set some padding on that nested paragraph, the nested paragraph on the inside.

So for that one, I want to give it a padding-left of 100px and a padding-right of 100px as well, and I am going to go ahead and save that. Now if I test this in my browser, I can see that what's happening is I still have this 100px worth of padding to the left of the div tag, but then I have 100px worth of padding for the paragraph, I have 100px worth of padding on this side.

The paragraph already has 300 pixels defined for its width, so that's a total we know of 500 pixels. Well, the 500 pixels isn't going to allow the padding over here for our div tag. So much the same way that we sacrificed margins in the last movie, we're sacrificing padding in this one. So in this case, the padding for the div tag is basically being sacrificed to make sure that the paragraph can fit inside of it. But what happens if the value is, again, too great to do that. If I go back here and do a padding- right of 200 pixels, save and test that, notice that we get our overflow.

Hopefully, this illustrates to you the importance of understanding how much room you have to work with when you're defining the width and padding for these interior elements. It's really easy to break a layout without meaning to by simply taking up too much available space. Now, I closed the last movie by showing you guys auto-margins and talked a little bit about that property. Padding doesn't really have an auto value, we don't have anything that's comparable in terms of padding. However, we can use percentages with padding and when you use percentages with padding, you can sort of often calculate a padding that sort of flexes based upon the available amount of space.

Let me show you what I mean. Let's go right back into our code and what I am going to do is I am going to get rid of this h1 element just because I really want us to focus on the percentage based nature of what I am going to show you guys. All right, now I am going to go up to my body element and instead of a very fixed width here for the body, I am going to go ahead and give the body 80% so that's saying hey 80% of the available viewport window, go ahead and give me that. All right! Now I am going to scroll down and in the paragraph selector, I am going to change the padding right and left.

I am going to change both of those values to 20%, so 20%, and I am also going to get rid of the width value, I don't need that. Same thing over here in the div tag. I am going to get rid of its padding right and left in terms of those values and I am going to replace it with 10% and then save that. And looking down the page here, I can see I left a little px here on the padding right, so I am just going to get rid of that, there we go. And I want to do one more thing here.

I need a little bit more content inside this paragraph and I know watching somebody type is never fun, especially me because I have trouble typing, but I'm just going to add a sentence or two here. I am going to type in I want to put more content into the paragraph so that I can see the fluid nature of the padding values when they are set to percentages.

By adding more content to the paragraph element, I should be able to visualize this better as a solid block of text so sort of describing what I'm doing there as well. Feel free to copy and paste some text, put in some Lorem Ipsum or whatever you want to do there. Now before I preview this, I've got to remember to go up and also delete the width here from the div tag. So let's just review these styles to make sure we've got everything right before we preview this.

We don't want any width elements on the div or the paragraph. And on the div, I just want padding right left values of 10%; on the paragraph, I want padding left and right values of 20%. On the body selector, I just want a width of 80%. So I am going to go ahead and save this and then preview this in my browser and now if I resize the page, you can see exactly what I am talking about in terms of the fluid nature of this padding. As it gets smaller, the amount of space available for the padding shrinks and as it gets taller the available space for the padding grows.

So it's not quite auto margins, it's not an auto padding, but you can use percentages to give yourself fluid and flexible padding regions. So you know as a whole, padding is a little less complicated than margins, but it still has its quirks that you need to be aware of when setting values for it. Remember, when we set too great a padding value and it broke our layout, if you don't account for element width and height and then how those work with padding, it can have disastrous consequences for your layout or just overall element spacing.

You know oftentimes, when your layout breaks, padding is sometimes like that hidden culprit, because designers can forget that padding is applied to parent elements. So just to make sure that when you begin to organize your styles that you track when and where your padding is applied, so that as you begin to style those interior elements, you don't have any unwanted surprises later on.

Show transcript

This video is part of

Image for CSS: Core Concepts
CSS: Core Concepts

81 video lessons · 40212 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 57s
    1. Welcome
      55s
    2. Using the exercise files
      4m 2s
  2. 1h 7m
    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 34s
    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. 42m 39s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      6m 11s
    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 18s
    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 49s
    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 10s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 55s
    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

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
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

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

Congratulations

You have completed CSS: Core Concepts.

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


OK
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?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.