【问题标题】:flex model: second row with width of element of first rowflex模型:第二行,第一行元素的宽度
【发布时间】:2016-03-03 23:29:20
【问题描述】:

我不完全理解 flex 模型。在此示例中,第一行(第一和第二)的元素应显示在一行中:第一个元素 200 像素,第二个元素应具有剩余空间。

	.wrapper {
	  display: flex;
          width: 100%;
	}
	.first {
	  width: 200px;
	  background: red;
	}
	.second {
	  flex: 1;
	  background: blue;
	}
	
<div class="wrapper">
  <div class="first">first left</div>
  <div class="second">first right</div>
</div>

现在我想添加第三个元素,它应该放在第二个元素的正下方,宽度与第二个元素相同。所以它不应该有 100% 的完整页面,而只显示在第二个元素下方。

<div class="wrapper">
    <div class="first">first left</div>
    <div class="second">first right</div>
    <div class="third">second</div>
</div>

【问题讨论】:

    标签: css flexbox


    【解决方案1】:

    您可以这样做,使用calc 功能。

    .wrapper {
      display: flex;
      display: -webkit-flex;
      width: 100%;
      flex-flow: row wrap;
      -webkit-flex-flow: row wrap;
      justify-content: flex-end;
    }
    
    .first {
      width: 200px;
      background: red;
    }
    
    .second {
      width: calc(100% - 200px);
      background: blue;
    }
    
    .third {
      background: yellow;
      width: calc(100% - 200px);
    }
    <div class="wrapper">
      <div class="first">first left</div>
      <div class="second">first right</div>
      <div class="third">second right</div>
    </div>

    【讨论】:

    • 这不正确,因为红色和蓝色元素没有显示在一行中(Safari),如果我想保持一切动态怎么办?红色 flex 1 和蓝色 flex 3...
    • 这是正确的,只是缺少 -webkit- 前缀以在某些 Safari 版本上工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多