How to combine multiple selectors for the same rule [duplicate]

白昼怎懂夜的黑 提交于 2021-02-10 14:36:31

问题


I can't combine these selectors into a single CSS rule. I'm trying to edit the width of certain form fields.

input#input_9_2 {
  max-width: 500px;
}

input#input_9_3 {
  max-width: 500px;
}

When I try to combine them, the rule does not apply to either. But they work when written separately, as above.


回答1:


To group CSS selectors in a style sheet, you use commas to separate multiple grouped selectors in the style. In this example, the style affects two classes input#input_9_2 and input#input_9_3.

input#input_9_2,
input#input_9_3
{
  max-width: 500px;
}

The comma means "and", so this selector applies to all input#input_9_2 elements and input#input_9_3 elements. If the comma were missing, the selector would instead apply to all input#input_9_3 elements that are a child of an input#input_9_2. That is a different kind of selector, so the comma is important.

Any form of the selector can be grouped with any other selector.




回答2:


use a selector list

input#input_9_2, input#input_9_3 {
  max-width: 500px;
}


来源:https://stackoverflow.com/questions/57035939/how-to-combine-multiple-selectors-for-the-same-rule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!