CSS——伪类

在a标签中运用最多:

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

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

3、a:hover {color: #FF00FF} /* 鼠标移动到链接上 */

4、a:active {color: #0000FF} /* 选定的链接 */

:first-child 伪类:选择元素的第一个子元素。

<div>
<p>These are the necessary steps:</p>
<ul>
<li>Intert Key</li>
<li>Turn key <strong>clockwise</strong></li>
<li>Push accelerator</li>
</ul>
<p>Do <em>not</em> push the brake at the same time as the accelerator.</p>
</div>

给定以下规则:

p:first-child {font-weight: bold;}
li:first-child {text-transform:uppercase;}

效果:第一个规则将作为某元素第一个子元素的所有 p 元素设置为粗体。第二个规则将作为某个元素(在 HTML 中,这肯定是 ol 或 ul 元素)第一个子元素的所有 li 元素变成大写。

提示:最常见的错误是认为 p:first-child 之类的选择器会选择 p 元素的第一个子元素。

在下面的例子中,选择器匹配所有 <p> 元素中的第一个 <i> 元素:

<html>
<head>
<style type="text/css">
p > i:first-child {
  font-weight:bold;
  } 
</style>
</head>

<body>
<p>some <i>text</i>. some <i>text</i>.</p>
<p>some <i>text</i>. some <i>text</i>.</p>
</body>
</html>

注释:p>i这是选择p的直接子代中的i标签。之后是:first-child满足这个条件的第一个元素。

参考:CSS 伪类 (Pseudo-classes)

原文地址:https://www.cnblogs.com/wuqiuxue/p/7772493.html