flexbox:调整元素大小

时间:2015-03-29 06:59:31

标签: css resize flexbox

所以这是一个有希望的新手Flexbox挑战:

我有一个带有两个孩子的Flexbox(左和右)。我希望左边是500px宽,右边填充剩下的空间,除非右边是< 400px宽,在这种情况下我希望Left乖乖地开始收缩。好的解决方案?看起来这应该很简单,但我无法将头围绕哪些元素组合......

感谢 - 你!

PS:哦,顺便说一句:左边包含要裁剪的图像(而不是调整大小),所以这似乎是一个非常常见的问题:左边的图像,右边的文字:希望它们在所有屏幕宽度上看起来都很好......

2 个答案:

答案 0 :(得分:1)

以下是关于如何使用flex-basis和媒体查询来实现这一目标的想法:

html {
  color: #fff;
  font-family: sans-serif;
  height: 100%;
}

body {
  height: 100%;
  margin: 0;
}

.wrapper {
  display: flex;
  height: 100%;
  width: 100%;
}

.left {
  flex-basis: 500px;
  flex-shrink: 0;
  background-color: #f00;
}

.right {
  background-color: #00f;
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 0%;
}

@media (max-width: 900px) {
  .left {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 0%;
  }
}
<div class="wrapper">
  <div class="left">Left</div>
  <div class="right">Right</div>
</div>

答案 1 :(得分:1)

左边的图像是500px宽;右侧的文本设置为min-width:400px。在这里小提琴 - &gt; http://jsfiddle.net/LrscjLoq/

    <div class="flex-container">
    <div class="flex-image">
    </div>
    <div class="flex-box" style="min-width:400px">
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
    </div>
</div>

.flex-container {
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row;
justify-content: space-around;
}

.flex-image {
margin: 5px;
font-weight: bold;
font-size: 1.5em;
text-align: center;
flex-basis: 500px;
background-image: url(noimage.png);
}

.flex-box {
margin: 5px;
color: gray;
font-weight: bold;
font-size: 1.5em;
text-align: center;
-ms-flex: 1;
flex: 1;

}