css3选择器

本文详细介绍了CSS3中新增的基础选择器、属性选择器、伪类选择器等,并通过实例展示了如何使用这些选择器来精确地定位和修饰网页元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CSS3 是 CSS 的 技术升级版本!

【回顾:css中选择器】

目录


*** CSS3 基础选择器(新增) ***

 

  •  子元素选择器

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>子元素选择器</title>
<style type="text/css">
section > div {
	color: #f00;
}
</style>
</head>
<body>
<section>
	<article>
		<div>article里面的文字</div>
	</article>
	<div>article外面的文字</div>
</section>
</body>
</html>
  • 相邻兄弟元素选择器

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>兄弟元素选择器</title>
<style type="text/css">
section > div + article {      
 /*控制的是与section下第一个div挨着的后边的第一个article*/
	color: #f00;
}
</style>
</head>
<body>
<section>
	<div>article外面的文字</div>
	<article>
		<div>article里面的文字</div>
	</article>
	<article>
		<div>article里面的文字</div>
	</article>
</section>
</body>
</html>
  • 通用兄弟选择器

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>通用兄弟选择器</title>
<style type="text/css">
section > div ~ article {
	color: #f00;
/*section下第一个div后边的所有 article子元素都修改颜色*/
}
</style>
</head>
<body>
<section>
	<article>
		<div>article里面的文字</div>
	</article>
	<div>article外面的文字</div>
	<article>
		<div>article里面的文字</div>
	</article>
	<article>
		<div>article里面的文字</div>
	</article>
	<article>
		<div>article里面的文字</div>
	</article>
</section>
</body>
</html>
  • 群组选择器

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>群组选择器</title>
<style type="text/css">
section > article,
section > aside,
section > div {
	color: #f00;
	margin-top: 10px;
	background: #abcdef;
}
</style>
</head>
<body>
<section>
	<article>article</article>
	<aside>aside</aside>
	<div>div</div>
</section>
</body>
</html>

 

*** CSS3 属性选择器(新增) ***

  • Element[attribute]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute</title>
<style type="text/css">
a[href] {
	text-decoration: none;
}
</style>
</head>
<body>
<a href="attribute.html">attribute</a>
</body>
</html>
  • Element[attribute="value"]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute</title>
<style type="text/css">
a[href] {
	text-decoration: none;   /*所有拥有属性为href的a标签元素内 会改变样式*/
}
a[href="#"] {
	color: #f00;  /*只有属性href值为#的a标签元素才会改变样式*/
}
</style>
</head>
<body>
<a href="attribute.html">attribute</a>
<a href="#">attribute</a>
</body>
</html>
  • Element[attribute ~= "value"]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute</title>
<style type="text/css">
a[class~="two"] {    /*class属性值中包含“two”的所有a标签元素会改变样式*/
	color: #ff0;
}
</style>
</head>
<body>
<a class="one two" href="#">attribute</a>
<a class="two three" href="#">attribute</a>
</body>
</html>
  • Element[attribute ^= "value"]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute</title>
<style type="text/css">
a[href^="#"] {
	color: #0f0;   /*href属性值 以#开头的(包含只有#的标签元素)*/
}
</style>
</head>
<body>
<a href="attribute.html">attribute</a>
<a href="#">attribute</a>
<a href="#1">attribute</a>
<a href="#2">attribute</a>
<a href="#3">attribute</a>
<a href="#4">attribute</a>
<a href="1#">attribute</a>
<a href="2#">attribute</a>
<a href="3#">attribute</a>
<a href="4#">attribute</a>
</body>
</html>
  • Element[attribute $= "value"]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute</title>
<style type="text/css">
a[href$="#"] {
	color: #00f;   /* href属性值 以“#”结尾的标签元素*/
}
</style>
</head>
<body>
<a href="#">attribute</a>
<a href="#1">attribute</a>
<a href="#2">attribute</a>
<a href="#3">attribute</a>
<a href="#4">attribute</a>
<a href="1#">attribute</a>
<a href="2#">attribute</a>
<a href="3#">attribute</a>
<a href="4#">attribute</a>
</body>
</html>
  • Element[attribute *= "value"]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute</title>
