IE 8 fixed width select issue

I have to specify a width to the select in my html.

It works fine in all browsers except in IE8, which cuts the text that is more than the width

CSS

select {
        border: 1px solid #65A6BA;
        font-size: 11px;
        margin-left: 22px;
        width: 166px;
    }

HTML

<select>
        <option value="1">Some text</option>
        <option value="2">Text Larger Than the Width of select</option>
        <option value="3">Some text</option>
        <option value="4">Some text</option>
        <option value="5">Some text</option>
    </select>

DEMO

I am open to js, jquery solutions, but would prefer a css only solution

Answers


Fixed it!

Here is the revised jsFiddle: http://jsfiddle.net/PLqzQ/

The trick is adding the focus selector to your CSS so that it looks like this:

select {
        border: 1px solid #65A6BA;
        font-size: 11px;
        margin-left: 22px;
        width: 166px;
        overflow:visible ;
    }

select:focus { width:auto ;
position:relative ;
}‚Äč

I tested it in IE8 and it works like a charm.

VERY IMPORTANT: You have to add a Doctype Definition to your Code otherwise the fix won't work for IE8.

Example <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">


I found all the other answers to be really flaky and over-complicated. This can be easily accomplished with the following CSS (at least in my case):

html.IE8 .SelectContainer {
    position: relative;
}

html.IE8 .SelectContainer select {
    position: absolute;
    width: auto; /* Or fixed if you want. */
    max-width: 100%;
}

html.IE8 .SelectContainer select:focus {
    max-width: none;
}

I set a class on the html element if the browser is IE8 (or less) (Google how to do this).

Put your select inside a .SelectContainer and when you click it, it will expand as needed.

No need for jQuery, conditional comments, etc. Much simpler and works much smoother, for my needs, at least.

I hope none of you ever have to employee this.

It's time for the US gov't to upgrade to a modern browser!


I had the same problem and also tried mouseenter and mouseleave but I thought it was very clumsy-looking. I checked the event list for select elements (from MSDN) and tried using beforeactivate and deactivate intead. It worked much more smoothly in my opinion. Tested on a page running in IE8 directly (not compatibility mode):

$('select').beforeactivate(function () {
    var isIE8 = $.browser.version.substring(0, 2) === "8.";
    if (!isIE8)
        return;
    $(this).css('width', 'auto');

});

$('select').deactivate(function () {
    var isIE8 = $.browser.version.substring(0, 2) === "8.";
    if (!isIE8)
        return;
    $(this).css('width', '166px');
});

The page in question was a very old page and it forces IE 8 to go into compatibility mode, so the css solution did not work.

I ended fixing it using the following piece of jquery

Assume the select has a class of fixedWidth

$el = $("select.fixedWidth");
$el.data("origWidth", $el.outerWidth()) 

$el.mouseenter(function(){
    $(this).css("width", "auto");
  })
  .bind("blur change", function(){
    el = $(this);
    el.css("width", el.data("origWidth"));
  });

This was a handy plugin i stumbled upon while sorting IE8 issues. Do have a look, IE8 EXPAND SELECT WIDTH. Works amazing. Easy plug-n-play.


follow the link it may help.

<script>
  $(document).ready(function() {
    $('select').change(function(){
      $("#width_tmp").html($('select option:selected').text());
      $(this).width($("#width_tmp").width()+30); // 30 : the size of the down arrow of the select box 
    });
  });
</script>

I think you should change the width attribute setting: instead of fixed 168px use width:auto; Should work perfectly.


Need Your Help

Displaying divs inside container in dynamic number of rows

jquery html css

I want to display divs in rows. User chooses number of divs and then I want to display them in rows which number depends on number of divs. For example if user picked 6 I want to have 2 rows with 3...