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!
4
Answers
Try this.
Sorry I am not able to view the Lynda course as I am not a member, but will do my best to answer this.
If I were setting up I would make the following changes to your code:
Then in the CSS I would callout the checkmark-section class and add in the click effect image under the pseudo class of focus < This is the css word for clicked.
For example:
.checkmark-section:focus {
background: url(./checkmark-active)
}
This will mean that once checkmark-active is clicked, it will swap over to show the depressed check mark image instead. I have not tried this out, but that is how I would expect it to work.
All the best,
Dan
You are on the right path. Just that you need to resize the background image and
float
it to the left. And one of the most important part is to associate the label with the input checkbox withfor
andid
:The code you have provided makes no provision for the background
label
image when the adjacent siblingpseudo-state
is:checked
.You’ll need to account for both states, e.g:
input[type="checkbox"]
&input[type="checkbox"]:checked
Edit
You may also need to declare background size properties.