博客列表 >选择器及内外边距的使用规则--2019年9月3日

选择器及内外边距的使用规则--2019年9月3日

小毅小开的博客
小毅小开的博客原创
2019年09月08日 16:35:03767浏览
  1. 实例演示相邻选择器与兄弟选择器,并分析异同

实例

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

<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="style1.css">
    <title>选择器</title>
</head>

<body>
    <p>相邻选择器
        <ul>
            <li class="no1">1</li>
            <li>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>
    </p>
    <p>兄弟选择器
        <ul>
            <li class="no2">1</li>
            <li>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>
    </p>
</body>

</html>

运行实例 »

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

实例

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

ul li {
    list-style-type: none;
    width: 40px;
    height: 40px;
    background-color: lightblue;
    /* 文本水平居中 */
    text-align: center;
    /* 文本垂直居中 */
    line-height: 40px;
    /* 圆角 */
    border-radius: 50%;
    display: inline-block;
    /* 阴影 */
    box-shadow: 2px 2px 1px #888;
}

.no1 {
    background-color: rgb(212, 212, 34);
}

.no2 {
    background-color: rgb(40, 231, 206);
}


/* 相邻选择器 */

.no1+* {
    background-color: red;
}


/* 兄弟选择器 */

.no2~* {
    background-color: blue;
}

运行实例 »

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

QQ图片20190906230441.png

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

实例

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

<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="style1.css">
    <title>选择器</title>
</head>

<body>
    <p>
        <ul>
            <li class="no1">1</li>
            <li>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>
    </p>
</body>

</html>

运行实例 »

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

实例

/* 伪类选择器 */

ul :nth-child(5) {
    background-color: burlywood;
}

ul :nth-last-child(2) {
    background-color: yellowgreen;
}

运行实例 »

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

QQ图片20190908155450.png

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

实例

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

<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">
    <style>
        div {
            width: 500px;
            border: 1px solid red;
            background: lightgreen;
            padding: 50px;
        }
    </style>
    <title>Document</title>
</head>

<body>
    <div class="box">
        <img src="C:/phpstudy_pro/WWW/html/1.png" alt="" width="300px">
    </div>
</body>

</html>

运行实例 »

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

QQ图片20190908161526.png

解决方案:添加box-sizing语句

实例

    <style>
        div {
            width: 500px;
            border: 1px solid red;
            background: lightgreen;
            box-sizing: border-box;
            padding: 50px;
        }
    </style>

运行实例 »

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


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

实例

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

<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">
    <style>
    .box1 {
            width: 200px;
            height: 200px;
            background-color: lightblue;
            margin-bottom: 30px;
        }
        
        .box2 {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            margin-top: 30px;
        }
    }
    </style>
    <title>Document</title>
</head>

<body>
    <div class="box1">
    </div>
    <div class="box2">
    </div>
</body>

</html>

运行实例 »

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

QQ图片20190908163122.png

解决方案是只需要设置一个margin值。

实例

    .box1 {
            width: 200px;
            height: 200px;
            background-color: lightblue;
            margin-bottom: 60px;
        }
        
        .box2 {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
}

运行实例 »

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


QQ图片20190908163150.png





声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议