"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Customize Dropdown List Widths Across Different Browsers?

How to Customize Dropdown List Widths Across Different Browsers?

Published on 2024-11-07
Browse:263

How to Customize Dropdown List Widths Across Different Browsers?

IE Dropdown List Width Modification

In Internet Explorer, the dropdown list mirrors the width of its dropbox, while in Firefox, it adapts to the content. This constraint necessitates expanding the dropbox to accommodate the longest selection, resulting in an aesthetically unappealing web page.

CSS-Based Solution to Varying Widths

To overcome this issue and allow different widths for the dropbox and dropdown list using CSS, consider the following:

The jQuery-based method outlined below handles all keyboard and mouse events, including clicks:

if (!$.support.leadingWhitespace) { // if IE6/7/8
    $('select.wide')
        .bind('focus mouseover', function() { $(this).addClass('expand').removeClass('clicked'); })
        .bind('click', function() { $(this).toggleClass('clicked'); })
        .bind('mouseout', function() { if (!$(this).hasClass('clicked')) { $(this).removeClass('expand'); }})
        .bind('blur', function() { $(this).removeClass('expand clicked'); });
}

Combine this script with the following CSS:

select {
    width: 150px; /* Or whatever width you want. */
}
select.expand {
    width: auto;
}

By adding the "wide" class to the necessary dropdown elements, you can apply these modifications. For example:

Explore a live demonstration in this jsfiddle: [link]

Release Statement This article is reprinted at: 1729407915 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3