tags:

views:

1040

answers:

3

Basically I have four divs contained in another div, and I want the four divs to be centered.

I have used float: left on the four divs so that they are horizontally next to each other.

CSS:

<style>
    .square
    {
     float: left;
     margin:1pt;
     width:72pt;
     height:72pt;
    }
    .container
    {
     text-align:center;
     width:450pt;
     height: 80pt;
    }
</style>

and HTML:

<div class = "container">
    <div class = "square">...</div>
    <div class = "square">...</div>
    <div class = "square">...</div>
    <div class = "square">...</div>
</div>

How can I center the four divs inside the container?

EDIT: The number of div inside the container may not always be 4, it could be 3 or 2 (There is actually some php around that code...).

+3  A: 

Here's an alternate method if you can use an extra div:

<div class = "container">
  <div class="centerwrapper">
    <div class = "square">...</div>
    <div class = "square">...</div>
    <div class = "square">...</div>
    <div class = "square">...</div>
  </div>
</div>

<style>
    .square
    {
        float: left;
        margin:1pt;
        width:72pt;
        height:72pt;
    }
    .container
    {
        text-align:center;
        width:450pt;
        height: 80pt;
    }
    .centerwrapper
    {
       margin: auto;
       width: 302pt;
    }
</style>

Also, make sure you have a closing quote on your <div class = "container"> there. The code you pasted is missing one.

womp
This works for 4 divs, but the number of divs in the container may not always be the same. In my case, sometimes only 2 or 3 divs may appear, and I want them to be centered as well. I forgot to specify this in my question...
julienln
Hmm. It might be better if you can get away with displaying your squares inline with a given width, instead of floating them. If you can use margin: auto; in a surrounding div, then they will always be centered - the trick is getting away from specifying the width of it.
womp
Ok, I managed to make it work by defining the width of the centerwrapper like this, instead of using a css class: <div style = "margin:auto; width:<?php echo 74*$nbDiv ?>pt">
julienln
+1  A: 

Instead of floating the .square divs, give them display: inline-block. This might be dodgy in Firefox 3.0.x but I believe inline-block is fully supported in 3.5.x.

RwL
You'll need to put in a special rule for IE6/7 I believe as well.
womp
RwL
With a bit of extra CSS you can make this happen without changing the markup. Here's the code I use (thanks to several others) that works on IE6+ and all the other modern browsers:.inline-block { display: -moz-inline-box; -moz-box-orient: vertical; display: inline-block; vertical-align: middle; #display: inline; /* fixes alignment against native input/button on IE6 */ #vertical-align: auto;}If you are like me and no fan of hacks, you can take those last two properties out and move them into an IE stylesheet called by conditional comments (and remove the hash tags).
Eric Meyer
A: 

[Ask SM: CSS] Equal Spacing, CSS Font Replacement

This Link should be helpful.

Virat Kadaru