width:auto 和 width:100%有什么区别

width:auto 和 width:100%有什么区别

宽度计算不同

<div class="parent">
	<span class="child">content</span>
</div>
.parent {
    width: 800px;
}
  • 当子元素 width: auto; 时

    width: auto = 'width' + 'padding-left/right' + 'border-left/right' + 'margin-left/right' < 父元素宽度

    即无论子元素有没有内外边距或者边框,宽度始终不会超过父元素

  • 当子元素 width: 100%; 时

    width: 100% = '父元素width' + 'padding-left/right' + 'border-left/right' + (margin-left/right)

    即当子元素有了内外边距或者边框之后,宽度会溢出父元素

本文作者:伏月廿柒

本文链接:https://www.cnblogs.com/by0627/p/16028718.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   伏月廿柒  阅读(90)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起