如果div默认为100%宽度 [英] if a div is 100% width by default

查看:185
本文介绍了如果div默认为100%宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 div 的宽度为100%,我还应该放入 width:100%; ?我看了很多代码,它总是在那里,即使默认情况下 div 是100%。

If a div is 100% width, should I still put in width: 100%;? I look at a lot of code and it's always in there even though by default a div is 100%.

推荐答案

不,这样做实际上可能会导致问题。 100%不同于 auto 宽度指的是内容的宽度,不包括边框,内边距和边距。 auto 自动计算宽度,使得div的总宽度适合父级,但设置 100%会强制内容单独为100%,这意味着填充等将伸出div,使其大于父母。

No, doing so can actually cause problems. 100% is not the same as auto. width refers to the width of the content, excluding borders, padding and margins. auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent.

查看这个例子

这篇关于如果div默认为100%宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