Raiyu
Raiyu

Reputation: 101

CSS Checkbox example

I want to make this example to work. I need it on my project but i don't understand CSS and can't seem to figure out why this isn't working. Any help would be appreciated.

Why is this example working on codepen : codepen link

But is not working in jsfiddle or my website : Jsfiddle link

Code(CSS):

.inputGroup {
background-color: #fff;
display: block;
margin: 10px 0;
position: relative;

label {
  padding: 12px 30px;
  width: 100%;
  display: block;
  text-align: left;
  color: #3C454C;
  cursor: pointer;
  position: relative;
  z-index: 2;
  transition: color 200ms ease-in;
  overflow: hidden;

  &:before {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    content: '';
    background-color: #5562eb;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) scale3d(1, 1, 1);
    transition: all 300ms cubic-bezier(0.4, 0.0, 0.2, 1);
    opacity: 0;
    z-index: -1;
  }

  &:after {
    width: 32px;
    height: 32px;
    content: '';
    border: 2px solid #D1D7DC;
    background-color: #fff;
    background-image: url("data:image/svg+xml,%3Csvg width='32' height='32' viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5.414 11L4 12.414l5.414 5.414L20.828 6.414 19.414 5l-10 10z' fill='%23fff' fill-rule='nonzero'/%3E%3C/svg%3E ");
    background-repeat: no-repeat;
    background-position: 2px 3px;
    border-radius: 50%;
    z-index: 2;
    position: absolute;
    right: 30px;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    transition: all 200ms ease-in;
  }
}

input:checked ~ label {
  color: #fff;

  &:before {
    transform: translate(-50%, -50%) scale3d(56, 56, 1);
    opacity: 1;
  }

  &:after {
    background-color: #54E0C7;
    border-color: #54E0C7;
  }
}

  input {
  width: 32px;
  height: 32px;
  order: 1;
  z-index: 2;
  position: absolute;
  right: 30px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  visibility: hidden;
   }
   }


// codepen formatting
 .form {
  padding: 0 16px;
   max-width: 550px;
   margin: 50px auto;
   font-size: 18px;
  font-weight: 600;
   line-height: 36px;
 }

 body {
 background-color: #D1D7DC;
 font-family: 'Fira Sans', sans-serif;
 }

 *,
 *::before,
 *::after {
   box-sizing: inherit;
  }

  html {
   box-sizing: border-box;
  }

 code {
 background-color: #9AA3AC;
 padding: 0 8px;
 }

Here is the Html code :

  <form class="form">
  <h2>Checkboxes</h2>
  <div class="inputGroup">
  <input id="option1" name="option1" type="checkbox"/>
  <label for="option1">Option One</label>
  </div>
  <div class="inputGroup">
  <input id="option2" name="option2" type="checkbox"/>
  <label for="option2">Option Two</label>
  </div>
  <h2>Radio Buttons</h2>
  <div class="inputGroup">
  <input id="radio1" name="radio" type="radio"/>
  <label for="radio1">Yes</label>
  </div>
  <div class="inputGroup">
  <input id="radio2" name="radio" type="radio"/>
  <label for="radio2">No</label>
   </div>
   </form>

Upvotes: 0

Views: 231

Answers (1)

Anuresh VP
Anuresh VP

Reputation: 607

Because you are not using compiled css. See below snippet

.inputGroup {
  background-color: #fff;
  display: block;
  margin: 10px 0;
  position: relative;
}
.inputGroup label {
  padding: 12px 30px;
  width: 100%;
  display: block;
  text-align: left;
  color: #3C454C;
  cursor: pointer;
  position: relative;
  z-index: 2;
  transition: color 200ms ease-in;
  overflow: hidden;
}
.inputGroup label:before {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  content: '';
  background-color: #5562eb;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%) scale3d(1, 1, 1);
          transform: translate(-50%, -50%) scale3d(1, 1, 1);
  transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  z-index: -1;
}
.inputGroup label:after {
  width: 32px;
  height: 32px;
  content: '';
  border: 2px solid #D1D7DC;
  background-color: #fff;
  background-image: url("data:image/svg+xml,%3Csvg width='32' height='32' viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5.414 11L4 12.414l5.414 5.414L20.828 6.414 19.414 5l-10 10z' fill='%23fff' fill-rule='nonzero'/%3E%3C/svg%3E ");
  background-repeat: no-repeat;
  background-position: 2px 3px;
  border-radius: 50%;
  z-index: 2;
  position: absolute;
  right: 30px;
  top: 50%;
  -webkit-transform: translateY(-50%);
          transform: translateY(-50%);
  cursor: pointer;
  transition: all 200ms ease-in;
}
.inputGroup input:checked ~ label {
  color: #fff;
}
.inputGroup input:checked ~ label:before {
  -webkit-transform: translate(-50%, -50%) scale3d(56, 56, 1);
          transform: translate(-50%, -50%) scale3d(56, 56, 1);
  opacity: 1;
}
.inputGroup input:checked ~ label:after {
  background-color: #54E0C7;
  border-color: #54E0C7;
}
.inputGroup input {
  width: 32px;
  height: 32px;
  order: 1;
  z-index: 2;
  position: absolute;
  right: 30px;
  top: 50%;
  -webkit-transform: translateY(-50%);
          transform: translateY(-50%);
  cursor: pointer;
  visibility: hidden;
}

.form {
  padding: 0 16px;
  max-width: 550px;
  margin: 50px auto;
  font-size: 18px;
  font-weight: 600;
  line-height: 36px;
}

body {
  background-color: #D1D7DC;
  font-family: 'Fira Sans', sans-serif;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
}

code {
  background-color: #9AA3AC;
  padding: 0 8px;
}
<form class="form">
  
  <h2>Checkboxes</h2>
  <div class="inputGroup">
    <input id="option1" name="option1" type="checkbox"/>
    <label for="option1">Option One</label>
  </div>
  
  <div class="inputGroup">
    <input id="option2" name="option2" type="checkbox"/>
    <label for="option2">Option Two</label>
  </div>
  
  <h2>Radio Buttons</h2>
  <div class="inputGroup">
    <input id="radio1" name="radio" type="radio"/>
    <label for="radio1">Yes</label>
  </div>
  <div class="inputGroup">
    <input id="radio2" name="radio" type="radio"/>
    <label for="radio2">No</label>
  </div>
</form>





<link href="https://fonts.googleapis.com/css?family=Fira+Sans" rel="stylesheet">

Upvotes: 1

Related Questions