css伪类选择器

本文详细介绍了CSS中的动态伪类(:link,:visited,:hover,:active,:focus)以及结构伪类(:first-child,:last-child,:nth-child,:first-of-type,:last-of-type,:nth-of-type)的用法和示例,帮助开发者更好地理解和应用这些选择器来控制网页样式。
摘要由CSDN通过智能技术生成

1.动态伪类

  1. :link 超链接未被访问的状态。
  2. :visited 超链接被访问后的状态。
  3. :hover 鼠标悬停在元素上的状态。
  4. :active 元素激活的状态。

    激活:按下鼠标不松开。注意:lvha顺序原则。

  5. :focus 获取焦点的元素。

代码示例:

<body>
    <a href="http://www.baidu.com">去百度</a>
    <a href="http://www.jd.com">去京东</a>
    <br>
    <input type="text">
</body>
<style>
        /* 选中的是未访问过得a元素 */
        a:link{
            color: coral;
        }
       /* 选中的是访问过得a元素 */
        a:visited{
            color: darkgray;
        }
        /* 选中的是鼠标悬浮的a元素 */
        a:hover{
            color: darkred;
        }
        /* 选中的是激活中的a元素 */
        a:active{
            color:darksalmon;
        }
        input:focus{
            color: orange;
            background-color: green;
        }
    </style>

效果图:

2.结构伪类

1. :first-child 所有兄弟元素中的第一个

div>p:first-child{
            color: red;
        } 

2. :last-child 所有兄弟元素中的最后一个

 div>p:last-child{
            color: red;
        }

3. :nth-child 所有兄弟元素中的第n个

 div>p:nth-child(3){
            color: red;
        }

只显示偶数个

 div>p:nth-child(2n){
            color: red;
        }

只显示奇数个

 div>p:nth-child(2n+1){
            color: red;
        }

显示前n个(例子数字5)

 div>p:nth-child(-n+5){
            color: red;
        }

4. :first-of-type 所有同类型兄弟元素中的第一个

 div>p:first-of-type{
            color: red;
        }

5. :last-of-type 所有同类型兄弟元素中的最后一个

div>p:last-of-type{
            color: red;
        }

5. :nth-of-type 所有同类型兄弟元素中的第n个

  div>p:nth-of-type(2){
             color: aquamarine;
        }

html

  <div>
        <p>1</p>
        <p>2</p>
        <p>3</p>
        <p>4</p>
        <p>5</p>
        <p>6</p>
        <p>7</p>
        <p>8</p>
        <p>9</p>
        <p>10</p>
    </div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值