How to apply CSS styles in continuation classes
P粉447785031
P粉447785031 2024-04-02 22:44:36
0
1
463

When the .checked class exists, I need to insert the text-decoration: line-through; style into the .todo-name

(I never understood if it could be done with css, but in case I could also use js as a last chance)

<label class="checkbox-container" for="0"><span class="todo-name">Todo</span>
    <input onclick="updateStatus(this)" type="checkbox" id="0" checked="">
    <span class="checkmark checked"></span>
</label>

<!--This content does not have .checked and should not change-->
<label class="checkbox-container" for="0"><span class="todo-name">Todo</span>
    <input onclick="updateStatus(this)" type="checkbox" id="0">
    <span class="checkmark"></span>
</label>

<style>
/*.todo-name{text-decoration: line-through;}*/


/*Not working*/
/*
.checkbox-container .checked ~ .todo-name {
  text-decoration: line-through;
}
.checkbox-container:not(.checked) .todo-name {text-decoration: line-through;}
*/
</style>

Codepen Project

The final result should be like this

P粉447785031
P粉447785031

reply all(1)
P粉545682500

You have encountered several problems. As mentioned in the comments, if you don't want to use JavaScript, you should use pseudo-classes. : is checked in this example.

Next, you'll use the CSS selector ~, which selects sibling elements after this element rather than before it. So trying to select .todo-name using the selector #0:checked ~ .todo-name will not work because the name appears before the checkbox.

Below is an example of a working version.

input:checked ~ .todo-name {
    text-decoration: line-through;  
}


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template