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标记即可访问该元素。

更多相关文章

  1. TextView显示html样式的文字
  2. 使活动选项卡在模式下不起作用
  3. 单选按钮和多选按钮的样式设置
  4. 在刷新之后保持选择的选项
  5. 工作中遇到的几个CSS样式表的问题
  6. php 把驼峰样式的字符串转换成下划线样式的字符串
  7. 一些Apache、Php和Mysql的优化配置选项
  8. 来自php表单的样式电子邮件
  9. php怎么读取MYSQL数据到radio选项

随机推荐

  1. android LinearLayout布局子空间没有填充
  2. android 获取控件真实高度
  3. Android多国语言文件夹命名
  4. Android(安卓)项目无法生成R文件解决办法
  5. Android当中的SeekBar与iOS中的UISlider
  6. 在android jni中使用log
  7. Android:时间控件
  8. android各个文件分析
  9. 创建Android SD卡遇到的问题
  10. CyanogenMod | Android Community Rom ba