我不明白为什么以下代码会在黄色div下方创建边距?以下代码是HTML5(请参阅doctype)。如果您删除了doctype,因此它不是HTML5,那么就没有空白(我喜欢)。关键是该代码必须在HTML5中(这是我的要求)。同样,黄色的div必须具有width:300px(这是我的要求)。
在这种情况下,如何摆脱黄色div下方的边距?
<!DOCTYPE html>
<html>
<body>
<div style="background-color:#00f;">
<div style="display:inline-block;width:300px;background-color:#ff0;">
<div style="float:left;">Hello</div>
<div style="float:right;">World</div>
</div>
</div>
</body>
</html>
您可以在以下网址查看上面的代码:https : //jsfiddle.net/gscz9ahq/
您在这里有预览:
我尝试使用“ clear:both;”;没有成功。
关于如何解决这个问题的任何想法?您可以修改jsfiddle代码以使其正常工作吗?
正是display: inline-block;
创造空间的原因。但是您的html有点过时,使用float时需要清除div。
<div style="background-color:#00f;">
<div class="clearfix" style="width:300px;background-color:#ff0;">
<div style="float:left;">Hello</div>
<div style="float:right;">World</div>
</div>
</div>
.clearfix:after {
content: "";
display: table;
clear: both;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句