我有以下问题:我正在一个wrapper-div(.wrapper)内创建一个内嵌块元素(.content)。如果.content-div中有内容,则一切正常。但是,如果我从.content-div中删除内容,则会在inline-block-div下添加一个空格。如何删除空行内块空格下的空间(为什么它有吗?)
我不知道为什么发生这种情况,以及如何正确解决它。请注意,在手动删除我的代码中的所有空格和换行符后,问题仍然存在,但将font-size设置为0会有所帮助。
另外,将vertical-align:top设置为.content-div会有所帮助。我不确定为什么。
最好的修复方法是什么?为什么会发生?
小提琴:https://jsfiddle.net/cjqvcvL3/1/
<p>Works fine:</p>
<div class="wrapper">
<div class="content">not empty</div>
</div>
<p>Not so much:</p>
<div class="wrapper">
<div class="content"></div>
</div>
.wrapper {
background-color: red;
margin-bottom: 20px;
/* font-size: 0; *//* this would fix it, but why? (problem persists after manually removing all spaces and breaks) */
}
.content {
display: inline-block;
height: 20px;
width: 200px;
background-color: green;
/* vertical-align: top; *//* this would fix it, but why? */
}
更新
我已经把一个新的小提琴。这应该更好地说明我的问题。我如何摆脱textarea下面的绿线?
https://jsfiddle.net/cjqvcvL3/7/
<div class="content"><textarea>Some Content</textarea></div>
.content {
display: inline-block;
background-color: green;
}
这里有一个解释:http://stackoverflow.com/a/20107103/4334348 – silviagreen