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

时间:2015-06-22 17:50:14   收藏:0   阅读:255

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>

 

 

 

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

原文:http://www.cnblogs.com/moyuling/p/4593546.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!