I have lots of labels in the following example, where the label contains the input.
Is there any way to make the CSS opacity style apply to the innerText of the label and not to any of the child elements, without specifying the color explicitly? In the example below, the opacity has applied to the drop-down, not just the label-proper.
I know I could change the label element to surround only the text and then add a for=
, but I much prefer to wrap my labels around the labeled thing.
div{ padding: 10px;}
select {
background-color: white;
}
.colored{
color: white;
background-color: lightblue;
}
label {
opacity: .7;
}
<div class="colored">
<label>
My white label
<select><option>example that should be black-on-white</option></select>
</label>
</div>
<div>
<label>
My black label
<select><option>example that should be black-on-white</option></select>
</label>
</div>
<div>
<label for="select3">Good example</label>
<select id="select3"><option>example that is indeed black-on-white</option></select></div>
2
Answers
Unfortunately,
opacity
applies to the container and everything inside it. But in your case you might usergba
color
instead:You can nest span tags inside of label elements and wrap the text in that. This will allow you to target just the text with opacity (or any other) styles.
I would recommend doing this in general, in case you also need to apply spacing, custom typography, add a pseudo element, replace it with an image, translate the text, etc.
Also – Keep an out for the color contrast, and make sure the text will be readable for people with low vision.