【发布时间】:2014-11-24 16:28:57
【问题描述】:
我正在尝试使用display:inline-block、height:50px 和margin 25px 顶部和底部创建跨度。包装器 div 的高度为 100px,并具有 overflow: auto 属性。
即使 span 的高度 + margin-top + margin-bottom = wrapper div 的高度,也会出现垂直滚动。
我知道给出 min-height 可以解决问题,但我很想知道在整个过程中我在哪里丢失了几个像素。
请查看fiddle here 或下面的演示:
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
#myToolbar {
background: lightgrey;
width: 100%;
height: 100px;
overflow: auto;
white-space: nowrap;
}
.toolbar-item {
display: inline-block;
width: 15%;
height: 50px;
margin: 25px 2.5%;
background-color: green;
box-sizing: border-box;
border: 0;
}
<div id="myToolbar">
<span class="toolbar-item"></span><span class="toolbar-item"></span><span class="toolbar-item"></span><span class="toolbar-item"></span><span class="toolbar-item"></span>
</div>
【问题讨论】:
-
inline-block元素受line-height影响。你试过making the objects display typeblockand making themfloatleft instead吗? -
在你的#myToolbar 类中添加溢出:隐藏;
-
@Marty 让它们浮动会忽略空格:不换行。无论如何感谢您的建议。通过添加垂直对齐解决了问题:顶部