margin & padding

Objectives:

  • Set up html and css files with css file link in head.

File Location

You should be located in the following files:

   javascript-library
        └── 0-PreWork
            ...
            └── 2-CSS-Basics
                └── 03-margins-padding.css 
                └── 03-margins-padding.html

Description

Using margin and padding are ways that we can change the spacing around and between elements. The margin property is used to generate empty space between elements, outside of the border, where the padding property that creates space around the element's content inside of the border.

Let's grab an image from here to illustrate the difference between the two a little more clearly. Make sure that you save it to your 4-assets folder with the other images saving it as margins-padding.gif so it can be referenced in the HTML as below in our example.

Sample Code

HTML:

CSS:

Discussion

Now that you've got a little bit of experience with these properties, try testing different effects by uncommenting and commenting the padding-top/right/bottom/left in #padding-experiment at the bottom of your .css file.

Challenge

Check out the official documentation on box model to further understand these concepts.

Extra Resources

Click here!

Last updated