css 中的class是什么意思!

html-css013

css 中的class是什么意思!,第1张

class是类选择器,允许以一种独立于文档元素的方式来指定样式。

在使用类选择器之前,需要修改具体的文档标记,以便类选择器正常工作。

为了将类选择器的样式与元素关联,必须将 class 指定为一个适当的值。请看下面的 HTML 代码:

<h1 class="important">This heading is very important. </h1><p class="important">This paragraph is very important. </p>

在上面的代码中,两个元素的 class 都指定为 important:第一个标题( h1 元素),第二个段落(p 元素)。

扩展资料

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} 通过把两个类选择器链接在一起,仅可以选择同时包含这些类名的元素(类名的顺序不限)。

参考资料来源:百度百科-类选择器

先看下面的html,两个p 一个div ,都定义了style,设置字体大小,颜色等,这三者的样式都一样,假若所有的html标签都这么写,是不是很麻烦?而且html文档很长,影响下载的效率。

<p style="font-size:20pxcolor:#333">this is a p!</p>

<p style="font-size:20pxcolor:#333">this is another p!</p>

<div style="font-size:20pxcolor:#333">this is another div!</div>

于是,css出现了:

如下.font定义了个类。

<style type="text/css">

.font{

font-size:20px

color:#333

}

</style>

于是就可以如下定义样式,和上面的样式的结果相同。

<p class="font">this is a p!</p>

<p class="font">this is another p!</p>

<div class="font">this is another div!</div>

这样更加简介有效率。这就是所谓的“类”,拿出共性。