如何使用相同的类来定位输入和select元素

时间:2012-06-18 08:32:32

标签: css grouping

我怎么写这个,所以它针对两个元素而不写两次

.field_with_errors input{
    color: red;
    background: #CEF6EC;
    border: 1px solid red;
}


.field_with_errors select{
    color: red;
    background: #CEF6EC;
    border: 1px solid red;
}

这不起作用

.field_with_errors select input{
    color: red;
    background: #CEF6EC;
    border: 1px solid red;
}

2 个答案:

答案 0 :(得分:4)

.field_with_errors select,
.field_with_errors input {
    color: red;
    background: #CEF6EC;
    border: 1px solid red;
}

必须用逗号分隔多个css选择器。如上所述,您必须为每个选择器重复整个选择器。

另外,通常的做法是在每个逗号后面换行,以保持你的css更具可读性 - 它不会影响结果,所以以下意思相同:

.field_with_errors select,
.field_with_errors input {

.field_with_errors select, .field_with_errors input {

答案 1 :(得分:1)

.field_with_errors input,.field_with_errors select{
    color: red;
    background: #CEF6EC;
    border: 1px solid red;
}