need help for splitted sidebar with 3 different list-heights

Viewed 45

I'm creating a sidebar which contains a searchfield and 3 lists of items.

  • The first list should take 50% height if needed
  • The second and third list should split the height to 25% / 25%, but when the second or third list is empty, the other one should take as much space as available (so max is 50% - headline of the empty list)
  • if a list is bigger then the available height, it should add a scrollbar.

Over the three lists is a small searchfield which size is 7vh.

right now im trying it in the following way:

<div style="height: 100vh">
    <input (...) style="height: 7vh" />
    
    <div style="height: 93vh">
        <div class="list_1_container" style="max-height: 46.5vh">
            <LIST />
        </div>
        <div class="list_2_container" style="max-height: 46.5vh">
            <LIST />
            <LIST />
        </div>
    </div>
</div>

This doesn't work so far. And I think using magic numbers (100vh, 7vh...) is bad for responsibility.

Does anyone know a good solution for this?

1 Answers

I strongly suggest using flex for stuff like this. Its very flexible and at this point state of the art. Here you can get a few great examples on how to use it.

Focus on the flex, flex-direction and display: flex styles. The flex property pretty much lets you define the size and weather a element should be able to shrink or grow. For the 2 lists at the bottom you can use flex: 1 1 auto to allow scaling depending on the required space by the element.

I recommend you only render the bottom list into the dom if length > 0 so that the other list can scale to its max height.

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

<head>
  <meta charset="utf-8">
  <title></title>
</head>

<body style="margin: 0; padding: 0">
  <div style="height: 100vh; max-height: 100vh; display: flex; flex-direction: column; background-color: rgba(0,0,255, 0.2); width: 20%; ">
    <input style="flex: 7%" />

    <div style="flex: 0 0 93%; display: flex; flex-direction: column;">
      <div class="list_1_container" style="flex: 0 0 50%; background-color: red;">
        list
      </div>
      <div class="list_2_container" style="flex: 0 0 50%;  display: flex; flex-direction: column; overflow-y: hidden;">
        <div style="flex: 1 1 auto; background-color: green; overflow-y: scroll;">
          <ul style="">
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
            <li>li1</li>
          </ul>
        </div>
        <div style="flex: 1 1 auto; background-color: blue; overflow-y: scroll;">
          <ul style="">
            <li>li2</li>
            <li>li2</li>
            <li>li2</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

Related