Forum Moderators: not2easy

Message Too Old, No Replies

Centering in a DIV

How to center elements

         

MissTrish

6:07 pm on May 14, 2005 (gmt 0)

10+ Year Member



I have another open post, but I thought it best to put this one in its own.

I have a parent DIV that is 780 pixels wide. It contains three other DIV's (columns). For whatever reason, I cannot get them to center across the parent DIV. I used the "float: left;" property and set margins, but IE and Mozilla render the margins differently. Is there a way to center these columns within the parent DIV?

fischermx

6:37 pm on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put this in the top of your page to get same margins in both IE and Mozilla.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

MissTrish

6:53 pm on May 14, 2005 (gmt 0)

10+ Year Member



I did as you suggested and there was no change. Mozilla and Firefox display the margins as specified, but IE widens the left margin of the first column. This is the same problem I was having before; that's why I was asking if there was a way to center the columns in the parent DIV.

fischermx

7:56 pm on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Show your CSS and HTML code, we could help a bit more.

Stormfx

4:58 am on May 15, 2005 (gmt 0)

10+ Year Member



You can use padding/margin and width attributes to get them aligned the way you need them to be. Then you can align the contents of each div from there.

Robin_reala

10:36 am on May 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you floating both the container and the inner block left? If you it's the infamous double margin bug. Try setting display:inline; on the leftmost inner element.

createErrorMsg

9:06 pm on May 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice one, Robin. If that is, indeed, the problem, you deserve a beer for catching the bug without any code. :)

If it's not the bug Robin mentioned (and even if it is), it could be the approach to centering that is causing the problem...

I have a parent DIV that is 780 pixels wide. It contains three other DIV's (columns). For whatever reason, I cannot get them to center across the parent DIV.

By it's very nature, centering three floated divs in a container is not possible. Since the divs float in one direction or the other, you will embroil yourself in a nightmare of tweak'n'peak margin and padding adjustments to get them into line, only to have IE smash up the whole thing on a browser resize.

Since the boxes all float to one side or the other, you can't center them. But if it were ONE unfloated box, you could definitely center THAT.

If you put your floats into a wrapper div and then center the wrapper div, it will result in centered floats...

html:
<div id="your_container">
<div id="my_wrapper">
<div id="left"></div>
<div id="center"></div>
<div id="right"></div>
</div>
</div>

css:
#your_container{
float:left;
width:100%;
background:blue;
text-align:center; /*centers the wrapper in early IE versions that don't support auto margins*/
}
#my_wrapper{
width:600px; /*necessary for auto margin centering; equals total width of all children*/
margin:0 auto; /*centers in compliant browsers*/
}
#left, #center, #right{
float:left;
width:200px;
}

You could, of course, do this with the existing parent container, but I'm assuming it is floated in order to contain the floated children, perhaps has a background or a border or something that needs to enclose the floats. By adding an element specifically for centering, you don't have to worry about all of that. Because #my_wrapper doesn't have any border or backgrounds that we need to have enclose around the floats, it does not need to be floated. And because it does not need to be floated, we can easily center it.

cEM