Giter VIP home page Giter VIP logo

toggle-switchy's Introduction

Toggle Switchy

Maintained Size Stars Issues License

A pure CSS toggle switch for form input checkboxes

Preview

Installation

CSS

<link rel="stylesheet" href="toggle-switchy.css">

HTML

<label for="ID_HERE" class="toggle-switchy">
	<input checked type="checkbox" id="ID_HERE">
	<span class="toggle">
		<span class="switch"></span>
	</span>
</label>

Options

Option Data Attribute
Rounded data-style="rounded"
Square data-style="square"
No Text data-text="false"
Disabled add the disabled attribute to the input tag
Checked add the checked attribute to the input tag
Sizes data-size="xl"
data-size="lg"
medium (default)
data-size="sm"
data-size="xs"
Colors data-color="red"
data-color="orange"
data-color="yellow"
data-color="green"
data-color="blue"
data-color="purple"
data-color="gray"
Labels data-label="left"
label on right (default)

Features

  • CSS only - no JavaScript!
  • No dependencies
  • Fully reponsive
  • Fully customizable
  • Fully self contained
  • 7 color schemes
  • Labels on the left and the right
  • Keyboard Accessible - accessibility (a11y)
  • Focus Styles - accessibility (a11y)
  • Supports all modern browsers
  • Search engine friendly
  • Screen reader friendly
  • Doesn't dump a bunch of global styles that'll screw with your other CSS

TODO

  • Add in error checking to make sure the input element is a checkbox - input[type='checkbox']
  • Better a11y (accessibility) support
  • Add tabindex="0" to the demo for keyboard accessibility instructions

Known Issues

  • IE10 and below doesn't support the smooth slide

See also Toggle Radios - A CSS only toggle switch for form input checkboxes (not Bootstrap dependent)

toggle-switchy's People

Contributors

adamculpepper avatar nacho656 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

toggle-switchy's Issues

Wrong alignment of switch when "box-sizing: border-box;" is not set on switch element

Hi Adam,

thank you for your library!

I had a hard time getting the alignment of the switch button correct with the example code.

Running the example on a clean homepage without any CSS framworks included, the buttons were displayed as in this image:
switch_wrong_alignment

Strangely, the buttons are displayed correctly when just including bootstrap.css as seems to be the case for the examples I found on Google:
switch_correct_alignment

The reason I found just now, these framworks apply the global style "box-sizing: border-box;" which is not a browser default at least on Firefox and current Chrome versions.

I fixed this by adding the global style, but this is also fixed by adding it to the switch element CSS as with the following patch:

diff --git a/toggle-switchy.css b/toggle-switchy.css
index dd6ac4a..e6f7acb 100644
--- a/toggle-switchy.css
+++ b/toggle-switchy.css
@@ -34,7 +34,7 @@ CORE STYLES BELOW - NO TOUCHY
 .toggle-switchy > input[disabled] + .toggle {opacity:0.5;}
 .toggle-switchy > input[disabled] + .toggle:hover {cursor:not-allowed;}
 .toggle-switchy > input + .toggle {width:100%; height:100%; margin:0; cursor:pointer;}
-.toggle-switchy > input + .toggle > .switch {display:block; height:100%; position:absolute; right:0; z-index:3;}
+.toggle-switchy > input + .toggle > .switch {box-sizing:border-box; display:block; height:100%; position:absolute; right:0; z-index:3;}

HTH, best wishes.

Create npm package

Similar to the bootstrap npm package. Allows for installation and version management via the package manager.

Focus styles

I'd love to see some aesthetically consistent custom focus styles on these bad boys. Great work!

Add icon support

Examples
image

image
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentcolor" class="css-1lr7thp e1w77hao0" style="transform: translateX(18px);"><path d="M19.934 7.656a.224.224 0 000-.316L18.66 6.065a.224.224 0 00-.316 0l-7.909 7.907a.224.224 0 01-.316 0L6.656 10.51a.224.224 0 00-.316 0l-1.274 1.274a.224.224 0 000 .316l5.053 5.054a.224.224 0 00.316 0l9.5-9.498z"></path></svg>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.