css-float - 填充剩余的水平空间(或最大宽度)并在最小宽度上换行

标签 css-float fluid-layout css flexbox

#fit#wrap 说明了我希望一个元素具有的两种不同的行为,具体取决于情况。如果有空间,该元素应该像 #fit 一样工作,但如果没有足够的空间,则应该像 #wrap 一样工作。

http://jsfiddle.net/benstenson/dN8VJ/

<div id="print">
    Printable
</div>
<div id="fit">
    Looks good on same line
</div>
<div id="wrap">
    Looks good on new line
</div>

CSS

body{overflow:hidden;padding:1em;}
div
{
    /*display:inline-block;*/
    float:left;
    height:1in;
    margin:.5em;text-align:center;line-height:1in;
    white-space:nowrap;box-shadow:0 0 .5em gray;
}
#print
{
    width:5in;
    background-color:black; color:white;
}
#fit
{
    /* when on same line
       Size to min-width
       OR fill remaining space
       (like flexible box style).
       Either way is fine.
    */
    min-width:3in;
    background-color:gold;
}
#wrap
{
    /* when wrapped to next line */
    /* fill 100% OR to max width */
    width:100%;
    min-width:3in;
    max-width:5in;
    background-color:orange;
}

最佳答案

您正在寻找的 Flexbox,但大多数支持 Flexbox 的浏览器支持换行。这样做的有 IE10、Chrome 和 Opera。

http://codepen.io/cimmanon/pen/lqrGB

<div class="container">
  <div id="print">
    Printable
  </div>
  <div id="either">
    Looks good on either line
  </div>
</div>

.container {
  display: -ms-flexbox;
  display: -webkit-flex;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}
@supports (flex-wrap: wrap) {
  .container {
    display: flex;
  }
}
.container div {
  height: 1in;
  margin: .5em;
  text-align: center;
  line-height: 1in;
  white-space: nowrap;
  box-shadow: 0 0 .5em gray;
}

#print {
  /*-webkit-flex: 1 5in;
  -ms-flex: 1 5in;
  flex: 1 5in;*/
  width: 5in;
  background-color: black;
  color: white;
}

#either {
  -webkit-flex: 1 3in;
  -ms-flex: 1 3in;
  flex: 1 3in;
  max-width: 5in;
  background-color: gold;
}

关于css-float - 填充剩余的水平空间(或最大宽度)并在最小宽度上换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16365621/

相关文章:

javascript - 突出显示菜单栏航点

css - 我的 div 不会向左或向右浮动?

css - 将流体图像垂直居中放置在流体容器中

javascript - 窗口大小调整时网页布局中断

css - 仅在移动 View 上隐藏 div 标签?

html - 使用流体中心实现 3 列布局的正确方法是什么

javascript - 尝试通过单击另一个页面上的链接访问页面上的部分时出错

html - 填充底部在 Firefox 和 IE11 中不起作用

html - 如何使 float 链接可点击整行?

jquery - 容器内的底部 float div(如 Facebook 消息中) - 采取 2