1

I have a form and an ul inside a flexbox.

I have set the height of ul manually. *not what I want tho

How can I set the height of ul to the computed height of form?

Edit computed-height

  • Do not use ref
  • Check styles.css
Hamidreza
  • 1,360
  • 11
  • 18
  • 1
    Please add a [repro] for debugging details. Also what have you tried so far? `height: 100%;`? JS: `innerHeight`/`outerHeight`? – tacoshy Aug 24 '21 at 09:49
  • I've removed items from the list and the list box is the same height as the form box. I can't understand what the problem is. – Ken Aug 24 '21 at 09:50
  • The default values of the flexible container mean that your children's height will automatically stretch to the height of the tallest child. In the case of the example you have linked above, it's the list which is actually causing the form to increase in height due to the larger amount of content – Jacob Riches Aug 24 '21 at 09:58
  • @Ken The height of the list and the form exceeds if you add list items back. – Hamidreza Aug 24 '21 at 09:58
  • @JacobRiches Exactly. I want to set the height of the list so items can scroll. – Hamidreza Aug 24 '21 at 10:00
  • 2
    Gotcha, set the height of the section to any height you wish, 100px for this example, and then set height of the ul to height: 100%;. That will do the trick – Jacob Riches Aug 24 '21 at 10:01
  • @JacobRiches But I don't want to set the height manually. I want the automatic computed height of the `form`. – Hamidreza Aug 24 '21 at 10:02
  • @Hamidreza So, you might want to set the height of form to the height of ul. Since they are sibling elements I hope this [question](https://stackoverflow.com/questions/2715360/html-css-set-div-to-height-of-sibling) might be useful – Rifky Niyas Aug 24 '21 at 10:05
  • Sandbox edited. – Hamidreza Aug 24 '21 at 10:10
  • This is how I would do it with vanilla JS but I'm not 100% sure how you would do the same in React as document.query selector doesn't play too nice with the virtual DOM I'm told. https://codepen.io/jriches/pen/YzQzOoG Could be a good starting place though – Jacob Riches Aug 24 '21 at 10:27

2 Answers2

2

I think this does more or less what you want.

I've changed the html, by including an extra div. I've commented the changes to the css.

The basic principle is this: make the height of the section depend only on the height of the form, by removing the ul from the flow, using position:absolute. Then set max-height for the ul to 100%, which will be 100% of the section... which in turn is derived from the height of the contained form.

section {
  display: flex;
  gap: 16px;
  position:relative; /* position relative means contained items are relative to this */
}

form {
  background-color: cadetblue;
  
}

ul {
  position:absolute; /* position absolute means the size of this block doesn't affect the height of the container */

  max-height:100%; /* 100% of the containing section, which has position:relative */
  margin: 0px;
  background-color: chocolate;
  overflow: auto;
}
<section>
      <form>
        <h1>Form</h1>
        <button>Submit</button>
      </form>
      <div>
      <ul>
        <li>Adobe</li>
        <li>Amazon</li>
        <li>Apple</li>
        <li>Facebook</li>
        <li>Google</li>
        <li>Microsoft</li>
        <li>Netflix</li>
      </ul>
      </div>
    </section>
Chris Lear
  • 6,592
  • 1
  • 18
  • 26
1

First, set the section container position to relative. Then, to make the form box adjust to the content use height: fit-content;. Finally, for the ul box use position: absolute;, max-height: 100%; and overflow: scroll;.

See example below.

section {
  position: relative;
  display: flex;
  gap: 16px;
}

form {
  height: fit-content;
  background-color: cadetblue;
}

ul {
  position: absolute;
  max-height: 100%;
  margin: 0px;
  background-color: chocolate;
  overflow: scroll;
}
<section>
  <form>
    <h1>Form</h1>
    <button>Submit</button>
  </form>
  <div>
    <ul>
      <li>Adobe</li>
      <li>Amazon</li>
      <li>Apple</li>
      <li>Facebook</li>
      <li>Google</li>
      <li>Microsoft</li>
      <li>Netflix</li>
      <li>Adobe</li>
      <li>Amazon</li>
      <li>Apple</li>
      <li>Facebook</li>
      <li>Google</li>
      <li>Microsoft</li>
      <li>Netflix</li>
    </ul>
  </div>
</section>
Ken
  • 574
  • 4
  • 15
  • This is nice, but it introduces a manual setting for the width of the section (200px), which might not be wanted. – Chris Lear Aug 24 '21 at 10:35
  • Ignore the comment above. The recent edit introduces a `div` containing the `ul`, and removes the setting of the width, so my comment is out of date. – Chris Lear Aug 24 '21 at 10:42
  • Thank you @ChrisLear, I thought the same. – Ken Aug 24 '21 at 10:43