Flip Toggle Switch

Home Search

Flip toggle switch

A binary "flip" switch is a common UI element on mobile devices that is used for binary on/off or true/false data input. You can either drag the flip handle like a slider or tap one side of the switch.

To create a flip toggle, start with a select with two options. The first option will be styled as the "on" state switch and the second will be styled as the "off" state so write your options accordingly. View the data- attribute reference to see all the possible attributes you can add to flip switches.

Set the for attribute of the label to match the id of the input so they are semantically associated. It's possible to accessibly hide the label if it's not desired in the page layout, but we require that it is present in the markup for semantic and accessibility reasons.

	
<label for="flip-1">Flip switch:</label>
<select name="flip-1" id="flip-1" data-role="slider">
	<option value="off">Off</option>
	<option value="on">On</option>
</select> 

This will produce a basic flip toggle switch input. The default styles set the width of the switch to 100% of the parent container and stack the label on a separate line.

Longer Labels

The control is proportionally scaled, so to use longer labels one can just add a line of CSS setting the switch to the desired width. For example, given the following markup:


<div class="containing-element">
	<label for="flip-min">Flip switch:</label>
	<select name="flip-min" id="flip-min" data-role="slider">
		<option value="off">Switch Off</option>
		<option value="on">Switch On</option>
	</select>
</div>

.containing-element .ui-slider-switch { width: 9em } will produce:

As some default styles hinge on fieldcontains, note that you may have to ensure that custom styles apply to switches within fieldcontains by using .ui-field-contain div.ui-slider-switch { width: […]; }.

Mini version

For a more compact version that is useful in toolbars and tight spaces, add the data-mini="true" attribute to the element to create a mini version.

	
<label for="flip-mini">Flip switch:</label>
<select name="flip-mini" id="flip-mini" data-role="slider" data-mini="true">
	<option value="off">Off</option>
	<option value="on">On</option>
</select>

This will produce a flip switch that is not as tall as the standard version and has a smaller text size.

Field containers

Optionally wrap the switch markup in a container with the data-role="fieldcontain" attribute to help visually group it in a longer form.

	
<div data-role="fieldcontain">
<label for="flip-2">Flip switch:</label>
	<select name="flip-2" id="flip-2" data-role="slider">
		<option value="nope">Nope</option>
		<option value="yep">Yep</option>
	</select> 
</div>

The flip toggle switch is now displayed like this:

Theming the flip switch

Like all form elements, this widget will automatically inherit the theme from its parent container. To choose a specific theme color swatch, specify the data-theme attribute on the select and specify a swatch letter.

	
<div data-role="fieldcontain">
	<label for="flip-3">Flip switch:</label>
	<select name="flip-3" id="flip-3" data-role="slider" data-theme="a">
		<option value="no">No</option>
		<option value="yes">Yes</option>
	</select> 
</div>

This results in a switch with the swatch "a" colors for the handle. Note that the lefthand "on" state gets the active state color.

Here is a swatch "e" variation:

It is also possible to theme the track of the flip switch by adding the data-track-theme attribute and specifying the chosen swatch letter on the select.

Here's an example of a flip switch with the swatch "a" applied to the track and swatch "c" applied to the handle:

	
<div data-role="fieldcontain">
        <label for="flip-5">Flip switch:</label>
        <select name="flip-5" id="flip-5" data-role="slider" data-theme="c" data-track-theme="a">
                <option value="no">No</option>
                <option value="yes">Yes</option>
        </select>
</div>

Calling the switch plugin

This plugin will auto-initialize on any page that contains a select with the data-role="slider" attribute. However, if needed you can directly call the slider plugin on any selector, just like any jQuery plugin:


$('select').slider();