Fork me on GitHub
魔芋铃

CSS width:100%和width:auto的区别

width:100%和width:auto的区别

width:auto比较聪明,如果margin已经左右占去10px的空间,那么width给的值就是580px。

 

<style>
div{
width:600px;
overflow:auto;
background:#ccc;
}

p{
        width:auto;
        margin:10px;
        background:red;
}
</style>
</head>
<body>

<div>
    <p>123</p>
</div>

</body>
</html>

 

 

 

如果是width:100%,则说明p的width会得到600px就已经充满div区域,然后自己又有margin,所以会出现滚动条。

 

div{
width:600px;
overflow:auto;
background:#ccc;
}

p{
        width:100%;
        margin:10px;
        background:red;
}
</style>
</head>
<body>

<div>
    <p>123</p>
</div>

 

 

 

都是以父元素的宽度为参照。

posted @ 2015-06-22 17:23  魔芋铃  阅读(1107)  评论(0编辑  收藏  举报