深入理解CSS伪类_html/css_WEB-ITnose
× 目录 [1]锚点 [2]UI元素 [3]结构伪类 [4]其他
前面的话
伪类经常与伪元素混淆,伪元素的效果类似于通过添加一个实际的元素才能达到,而伪类的效果类似于通过添加一个实际的类来达到。实际上css3为了区分两者,已经明确规定了伪类用一个冒号来表示,而伪元素则用两个冒号来表示。本文将详细介绍伪类的详细知识
锚点
关于锚点,有常见的5个伪类,分别是:link,:hover,:active,:focus,:visited
a:link{background-color:pink;}/*品红,未访问*/
a:hover{background-color:lightblue;}/*浅蓝,鼠标悬停*/
a:active{background-color:lightgreen;}/*浅绿,正被点击*/
a:focus{background-color:lightgrey;}/*浅灰,拥有焦点*/
a:visited{color:orange;}/*字体颜色为橙色,已被访问*//*[注意]visited伪类只能设置字体颜色的样式*/
伪类顺序
对于伪类顺序,有一个口诀是love-hate,代表着伪类的顺序是link、visited、focus、hover、active。但是否伪类的顺序只能如此呢?为什么是这个顺序呢?
CSS层叠中有一条法则十分重要,就是后面覆盖前面,所以伪类的顺序是需要精心考虑的。
【1】link和visited必须在最前面,且没有先后顺序,否则link或visited的效果将被覆盖
[注意]link和visited称为静态伪类,只能应用于超链接
【2】hover、active、focus这三个伪类必须是focus、hover、active的顺序,因为在focus状态下,也需要触发hover和active,而要触发active一定要先触发hover,所以active要放在hover后面
[注意]hover、active、focus称为动态伪类,可应用于任何元素,但IE7-浏览器不支持:focus,:hover和:active在IE6-浏览器下只支持给设置
所以最终的顺序只有两种:link、visited、focus、hover、active或visited、link、focus、hover、active
a:link{background-color:pink;}/*品红,未访问*/a:visited{color:orange;}/*字体颜色为橙色,已被访问*/a:focus{background-color:lightgrey;}/*浅灰,拥有焦点*/a:hover{background-color:lightblue;}/*浅蓝,鼠标悬停*/a:active{background-color:lightgreen;}/*浅绿,正被点击*/
UI元素伪类
UI元素伪类包括:enabled、:disabled、:checked三个,主要针对于HTML中的form元素,IE8-浏览器不支持
:enabled 可用状态:disabled 不可用状态:checked 选中状态
input:enabled{ border: 1px solid black; background-color: transparent;}input:disabled{ border: none; background-color: gray;}input:checked{ outline: 2px solid lightblue;}
<button onclick = "btn.disabled = false;">按钮可用</button><br /><button onclick = "btn.disabled = true;">按钮不可用</button><br /><input type="button" id="btn" value="按钮"><br /><br><label>Male<input type="radio" name="sex" /></label><br /><label>Female<input type="radio" name="sex" /></label>
[注意]input和:和enabled之间都不可以有空格
结构伪类
结构伪类可分为以下3种情况,IE8-浏览器不支持
//以下情况都是E为父元素,F为子元素
【1】:nth-child(n)、:nth-last-child(n)、first-child、last-child、:only-child
E F:nth-child(n) 选择父元素的第n个子元素E F:nth-last-child(n) 选择父元素的倒数第n个子元素E F:first-child 父元素的第一个子元素,与E F:nth-child(1)等同E F:last-child 父元素的最后一个子元素,与E F:nth-last-child(1)等同E F:only-child 选择父元素中只包含一个子元素
[注意]:firsr-child和:last-child只有IE6-浏览器不支持
n可以是整数(从1开始),也可以是公式,也可以是关键字(even、odd)
p:first-child 代表的并不是<p>的第一个子元素,而是<p>元素是某元素的第一个子元素p > i:first-child 匹配所有<p>元素中的第一个<i>元素p:first-child i 匹配所有作为第一个子元素的<p>元素中的所有<i>元素
li:nth-child(odd){color: red;} li:nth-last-child(3){color: green;}li:first-child{color: blue;}li:last-child{color: yellow;}div:only-child{background-color:lightgrey;}
<ul> <li><div>第一个DIV</div></li> <li><div>第二个DIV</div></li> <li><div>第三个DIV</div></li> <li><div>第四个DIV</div></li> <li><div>第五个DIV</div></li> <li><div>第六个DIV</div></li> </ul>
【2】:nth-of-type(n)、:nth-last-of-type(n)、:first-of-type、:last-of-type、:only-of-type
E F:nth-of-type(n) 选择父元素的具有指定类型的第n个子元素E F:nth-last-of-type(n) 选择父元素的具有指定类型的倒数第n个子元素E F:first-of-type 选择父元素中具有指定类型的第1个子元素,与E F:nth-of-type(1)相同E F:last-of-type 选择父元素中具有指定类型的最后1个子元素,与E F:nth-last-of-type(1)相同E F:only-of-type 选择父元素中只包含一个同类型的子元素
.box div:nth-of-type(even){color: red;} .box p:nth-last-of-type(3){color: green;}.box div:first-of-type{color: blue;}.box p:last-of-type{color: yellow;}.box div:only-of-type{color: pink;}
<div class="box"> <div>第一个div</div> <p>第一个p</p> <div>第二个div</div> <p>第二个p</p> <div class="in">第三个div</div> <p>第三个p</p></div><div class="box"> <div>第四个div</div></div>
【3】:root、:not、:empty、:target
:root 选择文档的根元素,即<html>元素:not 选择除某个元素之外的所有元素:empty 选择没有子元素的元素,而且该元素也不包含任何文本节点:target 匹配锚点对应的目标元素
[注意]:not选择器只有safari9+及ios9.2+浏览器支持
:root{color:red;}div:not{background-color: lightgrey;}p:empty{height:30px;width:30px;background:pink;}:target{color:blue;}
<body> <a href="#test">测试</a> <div>第一个div</div> <p>第一个p</p> <div id="test">第二个div</div> <p>第二个p</p> <p></p> </body>
其他
【1】:lang() 匹配某个语言,IE7-浏览器不支持
p:lang(en) 匹配语言为"en"的<p>
【2】不仅可以使用单一伪类,也可以伪类结合使用
[注意]顺序无关
div:hover:first-child{background-color: lightgreen;}div:last-of-type:active{background-color: lightblue;}
<div>第一个div</div> <div>第二个div</div>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.
