一些背景信息:我正在处理一个跨越整个网页顶部的标题。我希望当前用户名、注销按钮等靠右对齐,而标志和一些导航项则在左侧。当浏览器窗口缩小,使得左侧和右侧的项目相遇时,我不希望它们换行。我将header-container div设置为overflow:auto,所以我希望它开始滚动。
问题是:即使我有一个float:left的div和一个float:right的div,两者都有display:inline-block,并且父元素具有white-space:no wrap-它们还是换行了!!!偶然间我发现,通过再包含一个也具有display:inline-block的div,可以让它正常工作。这是为什么?
下面是我的代码和一个工作的jsfiddle,展示了工作的设置和换行的设置。我不明白为什么需要额外的div。 http://jsfiddle.net/klyngbaek/tNHLL/9/
当我缩小窗口时,我不希望第二个蓝色矩形掉到新行
HTML:
<h2>With extra inline-block dive. The blue rectangles do not wrap.</h2>
<div class="wrapper nowrap">
<div class="second-wrapper">
<div class="floating float-left">[logo] | home | [navitem1] | [navitem2]</div>
<div class="floating float-right">[username] | logout</div>
</div>
</div>
<h2>Without extra inline-block dive. The blue rectangles do wrap.</h2>
<div class="wrapper nowrap">
<div class="floating float-left">[logo] | home | [navitem1] | [navitem2]</div>
<div class="floating float-right">[username] | logout</div>
<div class="clearfix"></div>
</div>
CSS(层叠样式表):
.wrapper {
border:#333;
margin-bottom:;
}
.second-wrapper {
border:;
display:inline-block;
min-width: 100%;
}
.nowrap {
white-space: nowrap;
}
.clearfix {
clear: both;
}
.floating {
background: lightblue;
border:;
height:;
padding:}
.float-left {
float: left;
}
.float-right {
float: right
}
也许有更好的方法来实现我的目标。提前谢谢!编辑:更新了jsfiddle链接。