【发布时间】:2016-06-01 04:14:44
【问题描述】:
我有以下问题:我在 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;
}
【问题讨论】: