class 是类选择器, 允许以一种独立于文档元素的方式来指定样式; 会选取带有指定类 (class) 的元素来指定样式.
class 选择器可以单独使用, 也可以与其他元素结合使用.
在 CSS 中, 类选择器以一个点号显示:
.center {text-align: center}
在使用类选择器之前, 需要修改具体的文档标记, 以便类选择器正常工作.
为了将类选择器的样式与元素关联, 必须将 class 指定为一个适当的值. 请看下面的 html 代码:
- <h1 class="important">
- This heading is very important.
- </h1>
- <p class="important">
- This paragraph is very important.
- </p>
在上面的代码中, 两个元素的 class 都指定为 important: 第一个是标题( h1 元素), 第二个是段落(p 元素).
然后我们使用以下语法向这些归类的元素应用样式, 即类名前有一个点号(.), 然后结合通配选择器:
*.important {color:red;}
如果您只想选择所有类名相同的元素, 可以在类选择器中忽略通配选择器, 这没有任何不好的影响:
.important {color:red;}
扩展资料
class 值中包含一个词的情况. 在 HTML 中, 一个 class 值中可能包含一个词列表, 各个词之间用空格分隔.
例如: 如果希望将一个特定的元素同时标记为重要 (important) 和警告(warning), 就可以写作:
<p class="important warning"> This paragraph is a very important warning. </p>
这两个词的顺序无关紧要, 写成 warning important 也可以.
假设 class 为 important 的所有元素都是粗体, 而 class 为 warning 的所有元素为斜体, class 中同时包含 important 和 warning 的所有元素还有一个银色的背景 .
就可以写作:
- .important {
- font-weight:bold;
- }
- .warning {
- font-weight:italic;
- }
- .important.warning {
- background:silver;
- }
通过把两个类选择器链接在一起, 仅可以选择同时包含这些类名的元素(类名的顺序不限).
更多 web 前端开发 https://www.html.cn/ 知识, 请查阅 HTML 中文网 !!
来源: http://www.css88.com/qa/css3/17415.html