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

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

尝试时:

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

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


当前回答

不透明度的背景,但不是文本有一些想法。要么使用半透明图像,要么覆盖其他元素。

其他回答

该方法允许您在背景中显示图像,而不仅仅是纯色,还可以用于在其他属性(如边界)上显示透明度。不需要透明PNG图像。

在CSS中使用:before(或:after),并赋予它们不透明度值,使元素保持其原始不透明度。因此,您可以使用:before来创建一个人造元素,并为其提供所需的透明背景(或边框),然后将其移动到您希望使用z索引保持不透明的内容后面。

一个例子(fiddle)(注意,带有类dad的DIV只是为了提供一些上下文和与颜色的对比,实际上不需要这个额外的元素,红色矩形向下移动一点并向右移动,以便在fancyBg元素后面留下可见的背景):

<div class="dad">
    <div class="fancyBg">
        Test text that should have solid text color lets see if we can manage it without extra elements
    </div>
</div>

使用此CSS:

.dad {
    background: lime; border: 1px double black; margin: 1ex 2ex;
    padding: 0.5ex; position: relative; -k-z-index: 5;
}
.fancyBg {
    border: 1px dashed black; position: relative; color: white; font-weight: bold;
    z-index: 0; /*background: black;*/
}
.fancyBg:before {content:'-'; display: block;
    position: absolute; background: red; opacity: .5;
    top: 2ex; right: -2ex; bottom: -2ex; left: 2ex;
    /*top: 0; right: 0; bottom: 0; left: 0;*/
    z-index: -1;
}

在本例中,.fancyBg:before具有您想要的具有透明度的CSS财产(本例中为红色背景,但可以是图像或边框)。它被定位为绝对的,以便将其移到.faceyBg后面(使用零值或更适合您需要的任何值)。

CSS 3为您的问题提供了一个简单的解决方案。使用:

background-color:rgba(0, 255, 0, 0.5);

这里,rgba代表红色、绿色、蓝色和alpha值。由于255而获得绿色值,并且通过0.5α值获得半透明度。

最简单的方法是使用半透明背景PNG图像。

如果需要,可以使用JavaScript使其在InternetExplorer6中工作。

我使用Internet Explorer 6中透明PNG中概述的方法。

除此之外,您可以使用两个并排的兄弟元素来伪装它-使一个半透明,然后将另一个绝对放置在顶部。

我同意以上所有的答案,rgba是正确的选择。在我的例子中,我以编程方式提供了一个十六进制背景,因此我必须基于十六进制代码生成自己的rgba。我创建了唐先生答案的修改版本,将十六进制转换为rgba

function hexToRgba(hex,alpha) {
    // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
    var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
    hex = hex.replace(shorthandRegex, function(m, r, g, b) {
      return r + r + g + g + b + b;
    });

    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    if(result!=null){
      const r = parseInt(result[1], 16);
      const g = parseInt(result[2], 16);
      const b = parseInt(result[3], 16);
      //
      return `rgba(${r},${g},${b},${alpha})`;

    }
    return null;
  }

可以使用附加到十六进制值的不透明度值:

background-color: #11ffeeaa;

在本例中,aa是不透明度。不透明度为00表示透明,ff表示纯色。

不透明度是可选的,因此可以一如既往地使用十六进制值:

background-color: #11ffee;

您也可以使用rgba()的旧方法:

background-color: rgba(117, 190, 218, 0.5);

如果您想确保背景没有其他样式,如图像或渐变,请使用背景速记:

background: #11ffeeaa;

根据Mozilla规范(https://developer.mozilla.org/en-US/docs/Web/CSS/background-color):

/* Keyword values */
background-color: red;
background-color: indigo;

/* Hexadecimal value */
background-color: #bbff00;    /* Fully opaque */
background-color: #bf0;       /* Fully opaque shorthand */
background-color: #11ffee00;  /* Fully transparent */
background-color: #1fe0;      /* Fully transparent shorthand  */
background-color: #11ffeeff;  /* Fully opaque */
background-color: #1fef;      /* Fully opaque shorthand  */

/* RGB value */
background-color: rgb(255, 255, 128);        /* Fully opaque */
background-color: rgba(117, 190, 218, 0.5);  /* 50% transparent */

/* HSL value */
background-color: hsl(50, 33%, 25%);         /* Fully opaque */
background-color: hsla(50, 33%, 25%, 0.75);  /* 75% transparent */

/* Special keyword values */
background-color: currentcolor;
background-color: transparent;

/* Global values */
background-color: inherit;
background-color: initial;
background-color: unset;