CSS中设置背景颜色是十分常见的操作,下面介绍一种比较有规律的方式,即CSS六个一组设置背景颜色。首先,在CSS中,背景颜色的属性是backgroundcolor,它可以设置具体颜色值,如red、gr...
CSS中设置背景颜色是十分常见的操作,下面介绍一种比较有规律的方式,即CSS六个一组设置背景颜色。
首先,在CSS中,背景颜色的属性是background-color,它可以设置具体颜色值,如red、green等,也可以使用十六进制颜色表示方式,如#FF0000、#00FF00等。
/* 从0~5设置红色背景 */
div:nth-of-type(6n+0) {
background-color: red;
}
/* 从1~6设置橙色背景 */
div:nth-of-type(6n+1) {
background-color: orange;
}
/* 从2~7设置黄色背景 */
div:nth-of-type(6n+2) {
background-color: yellow;
}
/* 从3~8设置绿色背景 */
div:nth-of-type(6n+3) {
background-color: green;
}
/* 从4~9设置蓝色背景 */
div:nth-of-type(6n+4) {
background-color: blue;
}
/* 从5~8设置紫色背景 */
div:nth-of-type(6n+5) {
background-color: purple;
} 通过这种方式,可以将不同的元素分组设置相同的背景颜色,使样式代码更加简洁优雅。