我使用两种方式,但我更喜欢第一种方式,因为可以重用代码。有些人喜欢一个或另一个。但我想知道大型程式化页面的负载是否在某种程度上比另一种更好。
1)
<style>
.red{ color:red}
.right25{ margin-right: 25px}
.bold{ font-weight:bold}
</style>
<p class="red right25 bold">this is a red text, bold and right margin 25px</p>
2)
<style>
.class1{ color:red; margin-right: 25px; font-weight:bold}
</style>
<p class="class1">this is a red text, bold and right margin 25px</p>
最佳答案
这取决于您实际可以重用多少代码,但性能差异不会那么大。
这两种方法之间最大的区别在于类的含义。一般来说,你应该有代表你试图展示什么的类,而不是你用来展示它的确切样式。当您进行更改时,代表意图而不是实现的名称会更好。
例如,如果您有类 right25
并且想将页边距更改为 20 像素,那么您将有一个不代表它实际作用的类名,或者您有在您使用它的任何地方将其更改为 right20
。
关于css - 什么更快 : more classes or unique class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25980115/