Home  >  Q&A  >  body text

Is there a way to have the CSS transparency style apply only to the label's innerText and not to any child elements?

<p>In the example below, I have a lot of tags where the tags contain inputs. </p> <p><strong>Is there a way to make the CSS opacity style apply to the label's innerText rather than any child elements without explicitly specifying the color? In the example below, opacity has been applied to the dropdown menu, not just the label itself. </strong></p> <p>I know I could change the label element to just surround the text and then add a <code>for=</code>, but I prefer to wrap the label around the element being tagged. </p> <pre class="brush:css;toolbar:false;">div{ padding: 10px;} select { background-color: white; } .colored{ color: white; background-color: lightblue; } label { opacity: .7; }</pre> <pre class="brush:html;toolbar:false;"> <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></pre> <p><br /></p>
P粉794851975P粉794851975399 days ago473

reply all(1)I'll reply

  • P粉066224086

    P粉0662240862023-08-18 18:33:11

    Unfortunately, opacity applies to the container and everything in it. But in your case you can use rgba color instead:

    select {
      background-color: white;
    }
    .colored{
      color: white; 
      background-color: lightblue;
      padding: 10px;
    }
    
    label {
      color: rgba(255, 255, 255, .7);
    }
    <div class="colored">
       <label>
          我的标签
          <select><option>示例</option></select>
       </label>

    reply
    0
  • Cancelreply