零基础学习CSS(8)——:root、:empty、:first-chlid、:last-chlid、:only-chlid、:frist-of-type、:last-of-type等

官方资料

鱼C课程案例库:https://ilovefishc.com/html5/
html5速查手册:https://man.ilovefishc.com/html5/
css速查手册:https://man.ilovefishc.com/css3/

学习正文

:root 选择器:https://man.ilovefishc.com/pageCSS3/dotroot.html
:empty 选择器:https://man.ilovefishc.com/pageCSS3/dotEmpty.html
:first-chlid 选择器:https://man.ilovefishc.com/pageCSS3/dotfirst-child.html
:last-chlid 选择器:https://man.ilovefishc.com/pageCSS3/dotlast-child.html
:only-of-type 选择器:https://man.ilovefishc.com/pageCSS3/dotonly-of-type.html
:only-chlid 选择器:https://man.ilovefishc.com/pageCSS3/dotonly-child.html
:frist-of-type 选择器:https://man.ilovefishc.com/pageCSS3/dotfirst-of-type.html
:last-of-type 选择器:https://man.ilovefishc.com/pageCSS3/dotlast-of-type.html

<!DOCTYPE html>
<html>
<head>
    <title>结构伪类选择器</title>
    <meta charset="utf-8">
    <style type="text/css">
    /* 设置根元素的样式 */
        :root {
            background-color: red;
        }
    </style>
</head>
<body>
    <p>I love FishC.</p>
</body>
</html>

image.png

:empty 选择器用于匹配没有子元素的元素标签:

<!DOCTYPE html>
<html>
<head>
    <title>结构伪类选择器</title>
    <meta charset="utf-8">
    <style type="text/css">
    /* 设置根元素的样式 */
        :empty {
            background-color: red;
            width: 100px;
            height: 20px;
        }
    </style>
</head>
<body>
    <p></p>
    <p>I love FishC.</p>
    <p></p>
</body>
</html>

image.png

:first-chlid 选择器和 :last-chlid 选择器用于修改第一个和最后一个对应元素的样式:

<!DOCTYPE html>
<html>
<head>
    <title>结构伪类选择器</title>
    <meta charset="utf-8">
    <style type="text/css">
    /* 设置第一个p元素和最后一个p元素的样式 */
        p:first-child {
            border: 2px solid green;
        }
        p:last-child {
            border: 2px solid red;
        }
    </style>
</head>
<body>
    <p>I love FishC.</p>
    <p>I love FishC.</p>
    <p>I love FishC.</p>
</body>
</html>

image.png

:only-chlid 选择器用于选取父元素中唯一的子元素:

<!DOCTYPE html>
<html>
<head>
    <title>结构伪类选择器</title>
    <meta charset="utf-8">
    <style type="text/css">
    /* 设置第一个p元素和最后一个p元素的样式 */
        p:first-child {
            border: 2px solid green;
        }
        p:last-child {
            border: 2px solid red;
        }
        span:only-child {
            border: 1px solid pink;
        };
    </style>
</head>
<body>
    <p>I <span>love</span> FishC.</p>
    <p>I <span>love</span> FishC.</p>
    <p>I <span>love</span> FishC.</p>
</body>
</html>

image.png

:only-of-type 选择器用于选取父元素中唯一类型的子元素:

<!DOCTYPE html>
<html>
<head>
    <title>结构伪类选择器</title>
    <meta charset="utf-8">
    <style type="text/css">
        /* :only-of-type 选择器匹配父元素下唯一类型的子元素 */
        /* :only-child 和 only-of-type 的主要区别:only-of-type的父元素下可以有其他元素,only-child 不行*/
        strong:only-child {
            border: 2px solid pink;
        };
    </style>
</head>
<body>
    <p>I <span>love</span> <strong>FishC</strong></p>
    <p>I <span>love</span> FishC.</p>
    <p>I <span>love</span> FishC.</p>
</body>
</html>

image.png

:first-of-type 和 last-of-type 用于匹配父元素下相同类型的第一个和最后一个:

<!DOCTYPE html>
<html>
<head>
    <title>结构伪类选择器</title>
    <meta charset="utf-8">
    <style type="text/css">
        /* first-of-type 和 last-of-type */
        p:first-of-type {
            border: 2px solid green;
        }
        p:last-of-type {
            border: 2px solid red;
        }
    </style>
</head>
<body>
    <span>这才是父元素下的第一个元素</span>
    <p>I <strong>love</strong> <span>FishC</span></p>
    <p>I <span>love</span> FishC.</p>
    <p>I <span>love</span> FishC.</p>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值