简体   繁体   中英

How can I resize the height of the <select> element in CSS or Javascript?

I have a dropdown menu that is inside a box. I want the dropdown menu to fill up the entire box. The width fills up the entire box, but I cannot seem to find a way to change the height of the dropdown menu.

I have tried using height and messing with the padding but I can't find an answer anywhere.

 <div class="custom_select" id="main_dropdown_menu"> <select class="dropdown_list" name="list of majors" id="main_dropdown"> <option value="default" selected="selected" id="submenu">---</option> <option value="arts">Arts</option> <option value="business">Business</option> <option value="engineering">Engineering</option> <option value="health">Health</option> <option value="humanities">Humanities</option> <option value="natural sciences">Natural Sciences</option> <option value="social sciences">Social Sciences</option> </select> </div>

You can just set the height of select box in CSS:

 select.dropdown_list { height: 30px; }
 <div class="custom_select" id="main_dropdown_menu"> <select class="dropdown_list" name="list of majors" id="main_dropdown"> <option value="default" selected="selected" id="submenu">---</option> <option value="arts">Arts</option> <option value="business">Business</option> <option value="engineering">Engineering</option> <option value="health">Health</option> <option value="humanities">Humanities</option> <option value="natural sciences">Natural Sciences</option> <option value="social sciences">Social Sciences</option> </select> </div>

Maybe it is what you need:

 #main_dropdown_menu{ height: 200px; background: grey; } #main_dropdown{ height: 100%; }
 <div class="custom_select" id="main_dropdown_menu"> <select class="dropdown_list" name="list of majors" id="main_dropdown"> <option value="default" selected="selected" id="submenu">---</option> <option value="arts">Arts</option> <option value="business">Business</option> <option value="engineering">Engineering</option> <option value="health">Health</option> <option value="humanities">Humanities</option> <option value="natural sciences">Natural Sciences</option> <option value="social sciences">Social Sciences</option> </select> </div>

This CSS will do the trick, adjust to your requirements.

select {
   height: calc(1.5em + .75rem + 2px);
   padding: .375rem 1.75rem .375rem .75rem;
   font-size: 1rem;
   font-weight: 400;
   line-height: 1.5;
   vertical-align: middle;
   -webkit-appearance: none;
   -moz-appearance: none;
   appearance: none;
}
 <select style="position: absolute;" onmousedown="if(this.options.length>8){this.size=8;}"  onchange='this.size=0;' onblur="this.size=0;" class="dropdown_list" name="list of majors" id="main_dropdown">

This works just fine for me you can adjust the position according to where you want it to appear. By stating position to be absolute it shows the list down, if you do not state it then it will show on top moving all your other codes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM