Div with height automatico dropdown menu

Asked

Viewed 981 times

2

Hello, my question is this. Let’s assume this code

<div>
<a>teste</a>
<a>teste</a>
<a>teste</a>
<a>teste</a>
</div>

This div is displayed when passing the mouse on a button, so far so good, but I checked on a site a similar case but it happened the following...the div had many options, when clicking on the dropdown menu, the div extends to the end of the screen, if the space is not enough to display all the content it generates a scroll, regardless of the position of the dropdown menu, the div only extends to the end of the screen, like: if the user rolled the page and the dropdown menu button was there at the bottom of the screen, by clicking on the menu...it makes the div go to that end of the screen.. showing few options and generating a scroll in the div, I’m trying to do the same, but if I put to div have height auto, when I click on the menu...it already shows all the content and extending the div out of the screen and does not generate scroll even with overflow:auto, because the div com height auto fits to the content and not the screen, I do not know if I could understand but summarizing, I want a div that extends only to the end of the screen, that it fits the screen and never display content that exceeds the visible part of the screen, then she has to create a scroll. I know you can do a schematic with javascript, but I want to know if there is a simpler alternative that use maybe only css.

  • Can you present your example more fully? Put together the rest of the html relevant to the interaction, the js and the css you’re using for these elements.

  • Leandro is a common dropdown menu, with css and html, but I didn’t want the content of the div to leave the screen, see here...https://www.clashofstats.com/rankings/global/players, wherever it has global, is a dropdown menu, you will notice that every time you expand it, regardless of the position it never exceeds the screen, I wanted something similar to this.

1 answer

3


Well I mounted a test drop here that I think does what you wanted, I in case I put a height max-height fixed, but you can control your elements and your hierarchy to leave it 100% and it gets exactly the screen size as you mentioned, or do this by javascript. But I just wanted to show you the functionality of max-height together with overflow:auto, the overflow makes appear the scrollbar, while selecting auto, it will check if there is content out of its limits and add the scrollbar. And if you don’t have content out of bounds, it won’t add this bar.

Only HTML and CSS (with multiple elements to get bigger q the screen)

* {padding:0; margin:0;}
html, body {width:100%; height:100%;}
ul, li {list-style:none;}
a {cursor:pointer;}
nav {float:left; width:100%; background:#e0e0e0;}
nav > ul > li {display:inline-block; padding:5px;}
nav > ul > li > ul {position:absolute; top:0; background:#666; width:200px; display:none; max-height:100%; overflow-y:auto;}
nav > ul > li:hover > ul {display:block;}
nav > ul > li:hover > ul li a {display:block; width:100%; padding:5px 0;}
<nav>
  <ul>
    <li>
      <a>MenuDropDown</a>
      <ul>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
      </ul>
    </li>
  </ul>
</nav>

Only HTML and CSS (with few elements to make it smaller than the screen)

* {padding:0; margin:0;}
html, body {width:100%; height:100%;}
ul, li {list-style:none;}
a {cursor:pointer;}
nav {float:left; width:100%; background:#e0e0e0;}
nav > ul > li {display:inline-block; padding:5px;}
nav > ul > li > ul {position:absolute; top:0; background:#666; width:200px; display:none; max-height:100%; overflow-y:auto;}
nav > ul > li:hover > ul {display:block;}
nav > ul > li:hover > ul li a {display:block; width:100%; padding:5px 0;}
<nav>
  <ul>
    <li>
      <a>MenuDropDown</a>
      <ul>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
      </ul>
    </li>
  </ul>
</nav>

Using Javascript (jQuery)

$(document).ready(changeMaxHeight);
$(window).resize(changeMaxHeight);
function changeMaxHeight(){
  var $dropdown = $('.dropdown');
  var $maxHeight = $(document).height();
  
  // $dropdown.css('max-height', $maxHeight+'px');
  $dropdown.css('max-height', ($maxHeight - 28) +'px'); // Caso queira retirar a altura do elemento "nav" é só subtrair a altura dele. Caso não queira só descomentar a linha acima e remover esta.
}
* {padding:0; margin:0;}
ul, li {list-style:none;}
a {cursor:pointer;}
nav {float:left; width:100%; background:#e0e0e0;}
nav > ul > li {display:inline-block; padding:5px; position:relative;}
nav > ul > li > ul {position:absolute; top:100%; background:#666; width:200px; display:none; overflow-y:auto;}
nav > ul > li:hover > ul {display:block;}
nav > ul > li:hover > ul li a {display:block; width:100%; padding:5px 0;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <ul>
    <li>
      <a>MenuDropDown</a>
      <ul class="dropdown">
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
        <li><a>Link</a></li>
      </ul>
    </li>
  </ul>
</nav>

I hope I’ve helped.

  • Thanks for the help, however I wanted this on this site clashofstats.com/rankings/global/players, check that there in Global has a dropdown menu, anywhere you expand it, it never goes past the screen.... it resizes and generates the scroll, wanted to know if there is a way to do this without javascript.

  • I edited the post with what you wanted, but as I said earlier, you will have q control your styles and your hierarchies for the max-height:100% work, and also control very well the positioning of your dropdown to not get in the wrong place on the screen. I advise to do by javascript. it is not difficult, I will edit the post with the way to do via javascript jQuery.

  • Aldo, no need, I’m not using jQuery when it comes to the case.. realized in the site’s source code that they use max-height to limit the div to screen size, now missing to know if what they use to set this adaptive value.

  • I edited the code with jQuery javascript, they should use exactly this, I keep using the max-height but now I take this variable value as the screen size, and change it if the screen is resized.

  • If you go to that site, open the drop, and see the max-height, change the height of your browser to a smaller size than was already open, and reopen their drop, you will see that the value is not the same and is inserted only after opening the drop, this indicates use of something other than HTML and CSS

  • Aldo, I got here, putting this in the boot event, it sets the max-height up to the visible area of the screen Document.getElementById('dropper').style.maxheight = window.pageYOffset + 'px', thanks for the help.

  • Exactly, with jquery you could do so also easily, just subtract the top offset of the dropdown element :) I’m glad I helped. If you liked the reply, if you can an up vote. Thank you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.