1

I have the styling below to achieve a pseudo checkbox. In real life it has a background image to show the marks I want, but I simplified the example for researching purposes. It's working nicely in Chrome and Safari, but not in firefox or IE. Another set of eyes would be much appreciated.

http://www.bootply.com/6xig9trE5C

CODE:

/*CSS*/
ul {
  margin: 0 auto;
  width: 270px;
}
li {
  float: left;
  height: 30px;
  list-style-type: none;
  text-align: left;
  white-space: nowrap;
  width: 50%;
}

input[type=checkbox] {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  left: -2000px;
  margin: -1px;
  padding: 0;
  position: relative;
  width: 1px;
}
input[type=checkbox]:after {
  content: 'X';
  display: inline-block;
  height: 19px;
  left: 1969px;
  position: relative;
  top: -12px;
  width: 19px;
}
input[type=checkbox]:checked:after {
  content: 'V';
}

label {
  cursor: pointer;
  display: inline-block;
  font-size: 15px;
  height: 27px;
  line-height: 22px;
  padding-left: 32px;
  -khtml-user-select: none;
  -ms-user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

<!-- HTML -->
<div class="option">
  <ul>
    <li>
      <label><input name="cb1" type="checkbox" checked="checked" value="1">Checkbox1</label>
    </li>
    <li>
      <label><input name="cb2" type="checkbox" checked="checked" value="2">Checkbox2</label>
    </li>
    <li>
      <label><input name="cb3" type="checkbox" checked="checked" value="3">Checkbox3</label>
    </li>
  </ul>
</div>
Adminiculo
  • 303
  • 1
  • 14
  • 2
    I think some browsers don't allow `input` elements to have pseudo-elements - http://stackoverflow.com/questions/24867008/css-to-specify-pseudo-element-after-following-input – Paulie_D May 15 '15 at 15:51
  • I think this is going to be it – Adminiculo May 15 '15 at 15:58
  • possible duplicate of [CSS :after input does not seem to work](http://stackoverflow.com/questions/9840768/css-after-input-does-not-seem-to-work) or [Can I use the :after pseudo-element on an input field?](http://stackoverflow.com/q/2587669/1529630) – Oriol May 15 '15 at 16:00
  • It's probably a duplicate, but I didn't know that the problem was the input not accepting the pseudo, so it was hard to get to those. – Adminiculo May 15 '15 at 18:56

2 Answers2

1

:before and :after pseudo elements add new content before or after the target element's content. Since Input elements have no content; they just have a value. so technically we can't use pseudo element for input element.

so I guess in this case chrome Browser is wrong. and IE and Firefox Browser are right.

you may have a look what w3c says.

Kheema Pandey
  • 9,977
  • 4
  • 25
  • 26
0

In this case, knowing what the problem was, I fixed it by adding an empty span after the input and using there the styling of the :after, with little modifications. Like this:

<!-- html -->
<label>
   <input type="checkbox" checked="checked" value="1" name="cb1">
   <span></span>
   Checkbox 1
</label> 

/*CSS: (with the background use)*/
input[type=checkbox] + span {
  background: url("/img/checkbox.png") 0 0 no-repeat;
  display: inline-block;
  height: 19px;
  left: -12px;
  position: relative;
  top: 3px;
  width: 19px;
}

input[type=checkbox]:checked + span {
  background-position: 0 -32px;
}
Adminiculo
  • 303
  • 1
  • 14