受屈儿童的孩子占据所有空间

时间:2018-08-16 15:13:24

标签: css flexbox

我有2个div,它们是flexed元素的子元素。在其中一个内部,我想有一个div,仅占用其内容所需的空间。相反,它占用了所有空间。

我在做什么错了?

这是我的演示:

.wrapper {
  width: 500px;
  height: 200px;
  display: flex;
  flex: 1;
}

.c-1 {
  height: 100%;
  flex: 3;
  
  background-color: green;
}

.c-2 {
  height: 100%;
  flex: 1;
  
  background-color: red;
}

.c-1-inner {
  height: 100%;
  background-color: blue;
}

.box {
  height: 15px;
  width: 15px;
  background-color: yellow;
}
<div class="wrapper">
  <div class="c-1">
    <div class="c-1-inner">
      <div class="box"></div>
    </div>
  </div>
  <div class="c-2"></div>
</div>

2 个答案:

答案 0 :(得分:1)

这是您的行为吗? width: fit-content;

.wrapper {
  width: 500px;
  height: 200px;
  display: flex;
  flex: 1;
}

.c-1 {
  height: 100%;
  flex: 3;
  
  background-color: green;
}

.c-2 {
  height: 100%;
  flex: 1;
  
  background-color: red;
}

.c-1-inner {
  height: 100%;
  width: fit-content;
  background-color: blue;
}

.box {
  height: 15px;
  width: 15px;
  background-color: yellow;
}
<div class="wrapper">
  <div class="c-1">
    <div class="c-1-inner">
      <div class="box"></div>
    </div>
  </div>
  <div class="c-2"></div>
</div>

答案 1 :(得分:0)

删除.c-1-inner上的100%高度...不是必需的。

.wrapper {
  width: 500px;
  height: 200px;
  display: flex;
  flex: 1;
}

.c-1 {
  height: 100%;
  flex: 3;
  background-color: green;
}

.c-2 {
  height: 100%;
  flex: 1;
  background-color: red;
}

.c-1-inner {
  /* height: 100%; */
  background-color: blue;
}

.box {
  height: 15px;
  width: 15px;
  background-color: yellow;
}
<div class="wrapper">
  <div class="c-1">
    <div class="c-1-inner">
      <div class="box"></div>
    </div>
  </div>
  <div class="c-2"></div>
</div>