有没有办法让CSS的透明度样式只应用于标签的innerText,而不应用于任何子元素?
P粉794851975
P粉794851975 2023-08-17 20:51:57
[CSS3讨论组]

在下面的示例中,我有很多标签,其中标签包含输入。

有没有办法使CSS的不透明度样式应用于标签的innerText而不是任何子元素,而不需要显式指定颜色?在下面的示例中,不透明度已经应用于下拉菜单,而不仅仅是标签本身。

我知道我可以将标签元素更改为仅包围文本,然后添加一个for=,但我更喜欢将标签包裹在被标记的元素周围。

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>


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中文网订阅号
每天精选资源文章推送

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