Multiple centered floating divs in a liquid layout
Asked Answered
B

4

6

I have an idea for a layout I would like to use, but I can't get it to work correctly. I am hoping someone on here might be able to help as I have spent hours searching.

The layout is like so.

One wrapper div houses 6 child divs. Those child divs must be centered at ALL times regardless of the size of the wrapper div.

<html>
<head>
<title>Testing</title>
<style>
br.clear { clear:both; display:block; height:1px; margin:-1px 0 0 0; }
#wrapper { max-width: 960px; min-width: 320px; background: #444; margin: 0 auto; }
.box { width: 280px; padding: 10px; margin:10px; height: 260px; border: 0px; float: left; background: #fff; }
</style>
</head>
<body>

<div id="wrapper">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <br class="clear" />
</div>

</body>
</html>

The problem is when the browser is resized smaller and a box is knocked onto the line below the boxes will alight left, whereas I want them to be always centered. Is this possible using pure css or do I need to use some jquery?

Burck answered 12/10, 2010 at 9:53 Comment(1)
They are never centered for me using your code?Alkalimeter
B
6

Probably the easiest solution is if you remove the float: left style from the boxes and change the display property to inline-block. Then all you need to do is to text-align: center on the wrapper.

<html>
<head>
<title>Testing</title>
<style>
br.clear { clear:both; display:block; height:1px; margin:-1px 0 0 0; }
#wrapper { max-width: 960px; min-width: 320px; background: #444; margin: 0 auto; text-align:center }
.box { width: 280px; padding: 10px; margin:10px; height: 260px; border: 0px; background: #fff; display:inline-block }
</style>
</head>
<body>

<div id="wrapper">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <br class="clear" />
</div>

</body>

You can test the code here: http://jsbin.com/uqamu4/edit

Bier answered 12/10, 2010 at 10:3 Comment(2)
Hi Viktor, The problem with that is the boxes dont wrap. I want this display to work in any device. Eg, normal laptop will probably show 3 boxes side by side, but an iphone would just show one on top of the other. But if someone changes the viewport so it knocks a box to the next line it should still center all the boxesBurck
Actually, ignore me. Looks like it does work but only shows 2 side by side at the moment which I can change. Cheers :)Burck
A
3

You could use text-align: center in the wrapper and display: inline-block for the boxes to make them behave like normal text elements that are centered no matter what.

Caveat: Doesn't work in IE6 and IE 7. Works in Chrome and FF

JSFiddle here.

Alkalimeter answered 12/10, 2010 at 10:5 Comment(0)
T
0

This wont work in ie 8 or less, dont know about 9, but since your using max-width and min-width which dont work there either I'll post it anyway.

<html>
<head>
<title>Testing</title>
<style>
br.clear { clear:both; display:block; height:1px; margin:-1px 0 0 0; }
#wrapper { max-width: 960px; min-width: 320px; background: #444; margin: 0 auto; text-align:center; }
.box { width: 280px; padding: 10px; margin:8px; height: 260px; border: 0px; background: #fff; display:inline-block;}

</style>
</head>
<body>

<div id="wrapper">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <br class="clear" />
</div>

</body>
</html>
Titos answered 12/10, 2010 at 10:5 Comment(0)
W
0

The solution that worked for me:

<style>
    body {
        /* To center the list */
        text-align: center;
    }

    #wrapper {
        /* To reset 'text-align' to the default */
        text-align: left;

        display: inline;
    }

    .box {
        display: inline-block;
    }
</style>
Writer answered 13/7, 2014 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.