CSS伪类

1.链接伪类(注意顺序)

  • Link
  • Visited
  • Hover
  • Active

Tip:一个冒号(:)表示伪类,两个冒号(::)表示CSS3新增的伪元素。

2. :focus伪类 和 :target伪类

实例1:input:focus {border:1px solid blue;}

实例2: <h2 id="more_info">This is the information you are looking for.</h2>
      #more_info:target {background:#eee}

3.结构化伪类

3.1 :first-child和:last-child

格式:e:first-child

         e:last-child

实例:ol.results li:first-child{color:blue;}

3.2 :nth-child

格式:e:nth-child(n)

实例:li:nth-child(3)

说明:e表示元素名,n表示一个数值(也可以使用odd或even)

4.伪元素

4.1 ::first-letter伪元素

格式:e::first-letter
示例:p::first-letter {font-size:300%;}

提示:如果不用伪元素创建这个首字符放大效果,必须手工给该字母加上标签,
然后再为该标签应用样式。而伪元素实际上是替我们添加了无形的标签。

4.2 ::first-line伪元素

格式:e::first-line
示例:p::first-line {font-variant:small-caps;}
说明:选中文本段落(一般情况下是段落)的第一行。

4.3 ::before和::after伪元素

格式:
e::before
e::after
示例:
对标记:
<p class="age">25</p>
添加如下样式:
p.age::before {content:"Age: ";}
p.age::after {content:" years.";}
会得到如下结果:
Age: 25 years.

提示:如果标签中的内容是通过数据库查询生成的结果,那么用这种技巧再合适不过了。
因为所有结果都是数字,使用这两个伪元素可以在把数字呈现给用户时,加上说明
性文字。

原文地址:https://www.cnblogs.com/hlna/p/5477950.html