【css】选择器

选择器

作用:选择页面上的某一个或者某一类元素

1、基本选择器

1.1 标签选择器

选择某一类标签

格式: 标签名 { }

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        h1{
            color: orange;
            background: blue;
            border-radius: 10px;
        }
        h3{
            color: orange;
            background: blue;
            border-radius: 10px;
        }
        p{
            font-size: 80px;
        }
    </style>
</head>
<body>
<h1>标签选择器</h1>
<p>我爱学习</p>
<h3>学习JAVA</h3>
</body>
1.2 类选择器

选择所有class一致的标签,跨标签

格式: .类名{}

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*类选择器的格式 .class的名称{}
            好处:可以多个标签归类,是同一个class,可以复用*/
        .demo1{
            color: blue;
        }
        .demo2{
            color: red;
        }
        .demo3{
            color: aqua;
        }
    </style>
</head>
<body>
<h1 class = "demo1">类选择器:demo1</h1>
<h1 class="demo2">类选择器:demo2</h1>
<h1 class="demo3">类选择器:demo3</h1>
<p class="demo3">p标签</p>
</body>
1.3 id选择器

选择确定id的标签

格式: #id名{}

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*id选择器:id必须保证全局唯一
            #id名称{}
            不遵循就近原则,优先级是固定的
            id选择器 > class类选择器  >  标签选择器
        */
        #demo1{
            color: red;
        }
        .demo2{
            color: green;
        }
        #demo2{
            color: orange;
        }
        h1{
            color: blue;
        }
    </style>
</head>
<body>
    <h1 id="demo1" class="demo2">id选择器:demo1</h1>
    <h1 class="demo2" id = "demo2">id选择器:demo2</h1>
    <h1 class="demo2">id选择器:demo3</h1>
    <h1 >id选择器:demo4</h1>
    <h1>id选择器:demo5</h1>
</body>

优先级:id > class > 标签

2、高级选择器

2.1 层次选择器
2.1.1 后代选择器

选择body内所有的p标签,不论层级

body p{
	background:red;
}
2.1.2 子选择器

选择body内的第一层的p标签

body>p{
	background:orange;
}
2.1.3 相邻兄弟选择器

选择某元素的下一个元素

<style>
.active+p{
background: red
}
</style>
<body>
	<p class="active">p1<p>
	<p>p2</p>
</body>
2.1.4 通用兄弟选择器

选择某元素下方的所有元素

<style>
/*通用兄弟选择器,当前选中元素的向下的所有兄弟元素*/
	.active~p{
	background:red;
}
</style>
<body>
	<p class="active">p1<p>
	<p>p2</p>
</body>
2.2 结构伪类选择器
2.2.1 锚伪类

选择不同点击状态下的元素

/* 未访问的链接 */
a:link {
  color: #FF0000;
}

/* 已访问的链接 */
a:visited {
  color: #00FF00;
}

/* 鼠标悬停链接 */
a:hover {
  color: #FF00FF;
}

/* 已选择的链接 */
a:active {
  color: #0000FF;
}
2.2.2 -:first-child伪类
 ul li:first-child{/*ul的第一个li子元素*/
            background: aqua;
        }
 ul li:last-child{/*ul的最后一个li子元素*/
            background: blue;
        }
2.3 css伪元素
选择器 例子 例子描述
::after p::after 在每个

元素之后插入内容。

::before p::before 在每个

元素之前插入内容。

::first-letter p::first-letter 选择每个

元素的首字母。

::first-line p::first-line 选择每个

元素 的首行。

::selection p::selection 选择用户选择的元素部分。
2.4 属性选择器

根据属性以及属性的值(支持正则)来选择元素

/*元素名[属性名|属性名=属性值(正则)]
         = 表示绝对等于
         *= 表示包含
         ^= 表示以...开头
         $= 表示以...结尾
         
         */

*[title] {/*选择包含标题(title)的所有元素*/
            color:red;
}
a[id]{/*选择所有有id属性的a元素*/
            background: yellow;
}
a[id=first]{/*id=first的a元素*/
            background: green;
}
        
a[class*="links"]{/*class 中包含有links的a元素*/
            background: bisque;
}

a[href^=http]{/*选中href中以http开头的a元素*/
            background: aquamarine;
}
a[href$=pdf]{/*选中href中以pdf结尾的a元素*/
            background: aquamarine;
}

3、多选

不同选择器间用逗号隔开完成多选

/*选择a和div标签*/
a,div{}

