css3选择器详解

10类选择器

1.基本选择器

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<title>使用css3基本选择器</title>
</head>
<body>
<ul class="clearfix demo">
	<li class="first" id="first">1</li>
	<li class="active">2</li>
	<li class="important item">3</li>
	<li class="important">4</li>
	<li class="item">5</li>
	<li>6</li>
	<li>7</li>
	<li>8</li>
	<li>9</li>
	<li class="last" id="last">10</li>
</ul>
</body>
</html>
<style type="text/css">
	*{
    
		margin: 0;
		padding: 0;
	}
	/*清除浮动效果*/
	.clearfix:after,.clearfix:before{
    
		display: table;
		content: ""
	}
	.clearfix:after{
    
		clear: both;
	
	}
	.demo{
    
		width: 250px;
		border:1px solid #ccc;
		padding: 10px;
		/*上下外边距20px 元素水平居中*/
		margin: 20px auto;
	}
	li{
    
		/*list-style-type , list-style-position , list-style-image*/
		/*https://www.cnblogs.com/ymj0906/archive/2012/06/15/2550084.html*/
		/*隐藏列表项*/
		list-style: none outside none;
		float: left;
		height: 20px;
		line-height: 20px;
		width: 20px;
		border-radius: 10px;
		text-align: center;
		background: pink;
		color: green;
		margin-right: 5px;
	}
</style>

效果如图所示
在这里插入图片描述

通配符选择器

使用通配符选择器改变背景色
将元素类名为demo下的所有元素背景色设置为橙色

/*将元素类名为demo下的所有元素背景色设置为橙色*/
	.demo *{
		background: orange;
	}
	

在这里插入图片描述

元素选择器

列表ul的背景色将变成灰色

ul {
		background: grey;
	}

在这里插入图片描述

id选择器
#first{
		background: lime;
		color: #000;
	}
	#last{
		background: #000;
		color: lime;
	}

在这里插入图片描述

类选择器
.item{
		background: green;
		color: #fff;
		font-weight: bold;
	}

在这里插入图片描述

多类选择器

如果一个多类选择器包含的类名中其中有一个不存在,这个选择器将无法找到相匹配的元素
列表3同时具有important和item类名
列表4只有important类名
列表5只有item类名

.item{
		background: green;
		color: #fff;
		font-weight: bold;
	}
	.item.important{
		background: red;
	}

在这里插入图片描述

带标签的类名选择器

仅需要对ul为”block”定义样式

ul.block{
		background: #ccc;
	}
群组选择器

将具有相同样式的元素分组在一起,每个选择器之间用逗号隔开
“selector1,selector2”表示选择匹配为selector1和selector2的所有元素

2.层次选择器

主要层次关系包括后代 父子 相邻兄弟 和通用兄弟几种关系
在这里插入图片描述

举例说明

<!DOCTYPE html>
<html>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值