1

How do i make my checkboxes transparant? It does work with text fields and buttons but it doesnt seem to work with checkboxes. This is the form of my code.

#checkbox {
  width: 20px;
  height: 20px;
  cursor: pointer;
  vertical-align: bottom;
  color: black;
  background-color: rgba(0, 0, 0, 0.08);
}
<div class="checkbox check-transparent">
  <label>
      <input type="checkbox" id="checkbox">Onthoud mij
  </label>
</div>
UncaughtTypeError
  • 8,226
  • 4
  • 23
  • 38
Nick Chen
  • 13
  • 1
  • 1
  • 3
  • 1
    Use `opacity: 0.5`, for example. Background doesn't work like that. – jurchiks Nov 22 '17 at 21:56
  • https://stackoverflow.com/questions/4148499/how-to-style-a-checkbox-using-css – Sergio Tx Nov 22 '17 at 21:59
  • In short, you can't. How a checkbox is rendered is dependent on browser and (if the browser allows it - and most don't) OS settings. The only option you have is to hide the `checkbox` and use separate, style-able elements to display "**it**" the way you want. – tao Nov 22 '17 at 22:02
  • Could you rate up or mark the answer you found to be the solution or helpful for your problem. Thanks. –  Nov 27 '17 at 14:15

2 Answers2

5

The only thing that can be done for a regular checkbox is making it transparent by decreasing its opacity but it also lightens the tick mark inside if checked. A workaround is, having the checkbox wrapped in a div and changing the color of the checkbox when checked like shown in the snippet below:

#checkb {
    width: 20px;
    height: 20px;
    cursor: pointer;
    background: rgba(40,40,40,0.2);
    color:black;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    border: none;
    position: relative;
    left: -5px;
    top: -5px;
}

#checkb:checked {
    background: rgba(40,40,40,0.7);
}


.checkbox-container {
    position: absolute;
    display: inline-block;
    margin: 20px;
    width: 100px;
    height: 100px;
    overflow: hidden;
}
<div class="checkbox-container">
<input type="checkbox" id="checkb"/> Input
</div>
3

You can also (classic method that allows to restyle a checkbox crossbrowser following standards)

  • take input ahead and outside the label

  • link the label to its input via attribute for

  • use a pseudo on label to draw a checkbox via css and also apply size and background.

  • hide the checkbox

label {
  display: inline-block;
}

#checkbox {
  display: none;
}

label:before {
  content: '';
  
  cursor: pointer;
  vertical-align: bottom;
  color: black;
  background-color: rgba(0, 0, 0, 0.1);
  
  
  /* average checkbox styling */
  line-height: 1.1em;
  font-weight: bold;
  text-align: center;
  font-size: 20px;
  height: 20px;
  width: 20px;
  display: inline-block;
  box-shadow: inset 0 0 1px 1px rgb(153, 154, 154), inset 0 0 1px 2px white, inset 0 0 1px 2px white, inset 2px 2px 1px 1px rgb(182, 187, 192), inset -1px -1px 2px 1px rgb(182, 187, 192), inset 8px 8px 4px -4px rgb(182, 187, 192);
  box-sizing: border-box;
}

:checked+label:before {
  content: '\2713';
  color: rgb(74, 95, 151);
}

:checked+label {
  color: rgb(74, 95, 151);
}

label:hover {
  color: rgb(28, 50, 125);
}

label:hover:before {
  box-shadow: inset 0 0 1px 1px rgb(95, 133, 156), inset 0 0 1px 2px rgb(198, 237, 252), inset 0 0 1px 2px rgb(198, 237, 252), inset 2px 2px 1px 1px rgb(134, 203, 246), inset -1px -1px 2px 1px rgb(134, 203, 246), inset 8px 8px 4px -4px rgb(134, 203, 246);
}
<div class="checkbox check-transparent">
  <input type="checkbox" id="checkbox">
  <label for="checkbox"> Onthoud mij
  </label>
</div>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129