0

I have this very simple snippet:

.parent {
  display: flex;
  background-color: blue;
}

.child100 {
  width: 6px;
  height: calc(100% - 5px);
  background-color: red;
}
<body>
  <div class="parent">
    <div class="child100"></div>
    <div class="childContent">
      <table>
        <tr>
          <th>
            1
          </th>
          <th>
            2
          </th>
        </tr>
        <tr>
          <th>
            3
          </th>
          <th>
            4
          </th>
        </tr>
      </table>
    </div>
  </div>
</body>

height: calc(100% - 5px); doesn't take any effect, also if I set height: 100% for html body and parent tags.
I don't want to explicitly define the height of parent in pixels, rather I want it to fit its content (taking the height of table).
I don't want the position of child100 to be absolute as the parent div is part of a very long element with overflow and that will stuck child100 ontop when scrolling, rather than being next to table.
Also, omitting height attribute for .child100 and adding margin-bottom: -5px for the other child will affect the height of the whole parent element as seen by the background-color.

user2401856
  • 468
  • 4
  • 8
  • 22

1 Answers1

2

Remove the height definition and add a bottom margin. The stretch alignment of flexbox will do the job for your.

.parent {
  display: flex;
  background-color: lightblue;
}

.child100 {
  width: 6px;
  margin-bottom: 5px;
  background-color: red;
}
<body>
  <div class="parent">
    <div class="child100"></div>
    <div class="childContent">
      <table>
        <tr>
          <th>
            1
          </th>
          <th>
            2
          </th>
        </tr>
        <tr>
          <th>
            3
          </th>
          <th>
            4
          </th>
        </tr>
      </table>
    </div>
  </div>
</body>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415