html - 如何2列交替div背景?

标签 html css

<分区>

大家好 Stackoverflow

我一直在四处寻找,似乎无法找到我一直在寻找的答案,如您在此处看到的那样,如何正确交替使用 2 列 div 背景颜色:

enter image description here &:odd 和 &:even 选择器似乎对我不起作用,所以显然它必须是 &:nth-child() 之类的东西,但我的逻辑能力对我不起作用..任何人都有类似的经历也许可以帮助我? :( 图片中的数字表示 div 标 checkout 现的顺序...

最佳答案

使用

  1. :nth-child(4n+2) - 2, 6, 10, 14 ...

  2. :nth-child(4n+3) - 3, 7, 11, 15 ...

.wrap{    
    width: 100px;
    font-size: 0;    
}
.wrap > div{
    font-size: 30px;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    display: inline-block;
    vertical-align: top;  
    background: #fff;   
}
.wrap > div:nth-child(4n+2),
.wrap > div:nth-child(4n+3){    
    background: #F66C9B;
}
<div class="wrap">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
    <div>10</div>
</div>

关于html - 如何2列交替div背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184643/

相关文章:

html - 将动画放在有内容的伪元素上

javascript - HTML 输入类型数字千位分隔符

将鼠标悬停在 Javascript 文本框上

jquery - 获取 CSS 动画流体

html - CSS: Namespace 一段 HTML

HTML float ,100% 高度

css - 使用 Stylus mixin 定义 React CSSTransitionGroup 动画

html - 如何在IE中自定义滚动条

css - 字体粗细不能正常工作?

php - 在 Wordpress 中添加一个带钩子(Hook)的 Google 字体