CSS Margin and padding
2012/05/20 Css resources, examples & tutorials for webmasters and online professionals
CSS Margin and padding
p{ margin: 1px 2px 1px 4px; }
p{ padding: 1px 2px 1px 4px; }
CSS Margin and padding both generate a space around the HTML element of your page.
There is a fundamental diference between the two tags however:
- Margin creates white space outside the element;
- Padding creates white space inside the element.
This means that margin will create the white space outside the border element and background (see example below).
CSS Margin usage
You can use margin and padding in the following way:padding-left: 3px;
CSS Padding usage
margin-right: 2pt;
But you can also combine the 4 margin/padding options:
margin: 2px 0 3px 5px;
padding: 3px 4px 5px 6px;
Where the 4 numbers stand for 'top, right, bottom, left'.
Values
You can use fixed numbers (px, pt, em, etc) or % if you want to use a percentage of the html element.
Examples
<div style="background: red; margin: 6px;">This is a box with a margin of 6 pixels.</div>This is a box with a margin of 6 pixels.
<div style="background: red; padding: 6px;">This is a box with a padding of 6 pixels.</div>
This is a box with a padding of 6 pixels.
Summary
Use css margin and css padding to create space around the html elements on your page. Padding will create space -inside- an element and margin -outside- the element.
Shortcut
In your example you use 'margin: 6px' and 'padding: 6px'. These are very handy shortcuts which you do not mention in the text.
margin:6px sets a margin of 6 pixels on all for sites.
margin: 2px 4px; sets 2 pixels on the top and bottom and 4 pixels on the left and right.
2009-11-07 - Webhamster
Leave a reply.