我可以让这个 CSS 更简单以避免重复父选择器吗?
我遇到的一个常见模式如下:
form.request input {
/* ... */
}
form.request input[type="text"] {
/* ... */
}
form.request select {
/* ... */
}
form.request br {
/* ... */
}
我有几行以相同的选择器 (form.request
) 开头,并且我想选择不同的子项。我可以以一种更简洁的方式做到这一点,而不需要重复(最好没有像 LESS 这样的额外依赖项)?
相关问题 - 如果以上所有评论都包含相同的样式,我可以做得更好吗:
form.request input, form.request input[type="text"], form.request select, form.request br {
/* ... */
}
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 {
/* ... */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,你不能。但如果您想减少打字并使样式表更具可读性,请考虑使用 SCSS。
No, you cannot. But if you want to do less typing and make the stylesheets more readable, consider using SCSS.
使用LESS,这可以让你写
,甚至
Use LESS, which would let you write
or even
还没有 - 或者没有额外的依赖项。
事实上,有一个 CSS 嵌套模块的 W3C 工作草案 - 然而,< a href="https://caniuse.com/css-nesting" rel="nofollow noreferrer">尚无浏览器支持此功能。
如果您愿意使用 PostCSS,这里有一个 可用插件,因此您现在就可以开始使用此功能 - 该插件将扩展嵌套选择器(类似于 SASS或 LESS)到当今浏览器中工作的重复选择器中。
PostCSS 使许多其他 CSS 功能能够在旧版浏览器中工作。使用未来 CSS 功能的风险在于它们的最终形式可能会有所不同 - 或者就这一点而言,可能永远不会实现。另一方面,使用不断发展的标准,如果它最终由浏览器实现,那么您很可能比使用 SASS 或 LESS 更接近最终形式的功能。
请注意,语法与 SASS 或 LESS 相似但不同。
您可能还需要考虑 IDE 支持的问题。例如,有一个 VS Code 插件可用,它确实支持当前形式的 CSS 嵌套模块 - 但是,根据您选择的 IDE,PostCSS 插件可能可用也可能不可用,并且可能支持也可能不支持某些功能。
Not yet - or not without additional dependencies.
There is in fact a W3C working draft for a CSS Nesting Module - however, no browser supports this yet.
If you're willing to use PostCSS, there is a plugin available, so you can start using this feature now - the plugin will expand nested selectors (similarly to SASS or LESS) into repeated selectors that work in browsers today.
PostCSS enables many other CSS features to work in older browsers. The risk of using future CSS features, is that they may end up being different in their final form - or, for that matter, might never be implemented. On the other hand, using an evolving standard, if it is eventually implemented by browsers, you are most likely closer to something resembling the feature in it's final form than you would be with e.g. SASS or LESS.
Note that the syntax is similar but different from that of SASS or LESS.
You also might want to consider the issue of IDE support. For instance, there is a VS Code plugin available, and it does support the CSS Nesting Module in it's current form - however, depending on your IDE of choice, a PostCSS plugin may or may not be available, and may or may not support certain features.