Updating Select2 Styling to Match Bootstrap Fields
Select2 is an awesome jQuery plugin to enhance the functionality of combo boxes. But if you implement it in an application with a Bootstrap UI, you’ll notice that the styling is not consistent. In this post, I’ll show how to make the styling of Select2 fields consistent with other fields on the form.
Bootstrap Default Styling
Here’s an example of a simple Bootstrap form, including two combo box fields. The styling is very consistent across all of the fields.
Select2 Default Styling
Once I implement Select2 and add the code to initialize combo boxes as Select2 fields, they styling changes. The width and internal padding and line spacing are not consistent with other bootstrap fields. (Click image to enlarge in order to see the difference more clearly.)
Consistent Styling
Fortunately, by inspecting a standard bootstrap field either in the bootstrap CSS or in the browser’s developer tools, I can see the styles in use and replicate them for a more consistent look.
Here is the CSS required to make it happen:
.select2-container { width:100%; } .select2-container a.select2-choice { font-size: 14px; height: 38px; padding: 8px 12px; line-height: 1.42857; } .select2-container .select2-choice .select2-arrow { padding-top:6px; }
The first rule makes the field width use 100% of the size of its container, as bootstrap fields do.
The second set of rules sets the font and field styling to make them consistent.
The third rule vertically centers the drop-down arrow for the larger field.
❤ awesome