Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix width of drop down menu in select option

When I open my dropdown list, the width of list is greater than drop down box. List width should same as drop down box.

enter image description here

like image 531
Web Hosting Provider Avatar asked Jan 08 '23 14:01

Web Hosting Provider


2 Answers

You can use the following:

select, option { width: __; }

Keep in mind this may not work on Google Chrome. If you want a cross-browser solution you will probably have to use PHP to clip the String with SubStr. You can probably also use jQuery to set the length of the option text.

jQuery('#dropdown option').each(function() {
var optionText = this.text;
console.log(optionText);
var newOption = optionText.substring(0,19);
console.log(newOption);
jQuery(this).text(newOption + '..');
});
select, option {
    width:150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<table>
    <tr>
        <td>
            <select id="dropdown" style="width:150px;">
                <option value="test">123123123123123123123123123123</option>
                <option value="test2">123123123123123123123123123123</option>
                <option value="test3">123123123123123123123123123123</option>
            </select>
        </td>
        <td>
            <input type="text">
        </td>
    </tr>
</table>

JsFiddle

You will have to set the substring values of

var newOption = optionText.substring(0,19);

By yourself though.

like image 145
Rizky Fakkel Avatar answered Apr 20 '23 11:04

Rizky Fakkel


Try to set width of options same as select box

    #SelectBoxid {
  width:150px; 
}
 #SelectBoxid option{
   width:150px; 
}
like image 41
shweta chinchore Avatar answered Apr 20 '23 12:04

shweta chinchore