1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-24 21:40:17 +00:00
bootstrap/docs/components/custom-forms.md
2014-12-24 21:48:49 -08:00

3.7 KiB

layout title
page Custom forms

In the interest of customization and cross browser consistency, Bootstrap include a handful of customized form elements. They're solid replacements for default form controls as they're built on top of semantic and accessible markup.

Checkboxes and radios

Each checkbox and radio is wrapped in a <label> for three reasons:

  • It provides a larger hit areas for checking the control.
  • It provides a helpful and semantic wrapper to help us replace the default <input>s.
  • It triggers the state of the <input> automatically, meaning no JavaScript is required.

We hide the default <input> with opacity and use the .c-indicator to build a new custom form control. We can't build a custom one from just the <input> because CSS's content doesn't work on that element.

With the sibling selector (~), we use the :checked state to trigger a makeshift checked state on the custom control.

In the checked states, we use base64 embedded SVG icons from Open Iconic. This provides us the best control for styling and positioning across browsers and devices.

Checkboxes

{% example html %} Check this custom checkbox {% endexample %}

Radios

{% example html %} Toggle this custom radio Or toggle this other custom radio {% endexample %}

Stacked

Custom checkboxes and radios are inline to start. Add a parent with class .c-inputs-stacked to ensure each form control is on separate lines.

{% example html %}

Toggle this custom radio Or toggle this other custom radio
{% endexample %}

Select menu

Similar to the checkboxes and radios, we wrap the <select> in a <label> as a semantic wrapper that we can generate custom styles on with CSS's generated content.

{% example html %} Open this select menu One Two Three {% endexample %}

The <select> has quite a few styles to override and includes a few hacks to get things done. Here's what's happening:

  • The appearance is reset to none for nearly all styles to correctly apply across modern browsers (meaning not IE9).
  • The :-moz-focusring is overridden so that on focus there's no inner border in Firefox.
  • The arrow is hidden in Firefox with a media query hack. (There's a longstanding open bug for a native method of addressing this.)
  • The arrow is hidden in IE10+ with a simple selector.
  • The arrow is hidden in IE9 with a separate media query hack which generates another pseudo-element to literally mask it. Not ideal, but doable.

Heads up! This one comes with some quirks right now:

  • select[multiple] is currently currently not supported.
  • Clickability is limited in IE9.
  • Firefox's dropdown of options looks rather ugly.
  • The custom caret is unable to receive the selected state's color.

Any ideas on improving these are most welcome.