问题描述
如果 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%宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!