有点背景: 我正在打造一个页眉,它横跨整个网页。我希望将当前的用户名,注销按钮等悬浮在标题右侧,并且我希望徽标和一些navitems位于左侧。当浏览器窗口缩小时,左边的项目和右边的项目发生冲突,我不希望它们换行。我将header-container div设置为溢出:auto,所以我希望它开始滚动。CSS div左右浮动
问题: 即使当我有一个div float:left和div float:right同时使用display:inline-block和父空白:没有换行 - 换行!偶然,我发现我可以通过将另一个div与display:inline-block封装在一起工作。为什么是这样???
下面是我的代码和一个工作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链接
我想你忘了小提琴中的clearfix div,我认为second-wrapper拉伸以包含两个浮动divs的原因是因为它的display属性设置为inline-block。 –