css 中的class是什么意思!

html-css010

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

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

id:指定标签的唯一标识,定位到页面上唯一的元素。

使用场景: 使用#name定义(name为id名,可任意取名),使用id="name"调用,其优先级高于类选择器,一个标签只能有一个id且每个id只能使用一次,多用于页面分块的块级标签上。

class :类选择器,指定标签的类名,定位到页面上某一类的元素 。

使用场景:使用.name定义(name为类名,可任意取名),使用class="name"调用,一个标签可以有多个类且同一个类可以用到不同的标签上,多用于多个标签样式相似或完全相同时。

* {} /* a=0 b=0 c=0 d=1 ->0,0,0,0 */

p {} /* a=0 b=0 c=0 d=1 ->0,0,0,1 */

a:hover {} /* a=0 b=0 c=1 d=1 ->0,0,1,1 */

ul li {} /* a=0 b=0 c=0 d=2 ->0,0,0,2 */

ul ol+li {} /* a=0 b=0 c=0 d=3 ->0,0,0,3 */

h1+input[type=hidden]{} /* a=0 b=0 c=1 d=2 ->0,0,1,1 */

ul ol li.active {} /* a=0 b=0 c=1 d=3 ->0,0,1,3 */

#ct .box p {} /* a=0 b=1 c=1 d=1 ->0,1,1,1 */

div#header:after {} /* a=0 b=1 c=0 d=2 ->0,1,0,2 */

style="" /* a=1 b=0 c=0 d=0 ->1,0,0,0 */

先比较a,a大的权重最大,再依次比较b,c,d

#header{ }

id选择器,匹配特定id为header的元素

.header{ }

类选择器,匹配class包含header的元素

.header .logo{ }

后代选择器,匹配class为header元素所有的后代(不只是子元素、子元素向下递归)

calss为logo的元素

.header.mobile{ }

匹配class既有header又有logo的元素

.header p, .header h3{ }

同时匹配class为header所有后代元素p和class为header所有后代元素h3

#header .nav>li{ }

匹配id为header元素所有后代中calss为nav的所有直接子元素li

#header a:hover{ }

匹配id为header所有后代中鼠标悬停其上的a元素

#header .logo~p{ }

匹配id为header所有后代中class为logo之后的同级元素p(无论直接相邻与否)

#header input[type="text"]{ }

匹配id为header所有后代中属性type值为text的元素

n的取值

1,2,3,4,5

2n+1, 2n, 4n-1

odd, even

<style>.item1:first-child{ color: red} .item1:first-of-type{ background: blue} </style><div class="ct"><p class="item1">aa</p><h3 class="item1">bb</h3><h3 class="item1">ccc</h3></div>