【问题标题】:Multiple divs side by side with percentage and fixed width inline多个 div 并排,百分比和固定宽度内联
【发布时间】:2013-06-27 09:04:34
【问题描述】:

我有一个宽度为 100% 的父 div。如何在父 div 的每一侧放置一个宽度为 40px 的 div,这样我就有 3 个内联 div 并且父 div 具有整个视口宽度 - (2 * 40px)。

我喜欢浮动:在所有 3 个或每一侧都使用一个 span,并且父 div 有 display:inline-block(考虑宽度:100% 但不换行)我做不到正常工作。

我需要兼容 IE8+ 和最新的 FF/Chrome/Safari

这是我的代码示例:http://codepen.io/helloworld/pen/IGsoe

<div id="navBar">
    <div>
      <div style="height:100%;width:100%;background:lightgray;padding:5px;">
        <div data-bind="text: number"></div>        
      </div>
    </div>
    <div>
      <div style="height:100%;width:100%;width:100%;background:lightgray;padding:5px;">
        <div data-bind="text: number"></div> 
      </div>
    </div>
    <div>
      <div style="height:100%;width:100%;background:lightgray;padding:5px;">
        <div data-bind="text: number"></div> 
      </div>
    </div>
    <div>
      <div style="height:100%;width:100%;background:lightgray;padding:5px;">
        <div data-bind="text: number"></div> 
      </div>
    </div>
    <div>
      <div style="height:100%;width:100%;background:lightgray;padding:5px;">
        <div data-bind="text: number"></div> 
      </div>
    </div>
    <div style="clear:both;"></div>
</div>


:html, body{
  width:100%;
  height:100%;
  padding:0;
  margin:0;
  font-family:arial;
}

*{
  box-sizing:border-box;
}

#navBar {
    width: 100%;
    height: 80px;     
}


#navBar > div {
    text-align:left;
    font-size:20px;
    width: 20%;
    height: 100%;
    border-width: 1px;
    border-color: #000;
    border-style: solid;    
    float:left;
    margin-left:
}

【问题讨论】:

    标签: css html


    【解决方案1】:

    使用浮动,经典是浮动首先(右/左),然后在通量中居中,溢出:隐藏; http://codepen.io/anon/pen/iGvHd

    否则,你有 display:table;这更加连贯和坚实。 IE8可以理解。 http://codepen.io/anon/pen/sBcjp

    【讨论】:

    • 我猜你为什么 float:right 最右边的 div 的原因是因为中间 div 的 width:100% 强制一个 div 的 float:left 换行?但是 float:right div 被强制向右移动。这个假设正确吗?是的 IE8 似乎有一些浮动错误...
    • @Pascal ,是的,在浮动之前,如果元素在流中之前,则假设该元素要换行。 IE过去总是有一些奇怪的行为:)
    • 当左/右 div 的宽度为 40px 时,为什么我在这些 div 中放置文本时只能看到这个宽度?
    • 因为设置为40px,你可以增加宽度或者不给任何
    • 啊,我一定是瞎了。我再次删除了左侧,宽度保持不变???现在不要问我在 LOL 之前做了什么……再次感谢。
    猜你喜欢
    • 2014-08-28
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多