4

I am kinda new to web designing. Trying to create a toggle iOS like button but the issue is with its appearance on different browsers. It looks different in different browsers. As I tested it it works properly on Chrome.

Can somebody help me to make it look same on all browsers? Sorry for the broken english?

jsfiddle Link : http://jsfiddle.net/racy2408/Let1y8yr/

Given below is CSS I tried:

body {
  margin: 30px;
  text-align: center;
}
input[type=checkbox],
input[type=radio] {
  display: inline-block;
  width: 50px;
  height: 30px;
  position: relative;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  -webkit-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  -o-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  border-radius: 15px;
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  -o-border-radius: 15px;
  background-color: #fff;
  padding: 1px;
  margin: 0px;
  transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transform: scale(1);
  -webkit-transform: scale(1);
  /*Adjust size here*/
  -moz-transform: scale(1);
  -o-transform: scale(1);
}
input[type=checkbox]:checked,
input[type=radio]:checked {
  box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
  -webkit-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
  -moz-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
  -o-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
}
input[type=checkbox]:after,
input[type=radio]:after {
  content: " ";
  position: absolute;
  width: 28px;
  height: 28px;
  cursor: pointer;
  border-radius: 15px;
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  -o-border-radius: 15px;
  box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  -o-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  background-color: #fff;
  /* background-image: url(../images/toggle.png);
 background-repeat: no-repeat;
 background-size: contain; */
  transition: all 0.2s ease-in 0.2s;
  -webkit-transition: all 0.2s ease-in 0.2s;
  -moz-transition: all 0.2s ease-in 0.2s;
  -o-transition: all 0.2s ease-in 0.2s;
}
input[type="checkbox"]:checked:after,
input[type="radio"]:checked:after {
  left: 23px;
}
input[type=checkbox]:focus,
input[type=checkbox]:active,
input[type=radio]:focus,
input[type=radio]:active {
  outline: 0;
}
<h1>Checkbox :</h1> 
<input type="checkbox" />
<input type="checkbox" checked />

<h1>Radio :</h1>

<input type="radio" name="group" />
<input type="radio" name="group" checked />
<input type="radio" name="group" />
racy
  • 69
  • 1
  • 7
  • Finally done with the help of @sev !! New fiddle : http://jsfiddle.net/racy2408/Let1y8yr/ – racy Feb 24 '15 at 09:39

1 Answers1

3

inputs aren't supposed to be able to add before and after psuedo-elements because they aren't containers (e.g. an <input> can't have child elements).

There is some more information in the comment here: https://stackoverflow.com/a/4660434/4338477

You could add labels to get the same effect though (this works in Firefox and IE9+):

http://jsfiddle.net/8okjwLxx/2/

<h1>Checkbox :</h1> 

<input id="checkbox1" type="checkbox" />
<label for="checkbox1"></label>
<input id="checkbox2" type="checkbox" checked />
<label for="checkbox2"></label>

<h1>Radio :</h1>

<input id="radio1" type="radio" name="group" />
<label for="radio1"></label>

<input id="radio2" type="radio" name="group" checked />
<label for="radio2"></label>

<input id="radio3" type="radio" name="group" />
<label for="radio3"></label>
body {
    margin: 30px;
    text-align: center;
}

input[type=checkbox],
input[type=radio]{
    display: none;
}

input[type=checkbox] + label,
input[type=radio] + label {
    display: inline-block;
    width: 50px;
    height: 30px;
    position: relative;
    cursor: pointer;
    appearance:         none;
    -webkit-appearance: none;
    -moz-appearance:    none;
    box-shadow:         inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -webkit-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -moz-box-shadow:    inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -o-box-shadow:      inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    border-radius:         15px;
    -webkit-border-radius: 15px;
    -moz-border-radius:    15px;
    -o-border-radius:      15px;
    background-color: #fff;
    padding: 1px;
    margin: 0px;
    transition:         all 0.3s ease-out;
    -webkit-transition: all 0.3s ease-out;
    -moz-transition:    all 0.3s ease-out;
    -o-transition:      all 0.3s ease-out;  
    transform:         scale(1);
    -webkit-transform: scale(1); /*Adjust size here*/
    -moz-transform:    scale(1);
    -o-transform:      scale(1);
}

input[type=checkbox]:checked + label,
input[type=radio]:checked + label {
    box-shadow:         inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -webkit-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -moz-box-shadow:    inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -o-box-shadow:      inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
}

input[type=checkbox] + label:after,
input[type=radio] + label:after {
    left: 0;
    content: " ";
    position: absolute;
    width: 28px;
    height: 28px;
    cursor: pointer;
    border-radius:         15px;
    -webkit-border-radius: 15px;
    -moz-border-radius:    15px;
    -o-border-radius:      15px;
    box-shadow:         0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -webkit-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -moz-box-shadow:    0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -o-box-shadow:      0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    background-color: #fff;
    /* background-image: url(../images/toggle.png);
    background-repeat: no-repeat;
    background-size: contain; */
    transition:         all 0.2s ease-in 0.2s;
    -webkit-transition: all 0.2s ease-in 0.2s;
    -moz-transition:    all 0.2s ease-in 0.2s;
    -o-transition:      all 0.2s ease-in 0.2s;
}

input[type=checkbox]:checked + label:after,
input[type=radio]:checked + label:after {
    left: 23px;
}

input[type=checkbox]:focus,
input[type=checkbox]:active,
input[type=radio]:focus,
input[type=radio]:active {
    outline: 0;
}
Community
  • 1
  • 1
ratherblue
  • 2,108
  • 11
  • 14