Dimensions, Padding & Margin
ON THIS PAGE
| Property Name | Some Possible Values | What It Does |
|---|---|---|
| width | 400px | 70% | Sets width |
| height | 400px | 70% | Sets height |
| padding | 10px | 5% | Set padding on all 4 sides |
| padding-top | 10px | 5% | Set padding on top |
| padding-bottom | 10px | 5% | Set padding on bottom |
| padding-left | 10px | 5% | Set padding on left side |
| padding-right | 10px | 5% | Set padding on right side |
| margin | 20px | 7% | Set margin on all 4 sides |
| margin-top | 20px | 7% | Set margin on top |
| margin-bottom | 20px | 7% | Set margin on bottom |
| margin-left | 20px | 7% | Set margin on left side |
| margin-right | 20px | 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 onedivelement inside of another. The outerdivelement (red border) has its width set to 400 pixels (400px). The innerdivelement (green border) has its width set to50%hence its width becomes 200 pixels.
width: 50%;
If no width is specified, thedivelement will expand to take 100% of the available space. On the other hand,tableelements will only take as much space as is required to display its content.
The following examples will illustrate ~
Below is adivelement 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 atableelement (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
paddingproperty. 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 thepaddingshorthand 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 5pxtop, right, bottom, left respectively
(Easy reminder - Clockwise starting with top) - Margin ~ Here's what's going on with the
marginproperty. 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 themarginshorthand 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 5pxtop, 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
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...

Legend
= Margin
= Border of block element
= Padding
= Content of block element
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):
<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:
<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:
- How to Use CSS - An Overview
- Inline Styles
- Embedded Style Sheets
- External Style Sheets
- Class Selectors
| <~ BACK | TOP | NEXT ~> |
