CSS pseudo-class
CSS Pseudo-classes
CSS pseudo-classes are used to add some special effects to selectors.
Syntax
Pseudo class syntax:
CSS classes can also use pseudo-classes:
anchor pseudo-class
In browsers that support CSS, different states of the link can be displayed in different ways
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> a:link {color:#FF0000;} /* unvisited link */ a:visited {color:#00FF00;} /* visited link */ a:hover {color:#FF00FF;} /* mouse over link */ a:active {color:#0000FF;} /* selected link */ </style> </head> <body> <p><b><a href="http://www.php.cn/css/css-css_tutorial.html" target="_blank">这是一个链接</a></b></p> <p><b>注意:</b> a:hover 必须在 a:link 和 a:visited 之后,需要严格按顺序才能看到效果。</p> <p><b>注意:</b> a:active 必须在 a:hover 之后。</p> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
Note: In the CSS definition, a:hover must be placed between a:link and It is only valid after a:visited.
Note: In the CSS definition, a:active must be placed after a:hover to be valid.
Note: The names of pseudo-classes are not case-sensitive.
Pseudo classes and CSS classes
Pseudo classes can be used with CSS classes:
<a class="red" href="css-syntax.html">CSS Syntax</a>
If the link in the above example has been visited , it will appear in red.
CSS - :first - child pseudo-class
You can use the :first-child pseudo-class to select the first child element of an element
Note: In previous versions of IE8, <!DOCTYPE> must be declared so that :first-child can take effect.
Match the first <p> element
In the following example, the selector matches the <p> element that is the first child of any element:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> p:first-child { color:blue; } </style> </head> <body> <p>This is some text.</p> <p>This is some text.</p> <p><b>注意:</b>对于 :first-child 工作于 IE8以及更早版本的浏览器, DOCTYPE必须已经声明.</p> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Match the first <i> element among all <p> elements
In the following example, selects all <p> elements that match the first <i> element:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> p > i:first-child { color:blue; } </style> </head> <body> <p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p> <p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p> <p><b>注意:</b> 当 :first-child 作用于 IE8以及更早版本的浏览器, DOCTYPE必须已经定义.</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
Matches all <i> elements that are the first child of a <p> element
In the following example, the selector matches all elements that are the first All <i> elements within the <p> element of the child element:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> p:first-child i { color:blue; } </style> </head> <body> <p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p> <p>I am a <i>strong</i> man. I am a <i>strong</i> man.</p> <p><b>注意:</b> 当:first-child 作用于 IE8及更早版本的浏览器, DOCTYPE 必须已经定义.</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
CSS - :lang pseudo-class
:lang Pseudo-classes give you the ability to define special rules for different languages
Note: IE8 must declare <!DOCTYPE> to support the ;lang pseudo-class.
In the following example, the :lang class defines the type of quotes for the q element with attribute value no:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> q:lang(no) { quotes: "~" "~"; } </style> </head> <body> <p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p> <p>在这个例子中,:lang定义了q元素的值为lang =“no”</p> <p><b>注意:</b> 仅当 !DOCTYPE已经声明时 IE8支持 :lang.</p> </body> </html>
Run instance»
Click the "Run instance" button to view online instances
More instances
Example: Add different styles to hyperlinks
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> a.one:link {color:#ff0000;} a.one:visited {color:#0000ff;} a.one:hover {color:#ffcc00;} a.two:link {color:#ff0000;} a.two:visited {color:#0000ff;} a.two:hover {font-size:150%;} a.three:link {color:#ff0000;} a.three:visited {color:#0000ff;} a.three:hover {background:#66ff66;} a.four:link {color:#ff0000;} a.four:visited {color:#0000ff;} a.four:hover {font-family:monospace;} a.five:link {color:#ff0000;text-decoration:none;} a.five:visited {color:#0000ff;text-decoration:none;} a.five:hover {text-decoration:underline;} </style> </head> <body> <p>将鼠标移至链接上改变样式.</p> <p><b><a class="one" href="http://www.php.cn/css/css-css_tutorial.html" target="_blank">This link changes color</a></b></p> <p><b><a class="two" href="http://www.php.cn/css/css-css_tutorial.html" target="_blank">This link changes font-size</a></b></p> <p><b><a class="three" href="http://www.php.cn/css/css-css_tutorial.html" target="_blank">This link changes background-color</a></b></p> <p><b><a class="four" href="http://www.php.cn/css/css-css_tutorial.html" target="_blank">This link changes font-family</a></b></p> <p><b><a class="five" href="http://www.php.cn/css/css-css_tutorial.html" target="_blank">This link changes text-decoration</a></b></p> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
This example demonstrates how to add additional styles to hyperlinks.
Instance: use: focus
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> input:focus { background-color:yellow; } </style> </head> <body> <form action="" method="get"> 用户名: <input type="text" name="fname" /><br> 密 码: <input type="text" name="lname" /><br> <input type="submit" value="Submit" /> </form> <p><b>注意:</b>仅当 !DOCTYPE已经声明时 IE8支持 :focus.</p> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
This example demonstrates how to use the :focus pseudo-class.
All CSS pseudo-classes/elements
Selectors | Example | Example description |
---|---|---|
:checked | input:checked | Select all selected form elements |
:disabled | input:disabled | Select all disabled form elements |
:empty | p:empty | Select all p elements that have no child elements |
:enabled | input:enabled | Select all enabled form elements |
:first-of-type | p:first-of-type | Select each parent element that is the first p child element of the p element |
:in-range | input:in-range | Select values within the specified range of elements |
:invalid | input:invalid | Select all invalid elements |
:last-child | p:last-child | Select the last child element of all p elements |
:last-of-type | p:last-of-type | Select each p element that is the last p element of its parent element |
:not(selector) | :not(p) | Select all elements except p |
:nth-child(n) | p:nth-child(2) | Select the second child element of all p elements |
:nth-last-child(n) | p:nth-last-child(2) | Select all p elements The second to last child element |
:nth-last-of-type(n) | p:nth-last-of-type (2) | Select the second to last child element of p for all p elements |
:nth-of-type(n) | p:nth-of-type(2) | Select all p elements whose second child element is p |
: only-of-type | p:only-of-type | Select all elements with only one child element being p |
:only-child | p:only-child | Select all p elements that have only one child element |
:optional | input :optional | Select element attributes without "required" |
:out-of-range | input:out-of-range | Select element attributes with values outside the specified range |
:read-only | input:read-only | Select read-only attributes Element attributes |
:read-write | input:read-write | Select element attributes that do not have read-only attributes |
:required | input:required | Select the element attribute specified by the "required" attribute |
:root | root | Select the root element of the document |
:target | #news:target | Select the current active #news element (the click URL contains the name of the anchor) |
:valid | input:valid | Select All properties with valid values |
:link | a:link | Select all unvisited links |
:visited | a:visited | Select all visited links |
:active | a:active | Select the active link |
:hover | a:hover | The state of placing the mouse on the link |
:focus | input:focus | Select elements that have focus after input |
##:first-letter | p:first-letterSelect each The first letter of a<p> element | |
:first-line | p:first-lineSelects the first row of each <p> element | |
:first-child | p:first-childThe selector matches the <]p> element that is the first child element of any element | |
p:before | Insert content before each <p> element | |
:after | p:after | Insert content after each <p> element |
##:lang(language) | p:lang(it)Select a starting value for the lang attribute of the <p> element |