超级详细,玩转css选择器,这一篇就够了

导语:
css其实也能玩出花样来,只要学的够深入,够深刻,轻轻松松写出令人惊艳的样式出来,本文来写一写css选择器。

所有的css选择器整理:

选择器 例子 例子描述
.class .xiaomizhou 选择 class=“xiaomizhou” 的所有元素
#id #firstname 选择 id=“firstname” 的所有元素
* * 选择所有元素
element p 选择所有 < p > 元素
element,element div,p 选择所有 < div > 元素和所有 < p > 元素
element element div p 选择 < div > 元素内部的所有 < p > 元素
element>element div>p 选择父元素为 < div > 元素的所有 < p > 元素
element+element div+p 选择紧接在 < div > 元素之后的所有 < p > 元素。
[attribute] [target] 选择带有 target 属性所有元素。
[attribute=value] [target=_blank] 选择 target="_blank" 的所有元素。
[attribute~=value] [title~=flower] 选择 title 属性包含单词 “flower” 的所有元素。
[attribute] =value] [lang
:link a:link 选择所有未被访问的链接。
:visited a:visited 选择所有已被访问的链接。
:active a:active 选择活动链接。
:hover a:hover 选择鼠标指针位于其上的链接。
:focus input:focus 选择获得焦点的 input 元素。
:first-letter p:first-letter 选择每个 < p > 元素的首字母。
:first-line p:first-line 选择每个 < p > 元素的首行。
:first-child p:first-child 选择属于父元素的第一个子元素的每个 < p > 元素。
:before p:before 在每个 < p > 元素的内容之前插入内容。
:after p:after 在每个 < p > 元素的内容之后插入内容。
:lang(language) p:lang(it) 选择带有以 “it” 开头的 lang 属性值的每个 < p > 元素。
element1~element2 p~ul 选择前面有 < p > 元素的每个 < ul > 元素。
[attribute^=value] a[src^=“https”] 选择其 src 属性值以 “https” 开头的每个 < a > 元素。
[attribute$=value] a[src$=".pdf"] 选择其 src 属性以 “.pdf” 结尾的所有 < a > 元素。
[attribute*=value] a[src*=“abc”] 选择其 src 属性中包含 “abc” 子串的每个 < a > 元素。
:first-of-type p:first-of-type 选择属于其父元素的首个 < p > 元素的每个 < p > 元素。
:last-of-type p:last-of-type 选择属于其父元素的最后 < p > 元素的每个 < p > 元素。
:only-of-type p:only-of-type 选择属于其父元素唯一的 < p > 元素的每个 < p > 元素。
:only-child p:only-child 选择属于其父元素的唯一子元素的每个 < p > 元素。
:nth-child(n) p:nth-child(2) 选择属于其父元素的第二个子元素的每个 < p > 元素。
:nth-last-child(n) p:nth-last-child(2) 同上,从最后一个子元素开始计数。
:nth-of-type(n) p:nth-of-type(2) 选择属于其父元素第二个 < p > 元素的每个 < p > 元素。
:nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是从最后一个子元素开始计数。
:last-child p:last-child 选择属于其父元素最后一个子元素每个 < p > 元素。
:root :root 选择文档的根元素。
:empty p:empty 选择没有子元素的每个 < p > 元素(包括文本节点)。
:target #news:target 选择当前活动的 #news 元素。
:enabled input:enabled 选择每个启用的 < input > 元素。
:disabled input:disabled 选择每个禁用的 < input > 元素
:checked input:checked 选择每个被选中的 < input > 元素。
:not(selector) :not§ 选择非 < p > 元素的每个元素。
::selection ::selection 选择被用户选取的元素部分。

1,class

<!DOCTYPE html>
<html>
<head>
<style>
.intro{
     
     
	background-color:yellow;
}
</style>
</head>
<body>
	<div class="intro">
		<p>我是小米粥。</p>
	</div>
</body>
</html>

在这里插入图片描述

2,element+element

<!DOCTYPE html>
<html>
<head>
<style>
div+p{
     
     
	background-color:yellow;
}
</style>
</head>
<body>
	<div class="intro">
		<p>我是小米粥。</p>
	</div>
    <p>我是小米粥。2</p>
</body>
</html>

在这里插入图片描述

3,[attribute]

//[target] 有target属性
//[target=_blank] target属性为_blank
//[title~=flower] title属性包含flower
//[lang|=en] lang属性以en开头


//下面演示第二种target属性为_blank
<!DOCTYPE html>
<html>
<head>
<style>
a[target=_blank]{
     
     
	background-color:yellow;
}
</style>
</head>
<body>
	<p>带有 target 属性的链接会得到黄色背景:</p>
	  <a href="http://www.w3school.com.cn">w3school.com.cn</a>
	  <a href="http://www.disney.com" target="_blank">disney.com</a>
	  <a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>
</body>
</html>

在这里插入图片描述

4,a:link,:visited,:active,:hover,:focus

<head>
<style>
a:link{
     
     
	background-color:white;
}
a:visited{
     
     
	background-color:yellow;
}
a:active{
     
     
	background-color:yellow;
}
a:hover{
     
      
	background-color:yellow;
}
input:focus{
     
     
	background-color:yellow;
}
</style>
</head>
<body>
	<a href="http://www.w3school.com.cn">W3Sschool</a>
	<a href="http://www.google.com">Google</a>
	<a href="http://www.wikipedia.org">Wikipedia</a><br>
	First name: <input type="text" name="firstname" /><br>
	<p><b>注释:<br></b>:link 选择器为未被访问过的链接设置样式。</p><br>
	</b>:visited 选择器为已被访问过的链接设置样式。</p><br>
	</b>:active 选择器为点击时候链接设置样式。</p><br>
	</b>:hover 选择器为鼠标悬浮的时候链接设置样式。</p><br>
	</b>input:focus 选择器为input聚焦时候的设置样式。</p><br>
</body>
</html>

在这里插入图片描述

5,first-letter,first-line

<!DOCTYPE html>
<html>
<head>
<style>
p:first-letter{
     
     
	font-size:200%;
	color:#8A2BE2;
}
p:first-line{
     
     
	background-color:yellow;
}
</style>
</head>
<body>
  <div>
    <p>My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.My name is Donald.</p>
    <p>I live in Duckburg.</p>
  </div>
</body>
</html>

在这里插入图片描述

6,before,after

<!DOCTYPE html>
<html>
<head>
<style>
p:before{
     
     
	content:"台词:";
}
p:after{
     
     
	content:"哈哈哈哈";
}
</style>
</head>
<body>
  <p>我是小米粥。</p>
  <p>我住在中国。</p>
  <b>注释:</b>对于在 IE8 中工作的 :before,必须声明 DOCTYPE。
</body>
</html>

在这里插入图片描述

7,input[type=“text”]:enabled,:disabled

<!DOCTYPE html>
<html>
<head>
<style> 
input[type="text"]:enabled
{
     
     
background:#ffff00;
}
input[type="text"]:disabled
{
     
     
background:#dddddd;
}
</style>
</head>
<body>

<form action="">
First name: <input type="text" value="Mickey" /><br>
Last name: <input type="text" value="Mouse" /><br>
Country: <input type="text" disabled="disabled" value="Disneyland" /><br>
</form>

</body>
</html>

在这里插入图片描述
补充
在这里插入图片描述
微信搜索【web小馆】,回复全栈博客项目,即可获取项目源码和后续的实战文章教程。每天用最简单朴实的语言,潜移默化的提升你的计算机基础知识和前端技术。小米粥,一个专注的web全栈工程师,我们下期再见!

在这里插入图片描述
node后台

猜你喜欢

转载自blog.csdn.net/gitchatxiaomi/article/details/108918233