【问题标题】:Is flexbox grow affected by flexbox-basis?flexbox 的增长是否受 flexbox-basis 影响?
【发布时间】:2018-07-01 00:35:40
【问题描述】:

我有以下 HTML

<div class="container">
  <div class="box box1">one ????</div>
  <div class="box box2">two ????</div>
  <div class="box box3">three ????</div>
  <div class="box box4">four ????</div>
  <div class="box box5">five ????</div>
  <div class="box box6">six ????</div>
</div>

并使用这个 CSS 使用 flexbox 对其进行样式化

.container {
  display:flex;
  flex-wrap:wrap;
}

.box {
  flex-basis:500px;
  flex-grow:1;
}

.box3 {
  flex-grow:10;
}

我假设flex-grow:10; 会使box 3 比出现在我屏幕上的box 410 倍,但我看到了:

为什么这不是大 10 倍?

这里是相对的CodePen

【问题讨论】:

  • 这里很好地解释了正在发生的事情:css-tricks.com/flex-grow-is-weird
  • 因为 flex-basisinitial 宽度 before 其他 flex 属性受到影响。目前尚不清楚您要做什么。你为什么要使用flex-basis
  • flex-grow 从 0 到 1,它只定义了弹性项目增长的能力。检查这个:css-tricks.com/snippets/css/a-guide-to-flexbox
  • @FerranBuireu 这是不正确的。 flex-grow 定义每个项目应该从左边的空间得到多少部分,在这种情况下,根据屏幕截图,box3 所在行的 10/11 和 1/11。

标签: html css flexbox


【解决方案1】:

flex-grow 是否受 flex-basis 影响?

在某种程度上,是的,就像flex-grow 所做的那样,在项目的大小减小后分配剩余空间,其中项目可以通过内容或显式设置来调整大小.

例如,如果父级是 1000px 宽,有 2 个子级,每个子级是 300px 宽,剩余空间将为 1000px-300px-300px = 400px。

如果一个孩子的 flex grow 值为 1,另一个孩子为 10,则前者为 300px+(1/11*400px),后者为 300px+(10/11*400px)

在这里阅读更多关于flex-grow的信息


因此,在您的情况下,如果每个项目设置为 500 像素,每行 2 个,当将有 剩余空间时,该空间将在所有行上平均共享,但具有 @987654325 的行除外@。在该行上,空间将与 box3 的 10/11 和其他人的 1/11 共享。

如果每行 3 个项目,box3 的共享将是 10/12,其他两个每个 1/12。


为什么这不是大 10 倍?

在这种情况下,当在项目上设置宽度时,flex-grow 在项目之间分配剩余空间

如果您想使用flex-grow 来调整项目大小,请给它们一个flex-basis0。这将使 flex-grow 的计算相信这些项目是空的,这样它就会大 10 倍。

/* We start writing out flexbox here. The above is just page setup */
.container {
  display:flex;
  flex-wrap:wrap;
}

.box {
  flex-basis:0;
  flex-grow:1;
  overflow: hidden;        /*  to allow items to be smaller
                               than its content  */
}

.box3 {
  flex-grow:10;
}


/* CSS Normalize */
article,aside,details,figcaption,figure,footer,header,hgroup,nav,section,summary{display:block;}audio,canvas,video{display:inline-block;}audio:not([controls]){display:none;height:0;}[hidden]{display:none;}html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;}a:focus{outline:thin dotted;}a:active,a:hover{outline:0;}h1{font-size:2em;}abbr[title]{border-bottom:1px dotted;}b,strong{font-weight:700;}dfn{font-style:italic;}mark{background:#ff0;color:#000;}code,kbd,pre,samp{font-family:monospace, serif;font-size:1em;}pre{white-space:pre-wrap;word-wrap:break-word;}q{quotes:\201C \201D \2018 \2019;}small{font-size:80%;}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline;}sup{top:-.5em;}sub{bottom:-.25em;}img{border:0;}svg:not(:root){overflow:hidden;}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em;}button,input,select,textarea{font-family:inherit;font-size:100%;margin:0;}button,input{line-height:normal;}button,html input[type=button],/* 1 */
input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer;}button[disabled],input[disabled]{cursor:default;}input[type=checkbox],input[type=radio]{box-sizing:border-box;padding:0;}input[type=search]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box;}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none;}textarea{overflow:auto;vertical-align:top;}table{border-collapse:collapse;border-spacing:0;}body,figure{margin:0;}legend,button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;}

/* Box-sizing border-box */
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }


/* Some default styles to make each box visible */
.box {
  color:white;
  font-size: 50px;
  text-align: center;
  text-shadow:4px 4px 0 rgba(0,0,0,0.1);
  padding:10px;
}

/* Colours for each box */
.box1 { background:#1abc9c;}
.box2 { background:#3498db;}
.box3 { background:#9b59b6;}
.box4 { background:#34495e;}
.box5 { background:#f1c40f;}
.box6 { background:#e67e22;}
.box7 { background:#e74c3c;}
.box8 { background:#bdc3c7;}
.box9 { background:#2ecc71;}
.box10 { background:#16a085;}
<div class="container">
  <div class="box box1">one ?</div>
  <div class="box box2">two ?</div>
  <div class="box box3">three ?</div>
  <div class="box box4">four ?</div>
  <div class="box box5">five ?</div>
  <div class="box box6">six ?</div>
</div>

【讨论】:

    猜你喜欢
    • 2019-02-27
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2013-04-14
    • 2017-08-04
    • 2016-03-13
    相关资源
    最近更新 更多