有没有办法让CSS的透明度样式只应用于标签的innerText,而不应用于任何子元素?
P粉794851975
P粉794851975 2023-08-17 20:51:57
[CSS3讨论组]
<p>在下面的示例中,我有很多标签,其中标签包含输入。</p> <p><strong>有没有办法使CSS的不透明度样式应用于标签的innerText而不是任何子元素,而不需要显式指定颜色?在下面的示例中,不透明度已经应用于下拉菜单,而不仅仅是标签本身。</strong></p> <p>我知道我可以将标签元素更改为仅包围文本,然后添加一个<code>for=</code>,但我更喜欢将标签包裹在被标记的元素周围。</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;"> &lt;div class="colored"&gt; &lt;label&gt; My white label &lt;select&gt;&lt;option&gt;example that should be black-on-white&lt;/option&gt;&lt;/select&gt; &lt;/label&gt; &lt;/div&gt; &lt;div&gt; &lt;label&gt; My black label &lt;select&gt;&lt;option&gt;example that should be black-on-white&lt;/option&gt;&lt;/select&gt; &lt;/label&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="select3"&gt;Good example&lt;/label&gt; &lt;select id="select3"&gt;&lt;option&gt;example that is indeed black-on-white&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;</pre> <p><br /></p>
P粉794851975
P粉794851975

全部回复(1)
P粉066224086

很不幸,opacity适用于容器和其中的所有内容。但在您的情况下,您可以使用rgba color代替:

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>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号