【问题标题】:How can I have two fixed width columns with one flexible column in the center?我怎样才能有两个固定宽度的列,中间有一个灵活的列?
【发布时间】:2014-07-10 18:37:24
【问题描述】:

我正在尝试设置具有三列的 flexbox 布局,其中左列和右列具有固定宽度,并且中心列弯曲以填充可用空间。

尽管为列设置了尺寸,但它们似乎仍会随着窗口的缩小而缩小。

有人知道怎么做吗?

我需要做的另一件事是根据用户交互隐藏右列,在这种情况下,左列仍将保持其固定宽度,但中心列将填充其余空间。

#container {
    display: flex;
    justify-content: space-around;
    align-items: stretch;
    max-width: 1200px;
}

.column.left {
    width: 230px;
}

.column.right {
    width: 230px;
    border-left: 1px solid #eee;
}

.column.center {
    border-left: 1px solid #eee;
}
<div id="container">
    <div class="column left">
        <p>Anxiety was a blog series that ran in the New York Times Opinion section from January 2012 to July 2013. It featured essays, fiction, and art by a wide range of contributors that explored anxiety from scientific, literary, and artistic perspectives.</p>
    </div>
    <div class="column center">
        <img src="http://i.imgur.com/60PVLis.png" width="100" height="100" alt="">
    </div>
    <div class="column right">
        Balint Zsako
        <br/> Someone’s Knocking at My Door
        <br/> 01.12.13
    </div>
</div>

这是一个 JSFiddle:http://jsfiddle.net/zDd2g/185/

【问题讨论】:

标签: html css flexbox


【解决方案1】:

您可以使用flex: 0 0 230px;,而不是使用width(这是使用flexbox时的建议),这意味着:

  • 0 = 不要增长(flex-grow 的简写)
  • 0 = 不要缩小(flex-shrink 的简写)
  • 230px = 从230px 开始(flex-basis 的简写)

这意味着:永远是230px

See fiddle,谢谢@TylerH

哦,这里不需要justify-contentalign-items

img {
    max-width: 100%;
}
#container {
    display: flex;
    x-justify-content: space-around;
    x-align-items: stretch;
    max-width: 1200px;
}
.column.left {
    width: 230px;
    flex: 0 0 230px;
}
.column.right {
    width: 230px;
    flex: 0 0 230px;
    border-left: 1px solid #eee;
}
.column.center {
    border-left: 1px solid #eee;
}

【讨论】:

  • 在当前的实现中(至少在 Chrome 中),您还需要确保中间列定义了flex-grow(如 1)。 Updated fiddle.
  • 为什么只是宽度不起作用?我确实需要 flex: 0 0 230px;?
  • flex-grow: 1 在 Chrome 53 中仍然需要流体列。否则,它不会占用其余的宽度。我想答案应该考虑到这个事实。
  • 对于任何仍然困惑的人,flex: 0 0 200px 的作用与width: 200px; flex-shrink: 0 相同。
  • 我已经在此处复制了 Fiddle 以防 Rudie 丢失它:jsfiddle.net/133rr51u
【解决方案2】:

尽管为列设置了尺寸,但它们似乎仍会随着窗口的缩小而缩小。

弹性容器的初始设置是flex-shrink: 1。这就是您的列正在缩小的原因。

您指定的宽度无关紧要 (it could be width: 10000px),flex-shrink 可以忽略指定的宽度并防止弹性项目溢出容器。

我正在尝试设置一个具有 3 列的 flexbox,其中左列和右列具有固定宽度...

您需要禁用收缩。以下是一些选项:

.left, .right {
     width: 230px;
     flex-shrink: 0;
 }  

.left, .right {
     flex-basis: 230px;
     flex-shrink: 0;
}

或者,按照规范的建议:

.left, .right {
    flex: 0 0 230px;    /* don't grow, don't shrink, stay fixed at 230px */
}

7.2. Components of Flexibility

鼓励作者使用flex 简写来控制灵活性 而不是直接使用它的速记属性,作为速记 正确重置任何未指定的组件以适应 common uses

更多详情在这里:What are the differences between flex-basis and width?

我需要做的另一件事是根据用户交互隐藏右列,在这种情况下,左列仍将保持其固定宽度,但中心列将填充其余空间。

试试这个:

.center { flex: 1; }

这将允许中心列消耗可用空间,包括其兄弟姐妹被移除时的空间。

Revised Fiddle

【讨论】:

  • flex: 1; 添加到中心div 就足够了,谢谢。
【解决方案3】:

与旧浏览器的兼容性可能会很麻烦,因此请注意。

如果这不是问题,请继续。 运行 sn-p。转到整页视图并调整大小。 Center 将自行调整大小,而不改变左右 div。

更改左右值以满足您的要求。

谢谢。

希望这会有所帮助。

#container {
  display: flex;
}

.column.left {
  width: 100px;
  flex: 0 0 100px;
}

.column.right {
  width: 100px;
  flex: 0 0 100px;
}

.column.center {
  flex: 1;
  text-align: center;
}

.column.left,
.column.right {
  background: orange;
  text-align: center;
}
<div id="container">
  <div class="column left">this is left</div>
  <div class="column center">this is center</div>
  <div class="column right">this is right</div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-07
    • 2017-10-27
    • 1970-01-01
    • 2020-04-20
    • 2011-11-09
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多