1
0
mirror of synced 2024-11-23 13:36:01 +03:00
select2/pages/03.appearance/docs.md
2017-09-03 22:23:13 -04:00

6.7 KiB

title taxonomy process never_cache_twig
Appearance
category
docs
twig
true
true

The appearance of your Select2 controls can be customized via the standard HTML attributes for <select> elements, as well as various configuration options.

Disabling a Select2 control

Select2 will respond to the disabled attribute on <select> elements. You can also initialize Select2 with disabled: true to get the same effect.

Enable Disable


Labels

You can, and should, use a <label> with Select2, just like any other <select> element.

<label for="id_label_single">
  Click this to highlight the single select element

  <select class="js-example-basic-single js-states form-control" id="id_label_single"></select>
</label>

<label for="id_label_multiple">
  Click this to highlight the multiple select element

  <select class="js-example-basic-multiple js-states form-control" id="id_label_multiple" multiple="multiple"></select>
</label>

Container width

Select2 will try to match the width of the original element as closely as possible. Sometimes this isn't perfect, in which case you may manually set the width configuration option:

element
Uses the computed element width from any applicable CSS rules.
resolve
Uses the `style` attribute value if available, falling back to the computed element width as necessary.
style
Width is determined from the `select` element's `style` attribute. If no `style` attribute is found, null is returned as the width.
_{width_value}_
Valid CSS values can be passed as a string (i.e. `80%`).
Value Description
"element" Uses javascript to calculate the width of the source element.
"style" Copies the value of the width style attribute set on the source element.
"resolve" Tries to use style to determine the width, falling back to element.
Anything else The value of the width option is directly set as the width of the container.

Responsive design - Percent width

Select2's width can be set to a percentage of its parent to support responsive design. The two Select2 boxes below are styled to 50% and 75% width respectively.

<select class="js-example-responsive" style="width: 50%"></select>
<select class="js-example-responsive" multiple="multiple" style="width: 75%"></select>


Select2 will do its best to resolve the percent width specified via a CSS class, but it is not always possible. The best way to ensure that Select2 is using a percent based width is to inline the style declaration into the tag.

Themes

Select2 supports custom themes using the theme option so you can style Select2 to match the rest of your application.

These examples use the classic theme, which matches the old look of Select2.



Various display options of the Select2 component can be changed. You can access the <option> element (or <optgroup>) and any attributes on those elements using .element.