仅使用CSS,是否可以使元素的背景半透明,但元素的内容(文本和图像)不透明?

我想在不将文本和背景作为两个独立元素的情况下完成这一点。

尝试时:

p型{位置:绝对;背景色:绿色;过滤器:alpha(不透明度=60);不透明度:0.6;}跨度{颜色:白色;过滤器:alpha(不透明度=100);不透明度:1;}<p><span>你好,世界</span></p>

看起来子元素受到其父元素的不透明度的影响,因此不透明度:1相对于父元素的不可见性:0.6。


当前回答

您可以通过(ab)使用渐变语法为Internet Explorer 8解决此问题。颜色格式为ARGB。如果使用Sass预处理器,可以使用内置函数“ie-hex-str()”转换颜色。

background: rgba(0,0,0, 0.5);
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#80000000', endColorstr='#80000000')";

其他回答

您可以通过(ab)使用渐变语法为Internet Explorer 8解决此问题。颜色格式为ARGB。如果使用Sass预处理器,可以使用内置函数“ie-hex-str()”转换颜色。

background: rgba(0,0,0, 0.5);
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#80000000', endColorstr='#80000000')";

在Firefox 3和Safari 3中,您可以像Georg Schölly提到的那样使用RGBA。

一个鲜为人知的技巧是,您可以在Internet Explorer中使用它,也可以使用渐变过滤器。

background-color: rgba(0, 255, 0, 0.5);
filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#7F00FF00', EndColorStr='#7F00FF00');

第一个十六进制数定义颜色的alpha值。

所有浏览器的完整解决方案:

.alpha60 {
    /* Fallback for web browsers that doesn't support RGBa */
    background: rgb(0, 0, 0) transparent;
    /* RGBa with 0.6 opacity */
    background: rgba(0, 0, 0, 0.6);
    /* For IE 5.5 - 7*/
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);
    /* For IE 8*/
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";
}

这是通过RGBa和过滤器在不影响子元素的情况下实现CSS背景透明度。

结果截图证明:

这是在使用以下代码时:

 <head>
     <meta http-equiv="X-UA-Compatible" content="IE=edge" >
    <title>An XHTML 1.0 Strict standard template</title>
     <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <style type="text/css" media="all">
         .transparent-background-with-text-and-images-on-top {
             background: rgb(0, 0, 0) transparent;   /* Fallback for web browsers that doesn't support RGBa */
            background: rgba(0, 0, 0, 0.6);   /* RGBa with 0.6 opacity */
             filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);  /* For IE 5.5 - 7*/
            -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";  /* For IE 8*/
         }
     </style>
 </head>

 <body>
     <div class="transparent-background-with-text-and-images-on-top">
         <p>Here some content (text AND images) "on top of the transparent background"</p>
        <img src="http://i.imgur.com/LnnghmF.gif">
     </div>
 </body>
 </html>

为了使元素的背景半透明,但使元素的内容(文本和图像)不透明,您需要为该图像编写CSS代码,并且必须添加一个名为opacity的最小值属性。

例如

.image {
   position: relative;
   background-color: cyan;
   opacity: 0.7;
}

//值越小,透明度越高,或者值越低,透明度越低。

这是我能想到的最好的解决方案,不使用CSS 3。据我所知,它在Firefox、Chrome和Internet Explorer上运行得很好。

将一个容器div和两个子div放在同一级别,一个用于内容,另一个用于背景。使用CSS,自动调整背景大小以适应内容,并使用z索引将背景放在后面。

.容器{位置:相对;}.内容{位置:相对;颜色:白色;z指数:5;}.背景{位置:绝对;顶部:0px;左:0px;宽度:100%;高度:100%;背景色:黑色;z指数:1;/*这三行用于所有浏览器中的透明度*/-ms filter:“progid:DXImageTransform.Microsoft.Alpha(不透明度=50)”;过滤器:alpha(不透明度=50);不透明度:.5;}<div class=“container”><div class=“content”>内容如下。<br/>背景应该适合。</div><div class=“background”></div></div>

有一个技巧可以最小化标记:使用伪元素作为背景,可以在不影响主元素及其子元素的情况下设置其不透明度:

DEMO

输出:

相关代码:

p型{位置:相对;}p: 之后{内容:“”;位置:绝对;顶部:0;左:0;宽度:100%;高度:100%;背景:#fff;-ms filter:“progid:DXImageTransform.Microsoft.Alpha(不透明度=50)”;不透明度:.6;z指数:-1;}/***以下仅适用于演示样式***/正文{背景:url('http://i.imgur.com/k8BtMvj.jpg')不重复;背景尺寸:封面;}p型{宽度:50%;填充:1em;保证金:10%自动;字体系列:arial,serif;颜色:#000;}国际货币基金组织{显示:块;最大宽度:90%;边距:.6em自动;}<p>Lorem ipsum dolor坐amet,consectetur adipiscing elit。不要用舌头骚扰。<img src=“http://i.imgur.com/hPLqUtN.jpg“alt=”“/></p>

浏览器支持Internet Explorer 8和更高版本。

伪元素不透明度