Can I make this CSS simpler to avoid repeating the parent selector?

百般思念 提交于 2020-01-10 06:03:10

问题


A common pattern I come across is the following:

form.request input {
    /* ... */
}

form.request input[type="text"] {
    /* ... */
}

form.request select {
    /* ... */
}

form.request br {
    /* ... */
}

I have several lines beginning with the same selector (form.request), and I want to select various children. Can I do this in a neater way without the repetition (and preferably without additional dependencies like LESS)?

Related question - if all the above comments contain the same styles, can I do better than:

form.request input, form.request input[type="text"], form.request select, form.request br {
    /* ... */
}

回答1:


No, you cannot. But if you want to do less typing and make the stylesheets more readable, consider using SCSS.




回答2:


Use LESS, which would let you write

form.request {
  input { /* ... */ }
  input[type="text"] { /* ... */ }
  select { /* ... */ }
  br { /* ... */ }
}

or even

form.request {
  input {
    /* ... */
    &[type="text"] { /* ... */ }
  }
  select { /* ... */ }
  br { /* ... */ }
}


来源:https://stackoverflow.com/questions/6894789/can-i-make-this-css-simpler-to-avoid-repeating-the-parent-selector

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