样式通常以相同的组合出现:创建单个类还是组合元素?
In one of my recent projects, I noticed that certain styles occur in the same combination repeatedly. According to the DRY principle, I should combine these styles. Regarding a good CSS style, what option is better/the best?
在我最近的一个项目中,我注意到某些样式反复出现在同一个组合中。根据DRY原则,我应该结合这些风格。关于一个好的CSS风格,哪个选项更好/最好?
Option 1
Creating a class that contains these styles and simply add it in the HTML to the according elements.
选项1创建一个包含这些样式的类,只需将其添加到HTML中即可。
Example
例
In the HTML:
在HTML中:
<a href='#' class="myClass">Link</a>
or
要么
<ul class="myClass">
<li>Item</li>
<ul>
In the CSS:
在CSS中:
.myClass {
font-weight: bold;
font-size: 14px;
color: grey;
}
Option 2
Simply combining all elements that need that style in my CSS, like in the following example.
选项2只需在CSS中组合需要该样式的所有元素,如下例所示。
a,
ul {
font-weight: bold;
font-size: 14px;
color: grey;
}
2 个解决方案
#1
2
.myClass {
font-weight: bold;
font-size: 14px;
color: grey;
}
is best ...
是最好的 ...
.myclass {
font-weight: bold;
font-size: 14px;
color: grey;
}
is even better (lower-case)
甚至更好(小写)
This way, the DOM engine will get to the element without having to stack across all a and all ul tags in your document.
这样,DOM引擎将无需堆叠文档中的所有和所有ul标记即可访问该元素。
更多相关文章
- TextView显示html样式的文字
- 使活动选项卡在模式下不起作用
- 单选按钮和多选按钮的样式设置
- 在刷新之后保持选择的选项
- 工作中遇到的几个CSS样式表的问题
- php 把驼峰样式的字符串转换成下划线样式的字符串
- 一些Apache、Php和Mysql的优化配置选项
- 来自php表单的样式电子邮件
- php怎么读取MYSQL数据到radio选项