Blogger Information
Blog 10
fans 0
comment 0
visits 6437
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
选择器及内外边距的使用规则--2019年9月3日
小毅小开的博客
Original
782 people have browsed it
  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





Correction status:qualified

Teacher's comments:几乎所有的布局bug, 都与这二个家伙相关: padding, margin
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post