Last updated on December 15, 2009. Tags: block elements, CSS layout, margin, padding, tableless web page layout
In designing the layout of a website, it is important to define the sides of a block element. A website designer should provide adequate spacing between divisions, paragraphs, item list and images to create a website layout that looks formal, organized and pleasing to the eyes. To accomplish this, designers frequently use two CSS attributes: margin and padding.
We can define a set of margin and padding attributes for divisions, images, paragraphs, lists and list items. In this article, I will discuss the behavior of margin and padding in block elements (or divisions). The behavior of these attributes in images, paragraphs, lists and list items will be covered in one of my future articles
Similar to print media, margin can be defined as the width of space on the side of an element. If two block of texts were to place side by side, these blocks can approach one another only up to the extent allowed by the margin. The CSS code that defines the margin applicable to all sides of the block element is shown below:
.class-sample {
margin: 10px;
}
In the above example, if a certain block element points to the .class-sample (<div class="class-sample">), that block element would have margin of 10 pixels at all sides.
The margins need not to be the same at all sides. In certain design or layout, the web designer might want to have a bigger margin on one side and smaller on another side. There are two ways of defining different margins for different sides. One is by having separate codes for each side:
#id-sample {
margin-left: 10px;
margin-right: 15px;
margin-top: 5px;
margin-bottom: 0;
}
The other way is by using a single line of code:
#id-sample {
margin: 5px 15px 0 10px;
}
The sequence is top right bottom left. The margin on top is 5px, the margin on right is 15px and so on.
If there are only two numbers, like margin: 5px 15px;, the first number applies to top and bottom and the second number applies to left and right. If there are three numbers, like margin: 5px 15px 10px;, the first number applies to top, the second number applies to sides (both left and right) and the third applies to bottom.
You will also notice that whenever the margin is defined as 0, it's just 0. If your margin is not zero, you need to use unit like pixels (px), points (pt), inches (in) and centimeters(cm). However, if the margin is zero, you must not put any unit.
The way margin works with block elements and borders are summarized in the list below:
Margins increases the actual space needed by a block element. Padding works in exactly the same way as margin except for a few differences in relation to border and background. The CSS code for padding is of exactly the same form as margin except that the word "margin" is replaced by padding.
.class-sample {
padding: 10px;
}
Each side can also be given different paddings in a way similar to margins.
#id-sample {
padding-left: 10px;
padding-right: 15px;
padding-top: 5px;
padding-bottom: 0;
}
You can also assign different padding to each side using only a single line of code. The order in which each number applies is the same as margin. The effect in case there are only two or three numbers is also the same as margin. Moreover, just like in margin, a zero padding is simply coded as 0 but a non-zero padding must come with units like pixels, points, inches or centimeters.
#id-sample {
padding: 5px 15px 0 10px;
}
As stated earlier, the only difference between padding and margin is the way they interact with border and background. Aside from that, margin and padding can coexist.
Padding increases the actual space occupied by a block element but without increasing the space that can contain other elements (text, image, etc.) within that block element. A 300px by 400px block element with 15px margin actually occupies a 330px by 430px space.
The widths of the padding and the border separately contribute to the additional space needed by the block element. A 300px by 400px block element with padding of 5px and border thickness of 1px will in fact, occupy a space of 312px by 412px.
If the block element has margin, padding and border, they will all separately contribute to the additional space needed by the block element. The separation between margin and padding space will also be more obvious because the padding space lies within the border while the margin space lies outside the border.
Posted by Greten on December 27, 2008 under Cascading Stylesheet (CSS), Hypertext Mark-up Language (HTML)
Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
* Required. Your email will never be displayed in public.