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-a">Select slider:</label>
<select name="slider" id="flip-a" 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="slider" 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-a">Select slider:</label>
<select name="slider" id="flip-a" 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-b">Flip switch:</label>
	<select name="slider" id="flip-b" data-role="slider">
		<option value="no">No</option>
		<option value="yes">Yes</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-c">Flip switch:</label>
	<select name="slider" id="flip-c" 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 A swatch colors for the handle. Note that the lefthand "on" state gets the active state color.

Here is a E swatch variation:

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();