小白教程
所有教程
关于
Search
172.70.126.92
172.70.126.92
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“CSS Id 和 Class选择器”的源代码
本页内容
上一节:
CSS_语法
下一节:
CSS_创建
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:CSS id 和 class选择器}}[[Category:CSS 教程|4]] = CSS id 和 class = == id 和 class 选择器 == 如果你要在HTML元素中设置CSS样式,你可以为元素设置"id" 和 "class"选择器。 == id 选择器 == id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式。 HTML元素以id属性来设置id选择器,CSS 中 id 选择器以 "#" 来定义。 以下的样式规则应用于元素属性 id="para1": <sample title="" desc="" lang="html" hererun="1"> #para1 { text-align:center; color:red; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> #para1 { text-align:center; color:red; } </style> </head> <body> <p id="para1">Hello World!</p> <p>这个段落不受该样式的影响。</p> </body> </html> </run> ID属性不要以数字开头,数字开头的ID在 Mozilla/Firefox 浏览器中不起作用。 == class 选择器 == class 选择器用于描述一组元素的样式,class 选择器有别于id选择器,class可以在多个元素中使用。 class 选择器在 HTML 中以 class 属性表示, 在 CSS 中,类选择器以一个点 . 号显示: 在以下的例子中,所有拥有 center 类的 HTML 元素均为居中。 <sample title="" desc="" lang="html" hererun="1"> .center {text-align:center;} </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> .center { text-align:center; } </style> </head> <body> <h1 class="center">标题居中</h1> <p class="center">段落居中。</p> </body> </html> </run> 你也可以指定特定的 HTML 元素使用 class。 在以下实例中, 所有的 p 元素使用 class="center" 让该元素的文本居中: <sample title="" desc="" lang="html" hererun="1"> p.center {text-align:center;} </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> p.center { text-align:center; } </style> </head> <body> <h1 class="center">这个标题不受影响</h1> <p class="center">这个段落居中对齐。</p> </body> </html> </run> 多个 class 选择器可以使用空格分开: <sample title="" desc="" lang="html" hererun="1"> .center { text-align:center; } .color { color:#ff0000; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> .center { text-align:center; } .color { color:#ff0000; } </style> </head> <body> <h1 class="center">标题居中</h1> <p class="center color">段落居中,颜色为红色。</p> </body> </html> </run> 类名的第一个字符不能使用数字!它无法在 Mozilla 或 Firefox 中起作用。
返回至“
CSS Id 和 Class选择器
”。
上一节:
CSS_语法
下一节:
CSS_创建