CSS:对齐偶数元素

标签 css

Here is my layout .我有一个 508px 宽的容器,里面有 div。我想将左列(奇数 div)向左对齐,将右列向右对齐。我尝试了 nth-child(even) - 但它没有调整右边距。一-为什么它不起作用?二 - 是否有更优雅的解决方案?

CSS

.clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; }  
.clearfix:after { clear: both; }  
.clearfix { zoom: 1; } 


#products {width:508px;background-color:#FFC;}
#products > div {
    float:left;
    margin: 0 30px 30px 0;
    text-align: center;
}
#products > div:nth-child(even) {margin: 0 0 30px 0;background-color:#CCC;}
#products a {font-weight:normal;}
#products img {
    width:224px;
    height:120px;
}

HTML

<div id="products" class="clearfix">
        <div>
            <div><a href=""><img src="" alt=""></a></div>
            <div><a href="">Product 1</a></div>
        </div>
        <div>
            <div><a href=""><img src="" alt=""></a></div>
            <div><a href="">Product 2</a></div>
        </div>
        <div>
            <div><a href=""><img src="" alt=""></a></div>
            <div><a href="">Product 3</a></div>
        </div>
        <div>
            <div><a href=""><img src="" alt=""></a></div>
            <div><a href="">Product 4</a></div>
        </div>
        <div>
            <div><a href=""><img src="" alt=""></a></div>
            <div><a href="">Product 5</a></div>
        </div>                    
    </div>

最佳答案

你试过让偶数 div 正确 float 吗?

像这样:

#products > div:nth-child(even) {margin: 0 0 30px 0;background-color:#CCC; float: right;}

Fiddle

关于CSS:对齐偶数元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17240113/

相关文章:

javascript - 使 Logo 在滚动时缩小到导航栏中

css - 媒体查询样式被默认样式覆盖

html - 最后一个 child 不工作 css/html

html - CSS避免绝对位置div的透明背景

css - 将图像缩小 57% 并在 css 结构中居中

jquery - CSS布局: 2 column fixed-fluid (again)

html - 返回 : Full Screen Background Image

css - 单元格边框宽度随内容自动增加

css - 扩展功能不适用于嵌套选择器

MS CRM 2011 Web 资源中的 CSS 转换偏斜