3

I'm developing a MVC3 application and need to select the checkboxes label.

In ASP MVC3 you have helper methods which creat a part of the code. So the code for a checkbox looks like this:

<input id="Jumping_successleicht" type="checkbox" value="true" name="Jumping_successleicht">
<input type="hidden" value="false" name="Jumping_successleicht">
<label for="Jumping_successleicht">
<span>leicht (4)</span>
</label>

Now I've thought I can use following code to select the label:

input[type=checkbox] + label {
    background: url("../../Images/Controls/Checkbox.png") no-repeat scroll left center transparent;
    clear: none;
    cursor: pointer;
    margin: 0;
    padding: 5px 0 4px 24px;
}

But it does not work. It looks like label and input have to be next to each other.

Does any ony have a solution how to solve this problem?

2 Answers 2

4

There is no CSS selector that can be used to select the target of a <label for="#"> element universally. The + selector is the "adjacent sibling" selector.

There are a few workarounds:

  • Put the <input> element directly within the <label> element (you won't need the for="" attribute, that way).
  • Seeing as each <input /> needs to have a unique id="" attribute set in order to use <label for="">, just select the checkboxes by their IDs in the stylesheet.
  • Assign classes for each of the appropriate inputs.
  • Create wrappers around each input and its label.
Sign up to request clarification or add additional context in comments.

1 Comment

A reference combinator has been proposed in the next spec that serves this specific purpose, but it looks like there hasn't been a decision whether to include it or not: w3.org/TR/selectors4/#idref-combinators Guess we'll have to wait and see.
3

Maybe you can try this?

        input[type="checkbox"] + label{
            background-color:red;
        }

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.