使用css或sass将样式应用于同一类的多个html元素

时间:2022-12-12 20:18:33

I want to apply a style to all <p> and <input> elements of the numeric class using css.

我想使用css将样式应用于数字类的所有

元素。

Is it possible to consolidate this so that I only write "numeric" once?

是否有可能巩固这一点,以便我只写一次“数字”?

p.numeric,input.numeric {
    float: right;
}

I'm also using sass, so if it's not possible in CSS is it possible with the sass additions?

我也在使用sass,所以如果在CSS中不可能,那么增加sass是否可能?

2 个解决方案

#1


24  

yes it is possible:

对的,这是可能的:

p, input {
    &.numeric {
        float: right;
    }
}

The '&' is necceassry to connect with p/input. Without the result will be p .numeric {...}

'&'是与p / input连接的必要条件。没有结果将是p .numeric {...}

#2


1  

You could simply do .numeric, but then it would apply to everything with a class of numeric. If you only want it to apply to paragraphs and inputs then what you're doing is the correct approach.

你可以简单地做.numeric,但它会适用于一类数字的所有东西。如果您只想将它​​应用于段落和输入,那么您正在做的是正确的方法。

#1


24  

yes it is possible:

对的,这是可能的:

p, input {
    &.numeric {
        float: right;
    }
}

The '&' is necceassry to connect with p/input. Without the result will be p .numeric {...}

'&'是与p / input连接的必要条件。没有结果将是p .numeric {...}

#2


1  

You could simply do .numeric, but then it would apply to everything with a class of numeric. If you only want it to apply to paragraphs and inputs then what you're doing is the correct approach.

你可以简单地做.numeric,但它会适用于一类数字的所有东西。如果您只想将它​​应用于段落和输入,那么您正在做的是正确的方法。