css:水平中心浮动图像库

时间:2012-11-21 15:16:25

标签: css

我有一个类似画廊的块项目列表,其中包含文本内容。使用display:table * stuff将文本内容与底部对齐。该列表不是固定宽度,因为它通过创建更多列扩展到视口宽度。我想将列表居中放在屏幕上,这样底行仍然会与所有其他行的左边对齐(一个原因我无法弄清楚如何使用内联块)。例如:

<!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>

1 个答案:

答案 0 :(得分:1)

你需要给UL一个宽度或者把它放在一个宽度为“overflow:auto;”的包含元素中。

宽度可以是px或%。

然后给容器一个“margin:0 auto;”

的边距

然后,您只需将LI元素设置为“display:inline-block;”或者给他们“浮动:离开;”

示例: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;
}