CSS选择器

本文深入探讨了CSS中的三种基本选择器:标签名选择器、类选择器和ID选择器,详细阐述了它们的优先级及如何在不同CSS属性中应用,通过实例展示了选择器间的继承特性及优先级反走现象。

几种选择器:3种,标签名选择器,clsss选择器,id选择器。

对于同样的CSS属性,选择器优先级是,id选择器>class选择器>标签名选择器。class+标签名选择器>class选择器。

对于不同的CSS属性,继承。上面优先级反走。


请看例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSS的基本选择器</title>
</head>
<style type="text/css">
h1
{color:blue; font-size:25px; text-decoration:underline;}
em
{ color:red;}

.red
{ color:red; font-size:18px;}

#one
{ font-weight:bold; font-size:36px;}
.two
{ font-weight:lighter; font-size:13px;}

p span
{ color:#00FF00;}
p
{font-weight:normal; font-size:55px;}

.father div
{ background-color:red;
}
.son
{
background-color
:blue;

}
</style>

<body>
<h1>标签名选择器</h1>
<class="red">class选择器</p>
<class="red">还是class选择器</p>
<id="one">id选择器</p>
<p><span>嵌套选择器</span></p>
<h1><em>嵌套选择有继承特性</em></h1>
<id="one" style=" font-weight:lighter; font-size:13px">行内样式>ID选择器</p>
<id="one" class="two">ID选择器>class选择器</p>
<class="two">class选择器>标签名选择器</p>

<div class="father">背景色为red
<div class="son">class+标签名选择器>class选择器</div>
</div>

</body>
</html>

转载于:https://www.cnblogs.com/samwu/archive/2011/04/16/2018287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值