[英]Override opaque text in a transparent div with CSS
我試圖讓透明div內的文字沒有不透明度,也就是說完全是黑色的:
<div style="opacity:0.6;background:#3cc;">
<p style="background:#000;opacity:1">This text should be all black</p>
</div>
這可能只與CSS有關嗎?
提前致謝
最簡單的方法是使用opacity / alpha為父div的背景設置樣式:
div {
background: #fff; /* for browsers that don't understand the following rgba rule */
background-color: rgba(255,255,255,0.5); /* rgb, white, with alpha at 0.5 */
}
但是,這與IE不兼容。
對於IE> = 7兼容性,您可以使用:
div {
background-image: url('path/to/partially_transparent.png');
background-position: 0 0;
background-repeat: repeat;
}
我記得IE <7有一個專有的過濾器選項,但我擔心我不記得它是如何工作的。 所以我省略了任何描述/展示它的嘗試。 如果我能找到一個有用的參考,雖然我會在以后添加它。
正如所指出的那樣,不透明度是由包含的元素繼承的,這就是為什么你不能覆蓋它,這就是為什么我更喜歡使用background-color
/ background-image
方法。
子元素繼承不透明度。 你可以做的是將<p>
放在不透明的div之外,並設置一個負邊距來移動它。
我經常遇到這個問題,通常會像這樣解決它。 問題是只有當你有動態內容並且div必須擴展時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.