<style type="text/css">
a[href*="#"] {
	color: #0ff;
}
</style>
</head>
<body>
<a href="attribute.html">attribute</a>
<a href="#">attribute</a>
<a class="one two" href="#">attribute</a>
<a class="two three" href="#">attribute</a>
<a href="#1">attribute</a>
<a href="#2">attribute</a>
<a href="#3">attribute</a>
<a href="#4">attribute</a>
<a href="1#">attribute</a>
<a href="2#">attribute</a>
<a href="3#">attribute</a>
<a href="4#">attribute</a>
<a href="1#1">attribute</a>
<a href="2#2">attribute</a>
<a href="3#3">attribute</a>
<a href="4#4">attribute</a>
<a href="#-1">attribute</a>
<a href="#-2">attribute</a>
<a href="#-3">attribute</a>
<a href="#-4">attribute</a>
</body>
</html>
  • Element[attribute |= "value"]

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute</title>
<style type="text/css">
a[href|="#"] {
	color: #f0f;  /*属性值 以#开头,或者以 #- 开头*/
}
</style>
</head>
<body>
<a href="#1">attribute</a>
<a href="#2">attribute</a>
<a href="#3">attribute</a>
<a href="#4">attribute</a>
<a href="1#">attribute</a>
<a href="2#">attribute</a>
<a href="3#">attribute</a>
<a href="4#">attribute</a>
<a href="1#1">attribute</a>
<a href="2#2">attribute</a>
<a href="3#3">attribute</a>
<a href="4#4">attribute</a>
<a href="#-1">attribute</a>
<a href="#-2">attribute</a>
<a href="#-3">attribute</a>
<a href="#-4">attribute</a>
</body>
</html>

 

*** CSS3 伪类选择器(新增) ***

  • 动态伪类(锚点伪类、用户行为伪类)


 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态伪类</title>
<style type="text/css">
a{
    text-decoration:none;
}
a:link{
    color:#000;  /*默认链接字体颜色 黑色*/
}
a:hover{
    color:#f00;  /*鼠标经过为 红色*/
}
a:active{
    color:ff0;   /*鼠标按下为 黄色*/
}
a:visited{
    color:#0ff;  /*鼠标松开为 天蓝色*/
}
</style>
</head>
<body>
<a href="#">动态伪类</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态伪类</title>
<style type="text/css">
input{
    width:200px;
    height:30px;
    border:2px red solid;
}
input:focus{
    background:#f00;  /*当光标点击进输入框后,输入框背景色改变为天蓝色;光标移开则没有背景颜色了*/
}
</style>
</head>
<body>
<input type="text>
</body>
</html>
  • UI元素状态 伪类

  

//  :checked  选择器匹配每个已被选中的 input 元素、只用于单选按钮和复选框

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>UI元素状态伪类</title>
<style type="text/css">
input:enabled{
    width:200px;
    height:30px;
    border:1px red solid;
}
input:disabled{         /*disabled会没有宽和高*/
    background:#abcdef;
    border:1px yellow solid;
}
</style>
</head>
<body>
<input type="text" disabled="disabled">
<input type="text">     <!--默认是 enabled-->
<input type="text">
<input type="text">
</body>
</html>

 

  • CSS3结构类(又称 :nth选择器)

  • :first-child

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First-Child</title>
<style type="text/css">
section > div:first-child {   /* section下p标签的的父元素下 第一个div标签元素*/
	color: #f00;
}
</style>
</head>
<body>
<div>0-1</div>
<div>0-2</div>
<div>0-3</div>
<section>
	<div>1-1</div>
	<div>1-2</div>
	<div>1-3</div>
</section>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>
</section>
</body>
</html>
  • :last-child 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Last-Child</title>
<style type="text/css">
div:last-child {    
	color: #f00;
}
</style>
</head>
<body>
<div>0-1</div>
<div>0-2</div>
<div>0-3</div>     /*这个div其父元素body最后一个元素不是div,而是section,所以不匹配*/
<section>
	<div>1-1</div>
	<div>1-2</div>
	<div>1-3</div>  /*这个div父元素section的最后一个元素是div,则变红*/
</section>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>  /*这个div父元素section的最后一个元素是div,则变红*/
</section>
</body>
</html>
  • :nth-child(n) 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>nth-Child</title>
<style type="text/css">
ul > li:nth-child(3) {    /*第三个li改变背景颜色*/
	background: #f00;
}
div:nth-child(2) {  
	color: #f00;
}
</style>
</head>
<body>
<ul>
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
	<li>5</li>
	<li>6</li>
	<li>7</li>
	<li>8</li>
	<li>9</li>
