Veselina Kolova
Veselina Kolova

Reputation: 345

Child of flexed child takes all the space

I have 2 divs that are children of a flexed element. Inside one of them, I want to have another div, that takes only the space it needs for its content. Instead it takes all the space.

What am I doing wrong?

Here's my demo:

.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>

Upvotes: 0

Views: 48

Answers (2)

SimonDunne
SimonDunne

Reputation: 32

Is this the behaviour you're after? 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>

Upvotes: 1

Paulie_D
Paulie_D

Reputation: 115045

Remove the 100% height on .c-1-inner...it is not required.

.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>

Upvotes: 0

Related Questions