博主信息
博文 5
粉丝 0
评论 0
访问量 3234
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS基础-2019年9月3日20时
贝瀚企管Jason的博客
原创
584人浏览过

1、实例演示相邻选择器与兄弟选择器,并分析异同。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="../css/style1.css">
    <title>Document</title>
</head>
<body>
    <div id="box1"></div>
    <ul>
        <li>1</li>
        <li id="bg_red">2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
    </ul>
</body>
</html>

CSS代码:

ul {

    margin: 0;

    padding-left: 0;

    border: 1px dashed red

}
ul>li {
    list-style: none;
    width: 40px;
    height: 40px;
    background-color: wheat;
    text-align: center;
    line-height: 40px;
    border-radius: 50%;
    display: inline-block;
    box-shadow: 2px 2px 3px #999
}

/*相邻选择器:选择li的id=bg_red相邻的那个*/
#bg_red+* {
    background: yellow;
}

/*兄弟选择器:选择li的id=bg_red后面所有的同级li*/
#bg_red~* {
    background: lightgreen;
}

分析:相邻选择器和兄弟选择都是从指定id的选择器后面开始选择。不同的时相邻只选择后面一个,兄弟选择后者的是后面一批。

2、实例演示nth-child()和nth-of-type()选择器并分析异同。

ul {
    margin: 0;
    padding-left: 0;
    border: 1px dashed red
}

ul>li {
    list-style: none;
    width: 40px;
    height: 40px;
    background-color: wheat;
    text-align: center;
    line-height: 40px;
    border-radius: 50%;
    display: inline-block;
    box-shadow: 2px 2px 3px #999
}


/*nth-chlid()*/

ul :nth-child(3) {
    background-color: lightcoral;
}


/*nth-of-type()*/

ul li:nth-of-type(6) {
    background-color: orangered;
    color: white;
}

p:nth-of-type(2) {
    background-color: lightcoral;
}

3、实例演示parding对盒子大小影响与解决方案,使宽度分离或box-sizing。

***g1 {
    width: 300px;
    box-sizing: border-box;
    padding: 50px;
    background-color: antiquewhite;
    border: 1px solid #ff9900
}

4、实例演示margin中同级塌陷,嵌套和传递自动挤压,并提出解决方案或应用场景。

 

实例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=s, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box1 {
            width: 200px;
            height: 200px;
            background-color: antiquewhite;
            margin-bottom: 50p;
        }
        
        .box2 {
            width: 200px;
            height: 200px;
            background-color: lightgreen;
            margin-top: 30px;
        }
        
        .box3 {
            width: 300px;
            height: 250px;
            background-color: lightblue;
            padding-top: 50px;
            margin-top: 30px;
        }
        
        .box4 {
            width: 200px;
            height: 200px;
            background-color: lightgreen;
        }
        
        .box5 {
            width: 200px;
            height: 200px;
            background-color: lightblue;
            margin: 30px auto;
        }
    </style>
</head>

<body>
    <!--  同级塌陷 -->
    <div class="box1"></div>
    <div class="box2"></div>
    <!--嵌套传递-->
    <div class="box3">
        <div class="box4"></div>
    </div>
    <!--自动挤压-->
    <div class="box5"></div>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

批改状态:合格

老师批语:padding , margin 非常有意思的, 多做些练习
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学