tobymackenzie
tobymackenzie

Reputation: 818

css: horizontally center floated image gallery

I have a gallery-like list of block items with text content. The text content is aligned to the bottom with using display: table* stuff. The list is not fixed width, as it expands to the viewport width by creating more columns. I want to center the list on the screen, such that the bottom row will still be aligned with the left of all other rows (one reason I couldn't figure out how to use inline-block). Example:

<!doctype html>
<html>
    <head>
        <title>test</title>
        <style>
            ul{
                margin: 0;
                padding: 0;
                list-style: none;
            }
            ul:after{
                display: block;
                clear: both;
                content: ' ';
            }
            li{
                display: table;
                float: left;
                border: 1px solid red;
                width: 300px;
                height: 300px;
            }
            .gridRow{
                display: table-row;
            }
            .gridCell{
                display: table-cell;
                vertical-align: bottom;
            }
        </style>
    </head>
    <body>
        <ul>
            <li><span class="gridRow"><span class="gridCell">1</span></span></li>
            <li><span class="gridRow"><span class="gridCell">2</span></span></li>
            <li><span class="gridRow"><span class="gridCell">3</span></span></li>
            <li><span class="gridRow"><span class="gridCell">4</span></span></li>
            <li><span class="gridRow"><span class="gridCell">5</span></span></li>
            <li><span class="gridRow"><span class="gridCell">6</span></span></li>
            <li><span class="gridRow"><span class="gridCell">7</span></span></li>
            <li><span class="gridRow"><span class="gridCell">8</span></span></li>
            <li><span class="gridRow"><span class="gridCell">9</span></span></li>
            <li><span class="gridRow"><span class="gridCell">10</span></span></li>
            <li><span class="gridRow"><span class="gridCell">11</span></span></li>
            <li><span class="gridRow"><span class="gridCell">12</span></span></li>
            <li><span class="gridRow"><span class="gridCell">13</span></span></li>
            <li><span class="gridRow"><span class="gridCell">14</span></span></li>
            <li><span class="gridRow"><span class="gridCell">15</span></span></li>
            <li><span class="gridRow"><span class="gridCell">16</span></span></li>
            <li><span class="gridRow"><span class="gridCell">17</span></span></li>
            <li><span class="gridRow"><span class="gridCell">18</span></span></li>
            <li><span class="gridRow"><span class="gridCell">19</span></span></li>
        </ul>
    </body>
</html>

Upvotes: 2

Views: 974

Answers (1)

Billy Moat
Billy Moat

Reputation: 21050

You need to give the UL a width or put it in a containing element with a width and "overflow:auto;".

The width can be in px or in %.

Then give the container a margin of "margin:0 auto;"

Then you simply set your LI elements as "display:inline-block;" or give them "float:left;"

Example: http://jsfiddle.net/jDqbJ/1/

.container {
 width:75%;
 overflow:auto;
 margin:0 auto;
}

.container ul li {
 float:left;width:75px;
 height:75px;
 background:#ccc;
 margin:0 10px 20px 0;
}

Upvotes: 1

Related Questions