CSS(五)选择器优先级

简单选择器优先级


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		
		<style>
			/* 简单选择器优先级顺序: !important>id选择器>类选择器>标签选择器>通配符选择器 
			 如果在简单选择器里的某个样式上加了!important,那么此时它的优先级是最高的
			 **/
			*{
			  color:yellow;	
			}
			
			p{
				color:blue;
			}
			
			.txt{
				color:red  !important;
			}
			#info{
				color:green;
			}
		</style>
	</head>
	<body>
		<p class="txt" id="info">我是一个段落</p>
	</body>
</html>

在这里插入图片描述

复合选择器优先级


<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		
		<style>
			/**
			 * 当修饰元素的选择器是复合选择器时,先比较id选择器出现的次数,谁的id选择器的次数多,谁的优先级高;
			 *  在id选择器次数相同情况下,再比较类选择器的次数,谁的类选择器的次数多,谁的优先级高
			 * 
			 */
			/* 1个标签选择器,2个id选择器 */
	   div #box-second-id #box-three-id {
            color: blue;
        }
        /*1个标签选择符  1个id选择符  1个类选择符*/
        div #box-second-id .box-three-class {
            color: red;
        }
			
		</style>
	</head>

	<body>
		<div class="box-first-class" id="box-first-id">
			<div class="box-second-class" id="box-second-id">
				<div class="box-three-class" id="box-three-id">
					我是一个div标签
				</div>
			</div>
		</div>
	</body>

</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值