</ul>
<hr>
<div>0-1</div>
<div>0-2</div>  <!-- 这边div标签的父元素body的第二个孩子标签是hr,不是div,所以不会变红 -->
<div>0-3</div>
<section>
	<div>1-1</div>
	<div>1-2</div>  <!-- 这个div标签的父元素section的第二个孩子标签是div,所以会变红 -->
	<div>1-3</div>
</section>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>
</section>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>nth-Child</title>
<style type="text/css">
ul > li:nth-child(3) {
	background: #f00;
}
div:nth-child(2) {
	color: #f00;
}
/*
ul > li:nth-child(2n) {
	background: #ff0;
}
ul > li:nth-child(2n+1) {
	background: #0f0;
}
ul > li:nth-child(n+4) {
	background: #abcdef;
}*/
ul > li:nth-child(odd) {  /*odd = 2n+1*/
	background: red;
}
ul > li:nth-child(even) {  /*even = 2n*/
	background: blue; 
}

</style>
</head>
<body>
<ul>
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
	<li>5</li>
	<li>6</li>
	<li>7</li>
	<li>8</li>
	<li>9</li>
</ul>
<hr>
<div>0-1</div>
<div>0-2</div>
<div>0-3</div>
<section>
	<div>1-1</div>
	<div>1-2</div>
	<div>1-3</div>
</section>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>
</section>
<hr>
<ul>
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
	<li>5</li>
	<li>6</li>
	<li>7</li>
	<li>8</li>
	<li>9</li>
</ul>
</body>
</html>
  • :nth-last-child(n) 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>nth-Child</title>
<style type="text/css">
ul > li:nth-last-child(3) {   
	background: #f00;
}
</style>
</head>
<body>
<ul>
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
	<li>5</li>
	<li>6</li>
	<li>7</li>
	<li>8</li>
	<li>9</li>
</ul>
<hr>
<ul>
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
	<li>5</li>
	<li>6</li>
	<li>7</li>
	<li>8</li>
	<li>9</li>
</ul>
</body>
</html>

【注意】:无论是nth-child还是nth-last-child,都是从1开始计数(但是很多js中都是从0开始计数)

 

  • :nth-of-type(n)

  nth-child(n),这个是不区分类型

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>nth-of-type</title>
<style type="text/css">
/*div:nth-child(2) {   <!-- 判断其父元素的第二个元素,如果是div则变色,不是则不变色 -->
	color: #f00; 
}*/
div:nth-of-type(2) {   <!-- 其父元素的第二个div即可 -->
	color: #f00;
}
</style>
</head>
<body>
<div>0-1</div>
<section>
	<div>1-1</div>
	<div>1-2</div>
	<div>1-3</div>
</section>
<div>0-2</div>    
<div>0-3</div>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>
</section>
</body>
</html>
  • :nth-last-of-type(n) 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>nth-Last-of-type</title>
<style type="text/css">
/*div:nth-last-child(2) {   /*先判断 父元素body/section的倒数第二个元素 是否是div*/
	color: #f00;
}*/
div:nth-last-of-type(2) {   /*父元素body/section的倒数第二个div即可*/
	color: #f00;
}
</style>
</head>
<body>
<div>0-1</div>
<section>
	<div>1-1</div>
	<div>1-2</div>
	<div>1-3</div>
</section>
<div>0-2</div>
<div>0-3</div>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>
</section>
</body>
</html>
  • :first-of-type 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First-of-type</title>
<style type="text/css">
div:first-of-type {   /*相当于 nth-of-type(1),body下边第一个div变红;section下边第一个div变红*/
	color: #f00;
}
</style>
</head>
<body>
<div>0-1</div>   
<section>
	<div>1-1</div>
	<div>1-2</div>
	<div>1-3</div>
</section>
<div>0-2</div>
<div>0-3</div>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>
</section>
</body>
</html>
  • :last-of-type 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Last-of-type</title>
<style type="text/css">
div:last-of-type {  /*section中最后一个div;body最后一个div*/
	color: #f00;
}
</style>
</head>
<body>
<div>0-1</div>
<section>
	<div>1-1</div>
	<div>1-2</div>
	<div>1-3</div>
</section>
<div>0-2</div>
<div>0-3</div>
<section>
	<div>2-1</div>
	<div>2-2</div>
	<div>2-3</div>
</section>
</body>
</html>
  • :only-child 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>only-child</title>
