CSS 条件条纹背景

Conditional striped background with CSS

我有一些 DIV 可以是 5 种颜色中的任何一种,具体取决于 DIV 设置的 class 颜色。如果设置了多个 class ,则优先确定使用哪种颜色。我想更改此设置,以便在设置多个 class 时获得 多个 颜色。

现在,其中一部分是 UI/UX 视角,因为条纹看起来很难看。如果您有任何建议,我会洗耳恭听 -- 但仅凭这一点不会让您接受。

另一部分是如何从技术上做到这一点。我更愿意纯粹在 CSS 中执行此操作,并且我更希望解决方案适用于 DIV 背景颜色,但究竟如何仍然是开放的。可以创建 two-color stripes, and it's possible to create multi-coloured backgrounds,但我不清楚如何以动态方式执行此操作,而不是硬编码示例, 具有...动态性?...通过 元素 classes.

触发

我想你可以使用 http://www.colorzilla.com/gradient-editor/ 来获得类似的东西:

.one{
   background:#ff0000;  
 }

.one.two{
   background: -moz-linear-gradient(left, rgba(255,0,0,1) 0%, rgba(255,0,0,1) 50%, rgba(0,255,0,1) 50%, rgba(0,255,0,1) 50%, rgba(0,255,0,1) 100%); /* FF3.6-15 */
   background: -webkit-linear-gradient(left, rgba(255,0,0,1) 0%,rgba(255,0,0,1) 50%,rgba(0,255,0,1) 50%,rgba(0,255,0,1) 50%,rgba(0,255,0,1) 100%); /* Chrome10-25,Safari5.1-6 */
   background: linear-gradient(to right, rgba(255,0,0,1) 0%,rgba(255,0,0,1) 50%,rgba(0,255,0,1) 50%,rgba(0,255,0,1) 50%,rgba(0,255,0,1) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ff0000', endColorstr='#00ff00',GradientType=1 ); /* IE6-9*/ 
}

.one.two.three{
   background: rgb(255,0,0); /* Old browsers */
   background: -moz-linear-gradient(left, rgba(255,0,0,1) 0%, rgba(255,0,0,1) 33%, rgba(0,255,0,1) 33%, rgba(0,255,0,1) 33%, rgba(0,255,0,1) 66%, rgba(0,0,255,1) 66%, rgba(0,0,255,1) 66%); /* FF3.6-15 */
   background: -webkit-linear-gradient(left, rgba(255,0,0,1) 0%,rgba(255,0,0,1) 33%,rgba(0,255,0,1) 33%,rgba(0,255,0,1) 33%,rgba(0,255,0,1) 66%,rgba(0,0,255,1) 66%,rgba(0,0,255,1) 66%); /* Chrome10-25,Safari5.1-6 */
   background: linear-gradient(to right, rgba(255,0,0,1) 0%,rgba(255,0,0,1) 33%,rgba(0,255,0,1) 33%,rgba(0,255,0,1) 33%,rgba(0,255,0,1) 66%,rgba(0,0,255,1) 66%,rgba(0,0,255,1) 66%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ff0000', endColorstr='#0000ff',GradientType=1 ); /* IE6-9 */  
} 

您可以使用 nth-child 选项在那里您可以指定 1n、2n、3n 等等

.container div {width:100px; color:#ffffff; padding: 5px;}
    .container :nth-child(1n){
        background: #F44336;
    }
    .container :nth-child(2n){
        background: #E91E63;
    }
    .container :nth-child(3n){
        background: #9C27B0;
    }
    .container :nth-child(4n){
        background: #673AB7;
    }
    .container :nth-child(5n){
        background: #009688;
    }
<div class="container">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</div>