Heim  >  Artikel  >  Web-Frontend  >  CSS width:100%和width:auto的区别_html/css_WEB-ITnose

CSS width:100%和width:auto的区别_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:42:26858Durchsuche

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>

 

 

 

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn