【问题标题】:CSS3 flexbox adjust height of vertically aligned elementsCSS3 flexbox调整垂直对齐元素的高度
【发布时间】:2013-10-08 22:56:42
【问题描述】:

是否可以使.item-1高度灵活并调整.item-2高度?例如:

  • 如果.item-1 高度是10% 那么.item-2 高度是90%
  • 如果.item-1 高度是11% 那么.item-2 高度是89%

所以根据.item-1 的内容,我们应该调整它的大小。

HTML

<div class="container">
    <div class="item item-1">1</div>
    <div class="item item-2">2</div>
</div>

CSS

html,
body,
.container {
    height: 100%;
}
.container {
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;

    -webkit-flex-flow: column;
    -moz-flex-flow: column;
    -ms-flex-flow: column;
    flex-flow: column;
}
.item {
    background: #eee;
    margin: 1px;
}
.item-1 {
  -webkit-box-flex: 3 3 100%;
  -moz-box-flex: 3 3 100%;
  -webkit-flex: 3 3 100%;
  -ms-flex: 3 3 100%;
  flex: 3 3 100%;
}
.item-2 {
  -webkit-box-flex: 1 1 100%;
  -moz-box-flex: 1 1 100%;
  -webkit-flex: 1 1 100%;
  -ms-flex: 1 1 100%;
  flex: 1 1 100%;
}

JSFiddle:http://jsfiddle.net/vpetrychuk/SYZtg/2

【问题讨论】:

标签: css flexbox


【解决方案1】:

您无法使用 2009 版 Flexbox 属性执行此操作,因为没有任何东西可以与 flex-basis 属性相媲美(2009 年草案中的 box-flex 属性与现代草案中的 flex 属性不同,因为它只能接受单个浮点值,而不是 0-2 整数和 0-1 长度)。

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

html,
body,
.container {
  height: 100%;
}

.container {
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
}

.item {
  background: #eee;
  margin: 1px;
}

.item-1 {
  -webkit-flex: 1 1 10%;
  -ms-flex: 1 1 10%;
  flex: 1 1 10%;
}

.item-2 {
  -webkit-flex: 1 1 90%;
  -ms-flex: 1 1 90%;
  flex: 1 1 90%;
}

【讨论】:

  • 感谢您的回答。很遗憾,我们无法避免百分比 :( 你能建议一些解决方法吗?
  • 我不想将.item-1 高度硬编码为10%。它的高度应该取决于里面的内容。而且.item-2的高度也应该是灵活的100% - .item-1 height。
  • 它是灵活的。请参阅我上面链接的问题。 flex-basis 属性只是元素的理想大小。这与指定 width 不同。
猜你喜欢
  • 2017-06-25
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 2018-03-19
  • 2013-07-03
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
相关资源
最近更新 更多