spacer

Dimensions, Padding & Margin

 
spacer
The CSS properties in the table below can be used to define widths, heights, padding and margin on various components on your web page. These properties are typically applied to block elements but you can also apply them to images and some form elements. These include the following:



CSS Properties List 2 - Dimensions, Padding and Margin
Property NameSome Possible ValuesWhat It Does
width400px | 70%Sets width
height400px | 70%Sets height
padding10px | 5%Set padding on all 4 sides
padding-top10px | 5%Set padding on top
padding-bottom10px | 5%Set padding on bottom
padding-left10px | 5%Set padding on left side
padding-right10px | 5%Set padding on right side
margin20px | 7%Set margin on all 4 sides
margin-top20px | 7%Set margin on top
margin-bottom20px | 7%Set margin on bottom
margin-left20px | 7%Set margin on left side
margin-right20px | 7%Set margin on right side
(Please see Padding and Margin for more information about these properties.)

NOTES:
  • Pixels (px) ~ This sets a width, height, padding or margin in pixels. The syntax is any number followed by px.

    Examples:
    width: 400px;
    padding: 10px;

    Use pixels for measurements when you want to closely control the layout of your web page. (More about pixels...)
  • Percentage ~ This sets the width, height, padding or margin as a percentage of available space allotted to the element. The syntax is any number followed by the "%" sign.

    Examples:
    width: 50%;
    padding: 5%;

    Percentages are useful for creating a 'liquid display' which is a web page layout that expands and contracts according to the user's screen resolution. Percentages are also useful for applying widths and heights based on quick visual estimations of the available space for an element.

    SOME EXAMPLES »
    Below is an example of placing one div element inside of another. The outer div element (red border) has its width set to 400 pixels (400px). The inner div element (green border) has its width set to 50% hence its width becomes 200 pixels.

    width: 50%;

    If no width is specified, the div element will expand to take 100% of the available space. On the other hand, table elements will only take as much space as is required to display its content.

    The following examples will illustrate ~

    Below is a div element that has no width specified and that contains a single word "TESTING". Notice how it expands to take 100% of the available space:

    TESTING

    Below is a table element (one row, one cell) that has no width specified and contains a single word "TESTING". Notice how it only expands to provide just enough room for the content:

    TESTING

  • Padding ~ Here's the lowdown on the padding property. Instead of laboriously typing out all four properties, e.g., padding-top, padding-right, padding-bottom, padding-left, to set the padding on all four sides of your element, you can just use the padding shorthand property and a special syntax which will allow you to set the padding on all four sides at once.

    The following examples will illustrate how to use the special syntax:
    padding: 10px;
    All four sides = 10px each

    padding: 10px 20px;
    top & bottom = 10px each, left & right = 20px each

    padding: 10px 20px 15px;
    top = 10px, left & right = 20px each, bottom = 15px

    padding: 10px 20px 15px 5px
    top, right, bottom, left respectively
    (Easy reminder - Clockwise starting with top)
  • Margin ~ Here's what's going on with the margin property. Instead of laboriously typing out all four properties, e.g., margin-top, margin-right, margin-bottom, margin-left, to set the margin on all four sides of your element, you can just use the margin shorthand property and a special syntax which will allow you to set the margin on all four sides at once.

    Here's how to use the special syntax:
    margin: 10px;
    All four sides = 10px each

    margin: 10px 20px;
    top & bottom = 10px each, left & right = 20px each

    margin: 10px 20px 15px;
    top = 10px, left & right = 20px each, bottom = 15px

    margin: 10px 20px 15px 5px
    top, right, bottom, left respectively
    (Easy reminder - Clockwise starting with top)
  • Padding and Margin? ~ So now you may be thinking, yeah that's all very fine and well BUT... uhhhhh....

    ...Exactly what are padding and margin?

    Well quite simply, padding equals space between the content of a block element and its border. On the other hand, margin equals space surrounding a block element's border.

    The following color-coded example will illustrate:


    PADDING & MARGIN ILLUSTRATED
    spacer
    spacer
    It was a dark and stormy night. Blah blah blah etcetera... I'll just ramble on mindlessly to create some content in this block element for demonstrational purposes... How now brown cow... And so on and so forth...
    spacer
    spacer

    Legend

     
    =   Margin

     
    =   Border of block element

     
    =   Padding

     
    =   Content of block element

EXAMPLES:

Here's an example of an inline style applied to a paragraph tag using some of the CSS properties described above (dummy text is inserted for demonstrational purposes):


Example 1 - INLINE STYLE

<p style="width: 300px; margin-left: 100px;">Donec ut dolor. Nulla orci leo, facilisis in, ultrices in, viverra pretium, ligula. Donec suscipit. Cras vulputate. Donec vel neque. Integer nec nisl sed felis eleifend adipiscing. Suspendisse arcu magna, viverra eget, pretium vitae, iaculis ac, lorem. </p>



OR...

The same thing could be declared as an embedded stylesheet in the document head:


Example 2 - EMBEDDED STYLE SHEET

<style type="text/css">
<!--
p {
   width: 300px;
   margin-left: 100px;

}
-->
</style>



...and in the document body, you would simply put this:


<p>Donec ut dolor. Nulla orci leo, facilisis in, ultrices in, viverra pretium, ligula. Donec suscipit. Cras vulputate. Donec vel neque. Integer nec nisl sed felis eleifend adipiscing. Suspendisse arcu magna, viverra eget, pretium vitae, iaculis ac, lorem. </p>



Both Example 1 and Example 2 will produce the following effect which is a paragraph that is 300 pixels wide and has a left margin of 100 pixels:

Donec ut dolor. Nulla orci leo, facilisis in, ultrices in, viverra pretium, ligula. Donec suscipit. Cras vulputate. Donec vel neque. Integer nec nisl sed felis eleifend adipiscing. Suspendisse arcu magna, viverra eget, pretium vitae, iaculis ac, lorem.



*   *   *


Now if you're wondering how I drew the borders around the div elements used in the examples on this page, you can find out on the next page where we'll learn how to apply borders using CSS...



MORE CSS PROPERTIES:
SEE ALSO: