I have gone trough a lot of ways to horizontally and vertically center a DIV. Many of the code snippets had a Child DIV1 inside a Parent DIV2 to center the Child. They work perfect but I found a simpler way to center a single DIV trough CSS.
I am not sure if this is already known to all, but I experimented and came up with this simple CSS snippet.
Here is a visual representation of how this works:

Here is the CSS Snippet to be assigned to the DIV to be centered. The size of the DIV is 100 x 100 px ( same as the above example ).
/* CSS Document */
Here is another example with size 500 x 200 px.
/* CSS Document */
You can also find an online example here
Comments
hey its a very good tutorial to work on div.
Thanks for your tutorials!
but in my variant you can not initialize height! that's very good
This is a bad solution.. try to resize browser window... i think you will be unhappy.
I using another way.
I like this for its simplicity but I have one problem - if the browser is smaller than the div, the scroll bar only allows you to scroll to the right and hides content on the left side.
Great!
=)
/cheers
wow...i was searching for a good method to put div in center....i got it....thank u............
Yeah, for a starter, this might be considered the ultimate solution, but to say the least, this is quite a lot of work for something as simple as centering a div.
This should take care of your center div, with out having on the extra styles.
.your_div{
width: 500px;
height: auto;
margin: 0px auto; <!--this code is all your need to really center the div-->
}
Benga creative
Yeah, for a starter, this might be considered the ultimate solution, but to say the least, this is quite a lot of work for something as simple as centering a div.
This should take care of your center div, with out having on the extra styles.
.your_div{
width: 500px;
height: auto;
margin: 0px auto; <!--this code is all your need to really center the div-->
}
Benga creative
Really Good and Useful Tips for Css developer
Post new comment