表格汇总

选择器 例子 例子描述
.class .intro 选择 class="intro" 的所有元素。
.class1.class2 .name1.name2 选择 class 属性中同时有 name1 和 name2 的所有元素。
.class1 .class2 .name1 .name2 选择作为类名 name1 元素后代的所有类名 name2 元素。
#id #firstname 选择 id="firstname" 的元素。
* * 选择所有元素。
element p 选择所有

元素。

element.class p.intro 选择 class="intro" 的所有

元素。

element,element div, p 选择所有
元素和所有

元素。

element element div p 选择
元素内的所有

元素。

element>element div > p 选择父元素是
的所有

元素。

element+element div + p 选择紧跟
元素的首个

元素。

element1~element2 p ~ ul 选择前面有

元素的每个

    元素。
[attribute] [target] 选择带有 target 属性的所有元素。
[attribute=value] [target=_blank] 选择带有 target="_blank" 属性的所有元素。
[attribute~=value] [title~=flower] 选择 title 属性包含单词 "flower" 的所有元素。
[attribute|=value] [lang|=en] 选择 lang 属性值以 "en" 开头的所有元素。
[attribute^=value] a[href^="https"] 选择其 src 属性值以 "https" 开头的每个 元素。
[attribute$=value] a[href$=".pdf"] 选择其 src 属性以 ".pdf" 结尾的所有 元素。
[attribute**=value*] a[href*="w3schools"] 选择其 href 属性值中包含 "abc" 子串的每个 元素。
:active a:active 选择活动链接。
::after p::after 在每个

的内容之后插入内容。

::before p::before 在每个

的内容之前插入内容。

:checked input:checked 选择每个被选中的 元素。
:default input:default 选择默认的 元素。
:disabled input:disabled 选择每个被禁用的 元素。
:empty p:empty 选择没有子元素的每个

元素(包括文本节点)。

:enabled input:enabled 选择每个启用的 元素。
:first-child p:first-child 选择属于父元素的第一个子元素的每个

元素。

::first-letter p::first-letter 选择每个

元素的首字母。

::first-line p::first-line 选择每个

元素的首行。

:first-of-type p:first-of-type 选择属于其父元素的首个

元素的每个

元素。

:focus input:focus 选择获得焦点的 input 元素。
:fullscreen :fullscreen 选择处于全屏模式的元素。
:hover a:hover 选择鼠标指针位于其上的链接。
:in-range input:in-range 选择其值在指定范围内的 input 元素。
:indeterminate input:indeterminate 选择处于不确定状态的 input 元素。
:invalid input:invalid 选择具有无效值的所有 input 元素。
:lang(language) p:lang(it) 选择 lang 属性等于 "it"(意大利)的每个

元素。

:last-child p:last-child 选择属于其父元素最后一个子元素每个

元素。

:last-of-type p:last-of-type 选择属于其父元素的最后

元素的每个

元素。

:link a:link 选择所有未访问过的链接。
:not(selector) :not(p) 选择非

元素的每个元素。

:nth-child(n) p:nth-child(2) 选择属于其父元素的第二个子元素的每个

元素。

:nth-last-child(n) p:nth-last-child(2) 同上,从最后一个子元素开始计数。
:nth-of-type(n) p:nth-of-type(2) 选择属于其父元素第二个

元素的每个

元素。

:nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是从最后一个子元素开始计数。
:only-of-type p:only-of-type 选择属于其父元素唯一的

元素的每个

元素。

:only-child p:only-child 选择属于其父元素的唯一子元素的每个

元素。

:optional input:optional 选择不带 "required" 属性的 input 元素。
:out-of-range input:out-of-range 选择值超出指定范围的 input 元素。
::placeholder input::placeholder 选择已规定 "placeholder" 属性的 input 元素。
:read-only input:read-only 选择已规定 "readonly" 属性的 input 元素。
:read-write input:read-write 选择未规定 "readonly" 属性的 input 元素。
:required input:required 选择已规定 "required" 属性的 input 元素。
:root :root 选择文档的根元素。
::selection ::selection 选择用户已选取的元素部分。
:target #news:target 选择当前活动的 #news 元素。
:valid input:valid 选择带有有效值的所有 input 元素。
:visited a:visited 选择所有已访问的链接。

表格数据来源:CSS 选择器参考手册 (w3school.com.cn)

posted @ 2021-08-05 16:16  Erics-C  阅读(58)  评论(0)    收藏  举报