Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 如何让flex grow表现得不贪婪?_Css_Flexbox - Fatal编程技术网

Css 如何让flex grow表现得不贪婪?

Css 如何让flex grow表现得不贪婪?,css,flexbox,Css,Flexbox,标记: <div class="playground"> <div class="red"> <div class="child">I don't need all this space, but my parents are greddy. : (</div> </div> <div class="blue">I want to grow big!</div> </div>

标记:

<div class="playground">
  <div class="red">
    <div class="child">I don't need all this space, but my parents are greddy. : (</div>
  </div>
  <div class="blue">I want to grow big!</div>
</div>

在上面的例子中

  • Red容器设置为使用所有可用空间(
    flex-grow
  • 红色容器与蓝色共享空间
  • 蓝色具有
    最大高度:300px
  • 红色内容没有占据所有高度。如果red不是贪婪的,那么它会向blue额外赠送200px

如何使红色不贪婪?

尝试添加
flex:1至.blue

顺便说一下,您在
.red、
中拼错了
width
,如果我理解正确,您可以在
.red
.blue
上使用
flex grow
,并给blue一个很大的值。使用
flex-grow
而不是
flex
速记将默认
flex-basis
auto
。因此,
flex basis
将以内容为基础,只有在考虑了初始内容之后,剩余空间才会被分配

.blue
上的非常大的值将导致它消耗几乎所有剩余的空间,直到达到其最大宽度

.red {
    flex-grow: 1;
    background: rgba(255, 0, 0, .5);

    .child {
        background: rgba(255, 255, 255, .5);
        padding: 10px 10px;
        margin: 10px;
    }
}

.blue {
    flex-grow: 999;
    background: rgba(0, 0, 255, .5);
    min-height: 100px;
    max-height: 300px;
}

这就是你要找的吗?我不记得我在哪里学的这项技术,但当我再次看到这个网站时,我会发布信用卡。我创建了两个FlexBox,一个是蓝色的,一个是红色的,当你将鼠标悬停在它们上面时,每个FlexBox都会扩展。希望这有帮助

    <!DOCTYPE html>
    <html lang="en">
      <head>

       <style>
       .flx
       {
          width: 400px;
          height: 200px;
          font: 12px auto;

          display: -webkit-flex;
          -webkit-flex-direction: column;
          display: flex;
          flex-direction: column;
       }

       .flx > div
       {
          -webkit-flex: 1 1 auto;
          flex: 1 1 auto;
          width: 50px;

          -webkit-transition: width 0.2s ease-out; /*adjust speed of size change*/
  transition: width 0.2s ease-out;

       }
       .flx > div:nth-child(1){ background : red; }
       .flx > div:nth-child(2){ background : blue; }
       .flx > div:hover    /*adjust size when you hover*/
       {

            width: 200px;
       }
       </style>  

     </head>
     <body>
      <div class="flx">
        <div>red box</div>
        <div>blue box</div>
      </div>
     </body>
    </html>

.flx
{
宽度:400px;
高度:200px;
字体:12px自动;
显示:-webkit flex;
-webkit柔性方向:列;
显示器:flex;
弯曲方向:立柱;
}
.flx>div
{
-webkit flex:1自动;
flex:1自动;
宽度:50px;
-webkit过渡:宽度0.2s放松;/*调整尺寸变化速度*/
过渡段:宽度0.2s放松;
}
.flx>div:n子级(1){背景:红色;}
.flx>div:n子级(2){背景:蓝色;}
.flx>div:hover/*悬停时调整大小*/
{
宽度:200px;
}
红盒子
蓝盒子

我想你可以设置
flex:1
而不是
flex-grow
.red
.blue
。这在Facebook的瑜珈版式中起作用。

当蓝色变得贪婪时,就会忽略
min height
。如果你用
height
替换
min height
max height
,然后用JavaScript修改
.blue
的大小,会怎么样?类似于:
var bluebox=document.querySelector(“.blue”);bluebox.onclick=function(){resize.style.height=“100px”;}哦,我把我所有的文本放在一个单独的div类中,我通常在一个单独的css上引用它,但是为了演示,它被嵌入到主html中。
    <!DOCTYPE html>
    <html lang="en">
      <head>

       <style>
       .flx
       {
          width: 400px;
          height: 200px;
          font: 12px auto;

          display: -webkit-flex;
          -webkit-flex-direction: column;
          display: flex;
          flex-direction: column;
       }

       .flx > div
       {
          -webkit-flex: 1 1 auto;
          flex: 1 1 auto;
          width: 50px;

          -webkit-transition: width 0.2s ease-out; /*adjust speed of size change*/
  transition: width 0.2s ease-out;

       }
       .flx > div:nth-child(1){ background : red; }
       .flx > div:nth-child(2){ background : blue; }
       .flx > div:hover    /*adjust size when you hover*/
       {

            width: 200px;
       }
       </style>  

     </head>
     <body>
      <div class="flx">
        <div>red box</div>
        <div>blue box</div>
      </div>
     </body>
    </html>