Skip to content

Instantly share code, notes, and snippets.

@patw0929
Created April 21, 2015 11:13
Show Gist options
  • Save patw0929/39af8527700c55b13761 to your computer and use it in GitHub Desktop.
Save patw0929/39af8527700c55b13761 to your computer and use it in GitHub Desktop.
[jQuery] Center text in the dropdown list (<select>)
var _optimize_selectbox_padding = function () {
$(".search__county, .search__district").each(function () {
var text_width = (parseInt($(this).css("font-size"), 10) * $(this).val().length);
var padding = (($(this).outerWidth() - text_width) / 2);
$(this).css("padding-left", padding);
});
};
_optimize_selectbox_padding();
$(".search__county, .search__district").on("change", function () {
_optimize_selectbox_padding();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment