搜索
博主信息
博文 14
粉丝 0
评论 0
访问量 12304
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
常用选择器与布局原理20190904作业
德性的博客
原创
768人浏览过

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

实例

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

<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">
    <title>相邻选择器与兄弟选择器</title>
    <link rel="stylesheet" href="demo03a.css">
</head>

<body>
    <ul>
        <li class="testclass">1</li>
        <li id="testid01">2</li>
        <li class="testclass">3</li>
        <li class="testclass">4</li>
        <li id="testid02">5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
    </ul>
</body>

</html>

运行实例 »

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

实例

/* demo03a.css */

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

ul li {
    list-style-type: none;
    width: 40px;
    height: 40px;
    background-color: wheat;
    border: 1px solid black;
    /* 水平和垂直居中 */
    text-align: center;
    line-height: 40px;
    border-radius: 50%;
    /* 将块元素转换为内联元素 */
    display: inline-block;
    box-shadow: 2px 2px 1px #777;
}


/* 相邻选择器 */

#testid01+* {
    background-color: blue;
}


/* 兄弟选择器 */

#testid02~* {
    background-color: red;
}

运行实例 »

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


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

实例

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

<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">
    <title>子类选择器与类型选择器</title>
    <link rel="stylesheet" href="demo03b.css">
</head>

<body>
    <ul>
        <li class="testclass">1</li>
        <li id="testid01">2</li>
        <li class="testclass">3</li>
        <li class="testclass">4</li>
        <li id="testid02">5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
    </ul>
</body>

</html>

运行实例 »

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


实例

/* demo03b.css */

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

ul li {
    list-style-type: none;
    width: 40px;
    height: 40px;
    /* background-color: wheat; */
    border: 1px solid black;
    /* 水平和垂直居中 */
    text-align: center;
    line-height: 40px;
    border-radius: 50%;
    /* 将块元素转换为内联元素 */
    display: inline-block;
    box-shadow: 2px 2px 1px #777;
}


/* 伪类:子元素选择器 */

ul :first-child {
    background-color: #ff0;
}

ul :nth-child(6) {
    background-color: #0ff;
}


/* 伪类:类型选择器 */

ul li:nth-of-type(9) {
    background-color: #0f0;
}

运行实例 »

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

三、实例演示:padding 对盒子大小的影响与解决方案, 使用宽度分离或box-sizing

实例

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

<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">
    <title>padding对盒子大小的影响与解决方案</title>
    <style>
        /* box-sizing设置为border-box,限定基准为边框 */
        
        .box1 {
            width: 400px;
            box-sizing: border-box;
            padding: 50px;
            background-color: #f00;
            border: 1px solid blue;
        }
    </style>
</head>

<body>
    <div class="box1">
        <img src="./test01.png" alt="中秋快乐" ">
</div>
</body>

</html>

运行实例 »

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

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

实例

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

<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">
    <title>margin中的同级塌陷, 嵌套传递与自动挤压</title>
    <style>
        /* box1与box2同级塌陷 */
        
        .box1 {
            width: 200px;
            height: 200px;
            background-color: #00f;
            margin-bottom: 50px;
        }
        
        .box2 {
            width: 300px;
            height: 250px;
            background-color: #f00;
            margin-top: 30px;
        }
        /* box3与box4嵌套传递*/
        
        .box3 {
            width: 200px;
            height: 200px;
            background-color: lightgreen;
        }
        
        .box4 {
            width: 150px;
            height: 100px;
            background-color: lightcoral;
            margin-top: 30px;
        }
        
        .box5 {
            width: 250px;
            height: 100px;
            background-color: #ffff00;
            margin: 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>

运行实例 »

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


批改状态:合格

老师批语:你是刚报名的学员吗? 作业进度不对, 如果是刚报名的, 建议将重点放在眼下正在学习的内容,之前的可以抽空进行复习, 作业可根据你的实际情况, 有选择的完成
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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

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