width、height为auto或者100%的区别

2019-10-09  本文已影响0人  手指乐

规则

实例

<!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">
    <title>Document</title>
    <style>
        .main {
            background: red;
            height: 40px;
            width: 700px;
            font-size: 30px;
            color: #fff;
            height: 80px;
            padding-left: 20px;
        }
         
        .right {
            background: green;
            height: 40px;
            width: auto;
            padding-left: 40px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <div class="right">right</div>
    </div>
</body>
 
</html>

效果图:

<!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">
    <title>Document</title>
    <style>
        .main {
            background: red;
            height: 40px;
            width: 700px;
            font-size: 30px;
            color: #fff;
            height: 80px;
            padding-left: 20px;
        }
         
        .right {
            background: green;
            height: 40px;
            width: 100%;
            padding-left: 40px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <div class="right">right</div>
    </div>
</body>
 
</html>

效果图:


如果right再加一个padding-right:40px,green区域要再加长40px

高度的设置类似于宽度,我们结合flexbox弹性布局看下效果:

<!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">
    <title>Document</title>
</head>
<style>
    .flex-container {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        height: 200px;
    }
    /* 以下为辅助样式 */
    
    .flex-container {
        background-color: #F0f0f0;
    }
    
    .flex-container .flex-item {
        padding: 20px;
        height: 200px;
        background-color: #B1FF84;
    }
    
    .flex-container .flex-item:first-child {
        background-color: #F5DE25;
    }
    
    .flex-container .flex-item:last-child {
        background-color: #90D9F7;
    }
</style>

<body>

    <div class="flex-container">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
    </div>
</body>

</html>

改为width:auto

想让文字垂直居中,设置line-height

<!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">
    <title>Document</title>
</head>
<style>
    .flex-container {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        height: 200px;
        line-height: 200px;
    }
    /* 以下为辅助样式 */
    
    .flex-container {
        background-color: #F0f0f0;
    }
    
    .flex-container .flex-item {
        padding: 20px;
        height: auto;
        background-color: #B1FF84;
    }
    
    .flex-container .flex-item:first-child {
        background-color: #F5DE25;
    }
    
    .flex-container .flex-item:last-child {
        background-color: #90D9F7;
    }
</style>

<body>

    <div class="flex-container">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
    </div>
</body>

</html>

发现并未居中,因为line-height的leading加在padding-top下面,所以居中位置会往下偏移20px

把上下的padding去掉后就可以垂直居中了

上一篇 下一篇

猜你喜欢

热点阅读