单个元素html上的多个类
Is it a good practice to use many classes on one single HTML element? For example:
在一个HTML元素上使用多个类是一个好习惯吗?例如:
<div class="nav nav-centered nav-reversed navbar navigation-block"></div>
I don't mean that two or three classes on one element is bad, but how about four, five or even six?
我并不是说一个元素上的两个或三个类是坏的,但四个,五个甚至六个怎么样?
2 个解决方案
#1
51
Short Answer
Yes.
是。
Explanation
It is a good practice since an element can be a part of different groups, and you may want specific elements to be a part of more than one group. The element can hold an infinite number of classes in HTML5, while in HTML4 you are limited by a specific length.
这是一个很好的做法,因为元素可以是不同组的一部分,并且您可能希望特定元素成为多个组的一部分。该元素可以在HTML5中保存无限数量的类,而在HTML4中,您受限于特定长度。
The following example will show you the use of multiple classes.
以下示例将向您展示多个类的使用。
The first class makes the text color
red.
第一个类使文本颜色变为红色。
The second class makes the background-color
blue.
第二类使背景颜色变为蓝色。
See how the DOM Element with multiple classes will behave, it will wear both CSS statements at the same time.
看看具有多个类的DOM元素将如何表现,它将同时佩戴两个CSS语句。
Result: multiple CSS statements in different classes will stack up.
结果:不同类中的多个CSS语句将叠加。
You can read more about CSS Specificity.
您可以阅读有关CSS特性的更多信息。
CSS
.class1 {
color:red;
}
.class2 {
background-color:blue;
}
HTML
<div class="class1">text 1</div>
<div class="class2">text 2</div>
<div class="class1 class2">text 3</div>
Live demo
更多相关文章
- 样式通常以相同的组合出现:创建单个类还是组合元素?
- 在href元素中使用标记<c:out [duplicate]
- CSS3列 - 添加第二个元素后的额外宽度
- 垂直对齐div中的两个元素
- 如何用c#获取服务器端元素的innerhtml(内部有另一个服务器端控件)
- 如何让你的前端代码更像HTML5(用语义元素构造html5)
- HTML 元素用来显示已知范围的标量值或者分数值。
- HTML5实战与剖析之媒体元素(1、video标签和audio标签简介)
- 迭代XML元素并将元素推送到数组中