<style type="text/css">
article:only-child {   /*父元素中仅能有一个article元素*/
	background: #f00;
}
</style>
</head>
<body>
<section>
	<article>1</article>
	<article>2</article>
</section>
<section>
	<article>3</article>  /*可以*/
</section>
<section>
	<div>4</div>
	<article>5</article>
	<div>6</div>
</section>
<section>
	<div>7</div>
	<article>8</article>
	<article>9</article>
	<div>10</div>
</section>
</body>
</html>
  • :only-of-type 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>only-of-type</title>
<style type="text/css">
article:only-of-type {   /*父元素中只能有一组article标签(可以有其他的标签),只要指定元素唯一即可*/
	background: #f00;
}
</style>
</head>
<body>
<section>
	<article>1</article>
	<article>2</article>
</section>
<section>
	<article>3</article>
</section>
<section>
	<div>4</div>
	<article>5</article>
	<div>6</div>
</section>
<section>
	<div>7</div>
	<article>8</article>
	<article>9</article>
	<div>10</div>
</section>
</body>
</html>
  • :empty 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>empty</title>
<style type="text/css">
div {
	height: 200px;
	background: #abcdef;
}
div:empty {    /*div标签元素中没有任何子元素 即可*/
	background: #f00;
}
</style>
</head>
<body>
<div></div>
<div>Second</div>
<div></div>
<div>Third</div>
</body>
</html>

 

  • 否定选择器(:not)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>not</title>
<style type="text/css">
* {
	margin: 0;
	padding: 0;
	border: none;
}
a {
	text-decoration: none;
	color: #333;
	font-size: 14px;
	display: block;
	float: left;
	width: 100px;
	height: 30px;
}
nav {
	width: 800px;
	margin: 0 auto;
}
nav > a:not(:last-of-type) {  /*不需要最后一个*/
	border-right: 1px solid red;
}
</style>
</head>
<body>
<nav>
	<a href="#">first</a>
	<a href="#">second</a>
	<a href="#">third</a>
	<a href="#">fourth</a>
	<a href="#">fifth</a>
</nav>
</body>
</html>

 

*** 权重 ***

 

*** css3 伪元素(新增) ***

  • ::first-line

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First-Line</title>
<style type="text/css">
div {
	width: 300px;
	margin: 0 auto;
}
div::first-line {  /*该元素的第一行 会改变样式*/
	color: #f00;
	font-weight: bold;
}
</style>
</head>
<body>
<div>大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!</div>
</body>
</html>
  • ::first-letter

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>First-Letter</title>
<style type="text/css">
div {
	width: 500px;
	margin: 0 auto;
	font-size: 12px;
}
div::first-letter {   /*第一个字符*/
	color: #f00;
	font-size: 24px;
	font-weight: bold;
}
</style>
</head>
<body>
<div>大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!大家好,欢迎来到这里!</div>
</body>
</html>
  • ::before

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>before</title>
<style type="text/css">
div {
	width: 300px;
	height: 100px;
	border: 1px solid #000;
}
div::before {                 /*在元素的前边*/
	content: "我在内容的前面";
}
</style>
</head>
<body>
<div>伪元素</div>
</body>
</html>

    // 可以设置display:block;

 

  • ::after

 // 同样默认为行内元素,也可以通过css控制

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>after</title>
<style type="text/css">
div {
	width: 300px;
	height: 100px;
	border: 1px solid #000;
}
div::after {
	content: "我在内容的后面";
}
</style>
</head>
<body>
<div>伪元素</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>clear</title>
<style type="text/css">
header {
	background: #abcdef;
}
/*清除浮动方法1:添加div块*/
div{
    clear:both;
}
/*清除浮动方法2:添加after伪元素*/
header::after {
	display: block;
	content: "";  /*必须写content*/
	clear: both;
}
header > article {
	float: left;
	width: 40%;
	height: 30px;
	background: #f00;
}
header > aside {
	float: right;
	width: 40%;
	height: 50px;
	background: #ff0;
}
</style>
</head>
<body>
<header>
	<article></article>
	<aside></aside>
        <div></div>
</header>
</body>
</html>
  • ::selection

  //选中文字时,文字颜色、背景颜色都会改变

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Selection</title>
<style type="text/css">
div::selection {
	background: red;
	color: #ff0;
}
</style>
</head>
<body>
<div>SelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelection</div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值