0

I have a div (absolute-container) that is absolute positioned to a static positioned parent. My goal is to make absolute-container have a height of the set browser with a max-height ~600px. It's current start position is ~middle of page and I'd like it to extend it to the bottom of browser page and have the height dynamically change if browser size is updated. It's also possible that overflow-scroll-container only contains 3-4 options and div height in this case should not extend to bottom of browser.

.parent {
    position: static;
}

.absolute-container {
    display: block;
    position: absolute;
    z-index: 121;
    background-color: #FFF;
    width: 90vw;
}

.overflow-scroll-container {
    overflow-y: scroll;
    padding: 4px 8px;
}

.content-footer {
    margin: 8px 0;
    display: -ms-flexbox;
    display: flex;
    justify-content: flex-end;
    border-top: 1px solid #BECAD6;
}



<div class='parent'>
   <button>Example Dropdown</button>
   <div class='absolute-container'>
      <div class='overflow-scroll-container'>
        <button>Button 1</button>
        <button>Button 2</button>
        <button>Button 3</button>
        <button>Button 4</button>
        <button>Button 5</button>
        <button>Button 6</button>
        <button>Button 7</button>
        <button>Button 8</button>
      </div>
      <div class='content-footer'>
        <button>Reset</button>
        <button>Done</button>
      </div>
   </div>
</div>

enter image description here

Yash Chitroda
  • 645
  • 4
  • 8
  • 27
Michael
  • 403
  • 1
  • 9
  • 28
  • I'm still stuck! Happy to zoom with someone too. It's a little tricky to explain and show here... – Michael Sep 08 '21 at 16:25

3 Answers3

2

There are a lot of obstacles to overcome with your problem. You'll need to use JavaScript to grab the screen.height in a function that sets the container height for you. You'll need to trigger this function every time the browser is resized, and also the first time the page is displayed. You'll also need to know the top Y value of your absolute positioned container. Then you can subtract the total window.height from your absolute positioned Y top value to get the height of that menu container. However you will probably want to set a max-height for that value, and have height 100% (or use flex-shrink appropriately) for your requirement that the menu not have any white space underneath the options. If you break the problem into the individual pieces it may be easier to find help.

Here are some helpful links to help with these pieces of the problem:
What is the difference between window, screen, and document in JavaScript?
Call a function when window is resized
Retrieve the position (X,Y) of an HTML element relative to the browser window
How do I force a DIV block to extend to the bottom of a page even if it has no content?

Rick Tilley
  • 126
  • 1
  • 6
0

Based on your screenshot, if this is what you wanted to achieve, I wouldn't do it this way. Obviously, I don't know all the specs, but I'm sure there would be a better way to do this, even using a library, plugin or something like that.

Now, based on what I understand, I made this: https://codepen.io/yofisim/pen/abwLrLK

I'm setting the height of the absolute-container in JS based on viewport height. (vh) I'm checking how many buttons I have and based on that, I set the height of the absolute-container. Moreover, I've added an EventListener on resize, to be sure when I resize the window, the element will be max 600px or total viewport height if list has more than 4 buttons.

I hope this fix your issue.

yofisim
  • 372
  • 1
  • 6
  • 17
0

Edit: since the moderator doesn't understand

"My goal is to make absolute-container have a height of the set browser with a max-height ~600px. It's current start position is ~middle of page and I'd like it to extend it to the bottom of browser page and have the height dynamically change if browser size is updated."

random doc quote: "The measure vh is equal to 1/100 of the height of the viewport"

Suggestion: height: clamp(50vh, 75vh, 600px); for example... you can make it 25, 50, 600px. You can also try using calc() in CSS, it does some pretty cool stuff. I have no issues using VH/VW units, and they're way more comfortable than using 9999 screen presets for every existing device, and since they work most of the time then it's the most simple solution...

The question doesn't state JS usage, nor rendering HTML really requires JS. With VH/VW you can scale the browser as much you want and the elements resize automatically...

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 15 '21 at 11:01