< BACK TO POST
LinkedIn Facebook Twitter

Blog

css3 Radio Switcher

December 25th, 2012 | Cool Stuff , Css3 , Tutorials

I needed a css 3 radio switcher for a project I am working on, coded it, and sharing with you.

Demo

on off
on off
.switcher-on {
  background: #6c8c37 ;
  background: -moz-linear-gradient(#6c8c37, #7fa341) ;
  background: -webkit-linear-gradient(#6c8c37, #7fa341) ;
  background: -o-linear-gradient(#6c8c37, #7fa341) ;
  background: -ms-linear-gradient(#6c8c37, #7fa341) ;
  background: linear-gradient(#6c8c37, #7fa341) ;
  border-color: #404e29;
}
.switcher-on div {
  left: 38px;
}
.switcher-on:hover div {
  left: 35px;
}
.switcher-wrapper input {
  display: none;
}
.switcher-wrapper input:checked ~ .switcher-no-js {
  background: #6c8c37 ;
  background: -moz-linear-gradient(#6c8c37, #7fa341) ;
  background: -webkit-linear-gradient(#6c8c37, #7fa341) ;
  background: -o-linear-gradient(#6c8c37, #7fa341) ;
  background: -ms-linear-gradient(#6c8c37, #7fa341) ;
  background: linear-gradient(#6c8c37, #7fa341) ;
  border-color: #404e29;
}
.switcher-wrapper input:checked ~ .switcher-no-js div {
  left: 38px;
}
.switcher-wrapper input:checked ~ .switcher-no-js:hover div {
  left: 35px;
}
.switcher-no-js {
  width: 80px;
  height: 23px;
  border-radius: 3px;
  background: #952b37 ;
  background: -moz-linear-gradient(#952b37, #952b37) ;
  background: -webkit-linear-gradient(#952b37, #952b37) ;
  background: -o-linear-gradient(#952b37, #952b37) ;
  background: -ms-linear-gradient(#952b37, #952b37) ;
  background: linear-gradient(#952b37, #952b37) ;
  border: 1px solid #4e2933;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
  position: relative;
  cursor: pointer;
  overflow: hidden;
}
.switcher-no-js span {
  width: 50%;
  text-align: center;
  color: #fff !important;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.3) !important;
  font-size: 11px;
  text-transform: uppercase;
  line-height: 21px;
  float: left;
  margin: 0 !important;
}
.switcher-no-js label {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 1;
  left: 0;
  top: 0;
}
.switcher-no-js div {
  position: absolute;
  width: 50%;
  background: #ffffff ;
  background: -moz-linear-gradient(#ffffff, #dadada) ;
  background: -webkit-linear-gradient(#ffffff, #dadada) ;
  background: -o-linear-gradient(#ffffff, #dadada) ;
  background: -ms-linear-gradient(#ffffff, #dadada) ;
  background: linear-gradient(#ffffff, #dadada) ;
  top: 1px;
  left: 1px;
  border-radius: 3px;
  bottom: 1px;
  text-align: center;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);
  border: 1px solid #4e2933;
  line-height: 16px;
  -webkit-transition: left 0.1s ease;
  -moz-transition: left 0.1s ease;
  -o-transition: left 0.1s ease;
  transition: left 0.1s ease;
  cursor: pointer;
}
.switcher-no-js div:before,
.switcher-no-js div:after {
  content: '';
  width: 1px;
  height: 8px;
  display: inline-block;
  box-shadow: -1px 0 #bebebe, -2px 0 #ffffff, 1px 0 #ffffff;
  margin: 0 1px;
}
.switcher-no-js div:after {
  box-shadow: -1px 0 #bebebe, 1px 0 #ffffff, 2px 0 #bebebe;
}
.switcher-no-js:hover div {
  left: 4px;
}
.switcher-no-js.on {
  background: #6c8c37 ;
  background: -moz-linear-gradient(#6c8c37, #7fa341) ;
  background: -webkit-linear-gradient(#6c8c37, #7fa341) ;
  background: -o-linear-gradient(#6c8c37, #7fa341) ;
  background: -ms-linear-gradient(#6c8c37, #7fa341) ;
  background: linear-gradient(#6c8c37, #7fa341) ;
  border-color: #404e29;
}
.switcher-no-js.on div {
  left: 38px;
}
.switcher-no-js.on:hover div {
  left: 35px;
}
.switcher-no-js.disabled {
  cursor: default;
}
.switcher-no-js.disabled div {
  background: #d8d8d8 ;
  background: -moz-linear-gradient(#d8d8d8, #dbdbdb) ;
  background: -webkit-linear-gradient(#d8d8d8, #dbdbdb) ;
  background: -o-linear-gradient(#d8d8d8, #dbdbdb) ;
  background: -ms-linear-gradient(#d8d8d8, #dbdbdb) ;
  background: linear-gradient(#d8d8d8, #dbdbdb) ;
}
.switcher-no-js.disabled.off {
  background: #957F82;
}
.switcher-no-js.disabled.off:hover div {
  left: 1px;
}
.switcher-no-js.disabled.on {
  background: #848C77;
}
.switcher-no-js.disabled.on:hover div {
  left: 38px;
}
.switcher-on {
    .grad(#6c8c37,#7fa341);
    border-color: #404e29;
    div {
        left: 38px;
    }
    &:hover div {
        left: 35px;
    }
}

.switcher-wrapper {
    input {
        display: none;
        &:checked ~ .switcher-no-js {
            .switcher-on;
        }
    }
}
.switcher-no-js {
    width: 80px;
    height: 23px;
    border-radius: 3px;
    .grad(#952b37, #952b37);
    border: 1px solid #4e2933;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, .3);
    position: relative;
    cursor: pointer;
    overflow: hidden;
    span {
        width: 50%;
        text-align: center;
        color: #fff !important;
        text-shadow: 0 1px 1px rgba(0, 0, 0, .3) !important;
        font-size: 11px;
        text-transform: uppercase;
        line-height: 21px;
        float: left;
        margin: 0 !important;
    }
    label {
        position: absolute;
        width: 100%;
        height: 100%;
        z-index: 1;
        left: 0;
        top: 0;
    }
    div {
        position: absolute;
        width: 50%;
        .grad(#fff,#dadada);
        top: 1px;
        left: 1px;
        border-radius: 3px;
        bottom: 1px;
        text-align: center;
        box-shadow: 0 1px 1px rgba(0, 0, 0, .5);
        border: 1px solid #4e2933;
        line-height: 16px;
        .transition(left .1s ease);
        cursor: pointer;
        &:before, &:after {
            content: '';
            width: 1px;
            height: 8px;
            display: inline-block;
            box-shadow: -1px 0 #bebebe, -2px 0 #fff, 1px 0 #fff;
            margin: 0 1px;
        }
        &:after {
            box-shadow: -1px 0 #bebebe, 1px 0 #fff, 2px 0 #bebebe;
        }
    }

    &:hover div {
        left: 4px;
    }
    &.on {
        .switcher-on;
    }
    &.disabled {
        cursor: default;
        div {
            .grad(#d8d8d8,#dbdbdb);
        }
        &.off {
            background: #957F82;
            &:hover div {
                left: 1px;
            }
        }
        &.on {
            background: #848C77;
            &:hover div {
                left: 38px;
            }
        }
    }
}

Leave a Reply