freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/css-flexbox/use-the-flex-shorthand-prop...

2.4 KiB

id title challengeType videoUrl forumTopicId dashedName
587d78ae367417b2b2512afe 使用 flex 短方法属性 0 https://scrimba.com/p/pVaDAv/cbpW2tE 301112 use-the-flex-shorthand-property

--description--

上面几个 flex 属性有一个简写方式。 flex-growflex-shrinkflex-basis 属性可以在 flex 中一并设置。

例如,flex: 1 0 10px; 会把项目属性设为 flex-grow: 1;flex-shrink: 0; 以及 flex-basis: 10px;

属性的默认设置是 flex: 0 1 auto;

--instructions--

请给 #box-1#box-2 添加 flex 属性。 设置 #box-1flex-grow 属性值为 2flex-shrink 属性值为 2flex-basis 属性值为 150px。 设置 #box-2flex-grow 属性值为 1flex-shrink 属性值为 1flex-basis 属性值为 150px

通过上面的设置,在容器大于 300px 时,#box-1 扩大的空间会是 #box-2 扩大空间的两倍;在容器小于 300px 时,前者缩小的空间会是 #box-2 缩小空间的两倍。 300px 是两个盒子的 flex-basis 属性值之和。

--hints--

#box-1 元素应具有 flex 属性,其属性值应为 2 2 150px

assert(
  $('#box-1').css('flex-grow') == '2' &&
    $('#box-1').css('flex-shrink') == '2' &&
    $('#box-1').css('flex-basis') == '150px'
);

#box-2 元素应具有 flex 属性,其属性值应为 1 1 150px

assert(
  $('#box-2').css('flex-grow') == '1' &&
    $('#box-2').css('flex-shrink') == '1' &&
    $('#box-2').css('flex-basis') == '150px'
);

应使用 flex 的简写属性为 #box-1#box-2 添加规则。

assert(code.match(/flex:\s*?\d\s+?\d\s+?150px;/g).length == 2);

--seed--

--seed-contents--

<style>
  #box-container {
    display: flex;
    height: 500px;
  }
  #box-1 {
    background-color: dodgerblue;

    height: 200px;
  }

  #box-2 {
    background-color: orangered;

    height: 200px;
  }
</style>

<div id="box-container">
  <div id="box-1"></div>
  <div id="box-2"></div>
</div>

--solutions--

<style>
  #box-container {
    display: flex;
    height: 500px;
  }
  #box-1 {
    background-color: dodgerblue;
    flex: 2 2 150px;
    height: 200px;
  }

  #box-2 {
    background-color: orangered;
    flex: 1 1 150px;
    height: 200px;
  }
</style>

<div id="box-container">
  <div id="box-1"></div>
  <div id="box-2"></div>
</div>