tags:

views:

39

answers:

2

Consider the following HTML and CSS. The desired result is the two pink boxes are centered within the single green box. The text is NOT centered. There is padding on the left and right of each box. I should be able to have 1 or more of these boxes, up to as many as 8; they should all float to fill in the container div. I've been bangin my head on this one for weeks now, and nothing I read or try seems to work - margin: auto, display:inline, etc etc. I added the box-margin div to create the padding when trying to use margin:auto, but it still didn't get the result I was looking for. Halp?

HTML

<body>
    <div id="page">

        <div id="main">

            <div class="box-group">

                <div class="box-margin">
                    <div class="box">
                        <h2>Data</h2>
                        <ul>
                            <li><a href="/URL">Link</a></li>
                        </ul>
                    </div>
                </div>

                <div class="box-margin">
                    <div class="box">
                        <h2>Reports</h2>
                        <ul>
                            <li><a href="/URL/">Link</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

CSS

body
{
    margin: 0;
    padding: 0;
}
#page
{
    width: 95%;
    min-width:800px;
    border: thin solid red;
}
#main
{
    padding: 30px 30px 15px 30px;
    margin-bottom: 30px;
    min-width: 875px;
    border: thin solid black;
}
.box-group
{
    width: 100%;
    padding: 10px;
    border: thin solid green;
}
.box-margin
{
    margin: auto;
    width: 275px;
    float: left;
    padding: 15px;
}
.box
{
    border: thin ridge pink;
    height: 200px;
    width: 250px;
}      
A: 

Floats will always stick to the left or right edge of their container. You should use display: inline-block for this.

Andrew Vit
A: 

Have a look at this page on centered floats. It has a neat trick that might not work for you (it will center them as a group, but not per-line, if they wrap), but it also details a fix for display: inline-block in IE.

Tim Yates
That's really funny Tim: "Here's a solution that won't work for you." Ha! Thanks, I'll take a look at it :)
morganpdx
Haha well your question made me curious, since I've run into this problem before. I found that page and thought it looked great, but then I saw the caveat. It still has lots of useful information though.
Tim Yates
I tried it and it made the page turn out ALL SORTS of crazy. Either I'm doin it wrong or it's not working. Although from the level of crazy, I'm guessing I might be doing it wrong...
morganpdx
I ended up having to make a few more classes: three-box, two-box-left and two-box-right. Ah well. Seriously though, it what is the deal? Being able to center some divs inside another container div should be basic, no?
morganpdx
It should be, but as appealing as floats are, I guess they just weren't designed for this. Did you try alternatives, like the inline-block method?
Tim Yates
Yep. inline block doesn't seem to work, although I'm not quite sure why. Perhaps I did it wrong?
morganpdx