博主信息
博文 40
粉丝 0
评论 0
访问量 36532
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS中的常用选择器-2019-04-25
小人物的博客
原创
1441人浏览过
实例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS中的常用选择器</title>
    <style>
        ul {
            border:1px solid red;
            margin: 0;
            padding-left: 0;

        }
        ul li {
            list-style: none;
            width:40px;
            Height:40px;
            background-color: aquamarine;
            /*background-position: 10px 10px;*/
            display: inline-block; /*块级元素显示为一行*/
            font-size: 20px;
            margin-left: 5px;
            border-radius: 40%;  /*四角弧度*/
            box-shadow: 2px 2px 10px black;
            text-align: center; /*文字居中*/
            line-height: 40px;
        }
         .A { /*类选择器*/
            background-color:red;
        }
        .B,.C { /*群组选择器*/
            background-color: burlywood;
        }
        .C + * { /*相邻选择器 下一个*/
            background-color: darkgray;
        }
        .E ~ * { /*兄弟选择器 水平以后*/
            background-color: white;
        }
        ul :first-child { /*子类选择器 ul第一个子元素*/
            color: greenyellow;
        }
        ul :last-child { /*子类选择器 ul最后一个子元素*/
            color: BLUE;
        }
        ul :nth-child(3) { /*子类选择器 ul顺数第3个子元素*/
            color: yellow;
        }
        ul :nth-last-child(2) {/*子类选择器 ul倒数第2个子元素*/
            color: greenyellow;
        }
        li:nth-of-type(2){  /*元素选择器 li顺数第2个子元素*/
            color:white;
        }
    </style>
</head>
<body>
<ul>
    <li class="A">1</li>
    <li class="B">2</li>
    <li class="C">3</li>
    <li class="D">4</li>
    <li class="E">5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
</ul>
</body>
</html>
运行实例 »
点击 "运行实例" 按钮查看在线实例

学习总结:

1、层级/后代选择器

   ul li {...}

2、属性选择器

  #id {...}

  <li id='id' >{...}

3、群组选择器

#id1 , #id2 {...}

4、相邻选择器

#id + li {...}  相邻的第一个li

#id + * {...}  相邻的第一个元素

5、兄弟/同级选择器

#id ~ * {...}  水平以后的全部元素

6、伪类:子类选择器

ul :first-child{...}  ul中第一个子元素

ul :last-child{...}  ul中最后一个子元素

ul :nth-child(2){...}  ul中第二个子元素,关注点在“元素位置”

ul :nth-last child(3){...}  ul中倒数三个子元素

7、伪类:类型选择器

li :nth_of _type(2){...} li 中第二个子元素,关注点在“元素类型”

批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学