76

I have a listbox and I want to decrease its width.

Here is my code:

<select name="wgtmsr" id="wgtmsr" style="width: 50px;">
  <option value="kg">Kg</option>
  <option value="gm">Gm</option>
  <option value="pound">Pound</option>
  <option value="MetricTon">Metric ton</option>
  <option value="litre">Litre</option>
  <option value="ounce">Ounce</option>
</select>

This code works on IE 6 but not in Mozilla Firefox (latest version). Can anybody please tell me how I can decrease the width of the dropdown list on Firefox?

Maximillian Laumeister
  • 19,107
  • 7
  • 56
  • 76
user1844626
  • 1,778
  • 7
  • 23
  • 36

7 Answers7

88

Try this code:

<select name="wgtmsr" id="wgtmsr">
<option value="kg">Kg</option>
<option value="gm">Gm</option>
<option value="pound">Pound</option>
<option value="MetricTon">Metric ton</option>
<option value="litre">Litre</option>
<option value="ounce">Ounce</option>
</select>

CSS:

#wgtmsr{
 width:150px;   
}

If you want to change the width of the option you can do this in your css:

#wgtmsr option{
  width:150px;   
}

Maybe you have a conflict in your css rules that override the width of your select

DEMO

Alessandro Minoccheri
  • 34,369
  • 22
  • 118
  • 164
  • 5
    How is that any different than what the OP has? – Chris Dec 18 '12 at 11:13
  • 2
    Inline `style` definitions override every other styling (unless there's `!important`), so I don't think so. The OP's original code works fine even on Firefox 17.0.1. – Chris Dec 18 '12 at 11:15
  • 1
    @Abody97 maybe he has a conflict in his css – Alessandro Minoccheri Dec 18 '12 at 11:17
  • 1
    Thank you so much for your help. yes, the option width wasn't working but now using #wgtmsr option{width:50px;} its working(working by using class too).but before I put width style in every option tag as well as in select tag but that didn't work. yes may be that was overriden. – user1844626 Dec 18 '12 at 11:31
  • 1
    @Chris or unless you override them with `element[style]` in your css. – Pere Dec 31 '14 at 11:50
  • 5
    FYI This does not seem to make any difference on Chrome or IE11, it does however as the OP asked work for Firefox, I was looking for something a little more generic... :( – PJUK Nov 17 '15 at 16:46
  • select {} never worked but using the name worked great! – AShah Mar 30 '21 at 21:15
  • It will not work if the option length is long. – Nandeep Barochiya Dec 03 '21 at 07:05
10

The dropdown width itself cannot be set. It's width depend on the option-values. See also here ( jsfiddle.net/LgS3C/ )

How the select box looks like is also depending on your browser.

You can build your own control or use Select2 https://select2.org

algorhythm
  • 8,172
  • 2
  • 34
  • 46
7

This:

<select style="width: XXXpx;">

XXX = Any Number

Works great in Google Chrome v70.0.3538.110

6

try the !important argument to make sure the CSS is not conflicting with any other styles you have specified. Also using a reset.css is good before you add your own styles.

select#wgmstr {
    max-width: 50px;
    min-width: 50px;
    width: 50px !important;
}

or

<select name="wgtmsr" id="wgtmsr" style="width: 50px !important; min-width: 50px; max-width: 50px;">
Amyth
  • 31,549
  • 25
  • 88
  • 134
  • Did you actually try what you're suggesting? If you did, you might notice that the OP's original code already works. – Chris Dec 18 '12 at 11:17
  • that is why i am suggesting him to use the `!important` argument to make sure the code is not conflicting with other styles. – Amyth Dec 18 '12 at 11:19
  • Adding `!important` is the only thing which worked for me. I always forget about it. – Tass Nov 02 '16 at 14:31
5

Create a css and set the value style="width:50px;" in css code. Call the class of CSS in the drop down list. Then it will work.

mychalvlcek
  • 3,908
  • 1
  • 18
  • 34
Vishal Bharti
  • 165
  • 1
  • 9
2

If you want to control the width of the list that drops down, you can do it as follows.

CSS

#wgtmsr option {
    width: 50px;
}
techfoobar
  • 63,712
  • 13
  • 108
  • 129
-1

This worked for me:

ul.dropdown-menu > li {
    max-width: 144px;
}

in Chromium and Firefox.

WJH
  • 481
  • 3
  • 14