有沒有辦法讓CSS的透明度樣式只套用於標籤的innerText,而不應用於任何子元素?
P粉794851975
P粉794851975 2023-08-17 20:51:57
0
1
470
<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;"> <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粉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>
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!