📄 css_pseudo_classes.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CSS Pseudo-classes</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>CSS Pseudo-classes</h1>
<a href="css_positioning.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="css_pseudo_elements.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>
<hr />
<p class="intro">CSS pseudo-classes are used to add special effects
to some selectors.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=trycss_link">Hyperlink</a><br />
This example demonstrates how to add different colors to a hyperlink in a
document.</p>
<p><a target="_blank" href="tryit.asp@filename=trycss_link2">Hyperlink 2</a><br />
This example demonstrates how to add other styles to hyperlinks.</p>
<p><a target="_blank" href="tryit.asp@filename=trycss_link_focus">Hyperlink: use
of :focus</a>
(does not work in IE)<br />
This example demonstrates how to use the :focus pseudo-class on a hyperlink.</p>
<p><a target="_blank" href="tryit.asp@filename=trycss_first-child1">:first-child
- change first child <p></a> <br />
This example sets any <p> element that is the first child
of any element to bold.<br />
(for IE <a href="../tags/tag_doctype.asp"><!DOCTYPE></a>
must be
declared)</p>
<p><a target="_blank" href="tryit.asp@filename=trycss_first-child2">:first-child
- change first child <em> in all <p> elements</a><br />
This example sets the first <em> element in all <p> elements to bold.<br />
(for IE <a href="../tags/tag_doctype.asp"><!DOCTYPE></a> must be
declared)</p>
<p><a target="_blank" href="tryit.asp@filename=trycss_first-child3">:first-child
- change all <em> elements in first child <p></a><br />
This example sets any <em> elements in first child
<p> elements to bold.<br />
(for IE <a href="../tags/tag_doctype.asp"><!DOCTYPE></a> must be
declared)</p>
<p><a target="_blank" href="tryit.asp@filename=trycss_lang">:lang</a> (does not
work in IE)<br />
This example demonstrates the use of the :lang pseudo-class.</p>
<hr />
<h2>Syntax</h2>
<p>The syntax of pseudo-classes:</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>selector:pseudo-class {property: value}</pre>
</td></tr></table>
<p>CSS classes can also be used with pseudo-classes:</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>selector.class:pseudo-class {property: value}</pre>
</td></tr></table>
<br />
<hr />
<h2>Anchor Pseudo-classes</h2>
<p>A link that is active, visited, unvisited, or when you mouse over a link can
all be displayed in different ways
in a CSS-supporting browser:</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>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 */</pre>
</td></tr></table>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS
definition in order to be effective!!</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS
definition in order to be effective!!</p>
<p><b>Note:</b> Pseudo-class names are not case-sensitive.</p>
<hr />
<h2>Pseudo-classes and CSS Classes</h2>
<p>Pseudo-classes can be combined with CSS classes: </p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>a.red:visited {color: #FF0000}</pre>
<pre><a class="red" href="css_syntax.asp">CSS Syntax</a></pre>
</td></tr></table>
<p>If the link in the example above has been visited, it will be displayed in red.</p>
<hr />
<h2>CSS2 - The :first-child Pseudo-class</h2>
<p>The :first-child pseudo-class matches a specified element that is the first child of
another
element.</p>
<p><b>Note:</b> For :first-child to work in IE. A
<a href="../tags/tag_doctype.asp"><!DOCTYPE></a> must be declared.</p>
<h2>Match the first <p> element</h2>
<p>
In the following example, the selector matches any <p> element that is the first child
of any element:<br />
</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table5"><tr><td>
<pre><html>
<head>
<style type="text/css">
p:first-child
{
font-weight:bold
}
</style>
</head></pre>
<pre><body>
<p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>
<p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>
</body>
</html></pre>
</td></tr></table>
<p><a href="tryit.asp@filename=trycss_first-child1">Try it yourself!</a></p>
<h2>Match the first <em> element in all <p> elements</h2>
<p>In the following example, the selector matches any <em> element that is the first child of a
<p> element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table6"><tr><td>
<pre><html>
<head>
<style type="text/css">
p > em:first-child
{
font-weight:bold
}
</style>
</head></pre>
<pre><body>
<p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>
<p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>
</body>
</html></pre>
</td></tr></table>
<p><a href="tryit.asp@filename=trycss_first-child2">Try it yourself!</a></p>
<h2>Match all <em> elements in all first child <p> elements</h2>
<p>In the following example, the selector matches any <em> elements in
<p> elements that are the first child of another element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table7"><tr><td>
<pre><html>
<head>
<style type="text/css">
p:first-child em
{
font-weight:bold
}
</style>
</head></pre>
<pre><body>
<p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>
<p>I am a <em>strong</em> man. I am a <em>strong</em> man.</p>
</body>
</html></pre>
</td></tr></table>
<p><a href="tryit.asp@filename=trycss_first-child3">Try it yourself!</a></p>
<hr />
<h2>CSS2 - The :lang Pseudo-class</h2>
<p>The :lang pseudo-class allows you to define special rules for different
languages. In the example below, the :lang class defines the type of quotation
marks for q elements with a lang attribute with a value of "no":</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre><html>
<head>
<style type="text/css">
q:lang(no)
{
quotes: "~" "~"
}
</style>
</head></pre>
<pre><body>
<p>Some text <q lang="no">A quote in a paragraph</q>
Some text.</p>
</body></pre>
<pre></html></pre>
</td></tr></table>
<br />
<hr />
<h2>Pseudo-classes</h2>
<p>
<b>Browser support:</b> IE: Internet Explorer, F: Firefox, N: Netscape.</p>
<p>
<b>W3C:</b> The number in the "W3C" column indicates in which CSS recommendation
the property is defined (CSS1 or CSS2).</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3">
<tr>
<th align="left" valign="top" width="25%">Pseudo-class</th>
<th align="left" valign="top" width="55%">Purpose</th>
<th align="left" valign="top" width="5%">IE</th>
<th align="left" valign="top" width="4%">F</th>
<th align="left" valign="top" width="4%">N</th>
<th align="left" valign="top" width="7%">W3C</th>
</tr>
<tr>
<td valign="top"><a href="pr_pseudo_active.asp">:active</a></td>
<td valign="top">Adds special style to an activated element</td>
<td valign="top">4</td>
<td valign="top">1</td>
<td valign="top">8</td>
<td valign="top">1</td>
</tr>
<tr>
<td valign="top"><a href="pr_pseudo_focus.asp">:focus</a></td>
<td valign="top">Adds special style to an element while the element has
focus</td>
<td valign="top">-</td>
<td valign="top">1.5</td>
<td valign="top">8</td>
<td valign="top">2</td>
</tr>
<tr>
<td valign="top"><a href="pr_pseudo_hover.asp">:hover</a></td>
<td valign="top">Adds special style to an element when you mouse over it</td>
<td valign="top">4</td>
<td valign="top">1</td>
<td valign="top">7</td>
<td valign="top">1</td>
</tr>
<tr>
<td valign="top"><a href="pr_pseudo_link.asp">:link</a></td>
<td valign="top">Adds special style to an unvisited link</td>
<td valign="top">3</td>
<td valign="top">1</td>
<td valign="top">4</td>
<td valign="top">1</td>
</tr>
<tr>
<td valign="top"><a href="pr_pseudo_visited.asp">:visited</a></td>
<td valign="top">Adds special style to a visited link</td>
<td valign="top">3</td>
<td valign="top">1</td>
<td valign="top">4</td>
<td valign="top">1</td>
</tr>
<tr>
<td valign="top"><a href="pr_pseudo_first-child.asp">:first-child</a></td>
<td valign="top">Adds special style to an element that is the first child of some other element</td>
<td valign="top">7</td>
<td valign="top">1</td>
<td valign="top">7</td>
<td valign="top">2</td>
</tr>
<tr>
<td valign="top"><a href="pr_pseudo_lang.asp">:lang</a></td>
<td valign="top">Allows the author to specify a language to use in a specified element</td>
<td valign="top">-</td>
<td valign="top">1</td>
<td valign="top">8</td>
<td valign="top">2</td>
</tr>
</table>
<br />
<hr />
<a href="css_positioning.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="css_pseudo_elements.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -