系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 > CSS/HTML > 详细页面

CSS中width和height的默认值auto与%案例详解

时间:2020-10-11来源:www.pcxitongcheng.com作者:电脑系统城

结论

  • width的%:定义基于包含块(父元素)宽度的百分比宽度,会突破父级的限制
  • width的auto:尽量被父级包裹
  • height的%:基于包含它的块级对象的百分比高度,会突破父级的限制
  • height的auto:尽量被父级包裹

案例

一定复制代码跑一遍,花几分钟体会一下尽量被父级包裹与突破父级的限制

width案例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        * {
            margin: 0;padding: 0;
        }
        body {
            background: #dcdcdc;
        }
        .box {
            width: 400px;
            border: 3px solid red;
            padding: 0 50px;
        }
        .box1 {
            width: auto;
            height: 100px;
            background: pink;
            padding: 0 50px;
            margin: 0 50px;
            border-width: 0 50px;
            border-style: solid;
            border-color: green;
        }
        .box2 {
            width: 100%;
            height: 100px;
            background: gold;
            padding: 0 50px;
            margin: 0 50px;
            border-width: 0 50px;
            border-style: solid;
            border-color: green;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html>

height案例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        * {
            margin: 0;padding: 0;
        }
        body {
            background: #dcdcdc;
        }
        .box {
            width: 400px;
            border: 3px solid red;
            padding: 50 0px;
            height: 400px;
        }
        .box1 {
            width: 200px;
            height:auto;
            background: pink;
            padding: 50px 0px;
            margin: 50px 0px;
            border-width: 50px 0px;
            border-style: solid;
            border-color: green;
        }
        .box2 {
            width: 200px;
            height:100%;
            background: gold;
            padding: 50px 0px;
            margin: 50px 0px;
            border-width: 50px 0px;
            border-style: solid;
            border-color: green;
        }
      
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
</div>
</body>
</html>

到此这篇关于CSS中width和height的默认值auto与%案例详解的文章就介绍到这了,更多相关css width和height默认值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载