0

I am new to html and css and i am working on a project where i want to use custom check boxes. I am trying to hide a check-box with an image via css. I dont want to re-write the html if i dont have to. Ive got my image to cover the check box using (display:none; )but this has also disabled my check box. Is there a way to make my check-box usable without displaying?

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

    input[type='checkbox'] + label
    {
    background: url('/Pictures/checkbox_unchecked.jpg')no-repeat;
    height: 30px;
    width: 30px;
    background-size: 100%;
    display:inline-block;
    padding: 0 0 0 0px;
    }

    input[type='checkbox']:checked + label
    {
    background: url('Pictures/checkbox_checked.jpg') no-repeat;
    height: 50px;
    width: 200px;
    background-size: 100%;
    display:inline-block;
    padding: 0 0 0 0px;
    }
1
  • 1
    Make it transparent with opacity: 0 Commented Jul 9, 2016 at 20:40

3 Answers 3

3

You can set the opacity of the checkbox to 0. This still makes it clickable.

Example (click inside the black rectangle):

#checkbox {
  opacity: 0;
}
#container {
  border: 2px solid black;
  width: 20px;;
}
<div id="container">
  <input type="checkbox" id="checkbox" onclick="alert('hello')"/>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Changing the opacity and then moving the checkbox worked great. Thank you!
I used this and it works perfectly but I have my div with overflow: scroll and this disables the scroll functionality. Any idea why?
@Naomi I don't know why that would happen. This shouldn't affect the scroll in any way.
2

label input {
  opacity: 0;
  width: 0;
  height: 0;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

label span.y {
  display: none;
}

label span.n {
  display: inline;
}

label input:checked ~span.y {
  display: inline;
}

label input:checked ~span.n {
  display: none;
}
<label>
  <input type="checkbox" value="1">
  <span class="y">Checked!</span>
  <span class="n">Click me!</span>
</label>

Just change span.y and span.n. to your elements (images or something else).

Comments

1

input[type='checkbox'] {
  display: block;
  height: 0;
  width: 0;
  opacity: 0;
  overflow: hidden;
}
input[type='checkbox'] + label {
  background: url('http://lorempixel.com/30/30/') no-repeat;
  height: 30px;
  width: 30px;
  background-size: cover;
  display: inline-block;
}
input[type='checkbox']:checked + label {
  background: url('http://lorempixel.com/50/200/') no-repeat;
  height: 50px;
  width: 200px;
  background-size: cover;
  display: inline-block;
}
<input type="checkbox" id="check" />
<label for="check"></label>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.