Now a days I am learning web technologies and want to share whatever I learn. The basic idea is to make my learning available to others and have them all at same place. I hope that it will be of some use to you all.
Why we need to center a DIV?
You must have seen few websites which have a centered layout. They will not span the whole computer screen. This is a really attractive and efficient design technique. It is quite soothing in looks and also it helps creating a site for almost all screen resolutions. e.g. www.w3schools.com.
Real work
Lets start the real work. We will be using HTML and CSS to achieve this.
Say you have a DIV as a parent control of your content. You want the content to be centered on the page.
< /DIV>
In your CSS style this DIV as:
#divSiteBody
{
width:800px;
margin:auto;
}
or you can style as:
#divSiteBody
{
width:800px;
margin-left:auto;
margin-right:auto;
}
You can choose "width" as per your need, it sets the width of the DIV. The CSS margin properties define the space around elements.When we set margin(-left...) as auto, the browser sets the margin. The result of this is dependent of the browser.Thus when we use the first code snippet the vertical margins can also vary. Thus using second snippet is better.
More InsightWhen we use "auto" the browser will first assign the mentioned width to the DIV and then it will distribute the available space to the left and right margin. This is because they both gets the same priority.
The snippets have been tested IE,FF and Google Chrome.
Hope the post will be of some use. I will try to continue posting.
Why we need to center a DIV?
You must have seen few websites which have a centered layout. They will not span the whole computer screen. This is a really attractive and efficient design technique. It is quite soothing in looks and also it helps creating a site for almost all screen resolutions. e.g. www.w3schools.com.
Real work
Lets start the real work. We will be using HTML and CSS to achieve this.
Say you have a DIV as a parent control of your content. You want the content to be centered on the page.
< DIV id="divSiteBody">
...
...
...
...
...
...
In your CSS style this DIV as:
#divSiteBody
{
width:800px;
margin:auto;
}
or you can style as:
#divSiteBody
{
width:800px;
margin-left:auto;
margin-right:auto;
}
You can choose "width" as per your need, it sets the width of the DIV. The CSS margin properties define the space around elements.When we set margin(-left...) as auto, the browser sets the margin. The result of this is dependent of the browser.Thus when we use the first code snippet the vertical margins can also vary. Thus using second snippet is better.
More InsightWhen we use "auto" the browser will first assign the mentioned width to the DIV and then it will distribute the available space to the left and right margin. This is because they both gets the same priority.
The snippets have been tested IE,FF and Google Chrome.
Hope the post will be of some use. I will try to continue posting.
Comments
Post a Comment