Issue
I’m trying out HTML and CSS and am relatively new to the entire concept. I’m currently working on styling a custom checkbox using an image I made from Photoshop. I am not able to figure out why my image is not appearing when I set it this way.
HTML
<ul id="myUL" class="ulChecklist">
<form action="/action_page.php">
<li><input type="checkbox" name="instruction">
<label for="Step1">Step 1</label>
</li>
<li><input type="checkbox" name="instruction">
<label for="Step2">Step 2</label>
</li>
<li><input type="checkbox" name="instruction">
<label for="Step3">Step 3</label>
</li>
</form>
</ul>
CSS
input[type="checkbox"] {
opacity: 0;
}
input[type="checkbox"] + label {
background: url(check.png) left center no-repeat;
}
This is the pre-checked image I want to add.
This is the post-checked image I want to add.
As you can see, it isn’t appearing.
Is something wrong with the way I write these codes? I’ve checked the following Lynda course link: https://www.lynda.com/HTML-tutorials/Styling-radio-buttons-check-boxes-images/612196/646907-4.html
But it isn’t working out for me. I would greatly appreciate help from people! Thank you for taking your time to answer a noob’s question!
Solution
Try this.
ul{
list-style:none;
}
input[type="checkbox"] {
opacity: 0;
position: absolute;
width: 100%;
height: 100%;
z-index: -1;
}
input[type="checkbox"] + label {
background: url(https://i.stack.imgur.com/eiFBl.png) no-repeat 0 center;
padding-left:60px;
line-height:50px;
display: inline-block;
cursor:pointer;
}
input[type="checkbox"]:checked + label {
background: url(https://i.stack.imgur.com/mCst2.png) no-repeat 0 center;
}
.check-wrap{
position:relative;
display: inline-block;
}
<ul id="myUL" class="ulChecklist">
<form action="/action_page.php">
<li>
<div class="check-wrap">
<input type="checkbox" name="instruction" id="Step1">
<label for="Step1">Step 1</label>
</div>
</li>
<li>
<div class="check-wrap">
<input type="checkbox" name="instruction" id="Step2">
<label for="Step2">Step 2</label>
</div>
</li>
<li>
<div class="check-wrap">
<input type="checkbox" name="instruction" id="Step3">
<label for="Step3">Step 3</label>
</div>
</li>
</form>
</ul>
Answered By – ankita patel
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0