仅使用CSS,是否可以使元素的背景半透明,但元素的内容(文本和图像)不透明?
我想在不将文本和背景作为两个独立元素的情况下完成这一点。
尝试时:
p型{位置:绝对;背景色:绿色;过滤器:alpha(不透明度=60);不透明度:0.6;}跨度{颜色:白色;过滤器:alpha(不透明度=100);不透明度:1;}<p><span>你好,世界</span></p>
看起来子元素受到其父元素的不透明度的影响,因此不透明度:1相对于父元素的不可见性:0.6。
仅使用CSS,是否可以使元素的背景半透明,但元素的内容(文本和图像)不透明?
我想在不将文本和背景作为两个独立元素的情况下完成这一点。
尝试时:
p型{位置:绝对;背景色:绿色;过滤器:alpha(不透明度=60);不透明度:0.6;}跨度{颜色:白色;过滤器:alpha(不透明度=100);不透明度:1;}<p><span>你好,世界</span></p>
看起来子元素受到其父元素的不透明度的影响,因此不透明度:1相对于父元素的不可见性:0.6。
当前回答
有一个更简单的解决方案是在同一个分区上的图像上放置一个覆盖层。这不是该工具的正确用法。但是,使用CSS制作覆盖层就像一种魅力。
使用如下插入阴影:
box-shadow: inset 0 0 0 1000px rgba(255, 255, 255, 0.9);
仅此而已:)
其他回答
可以使用附加到十六进制值的不透明度值:
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;
最好使用半透明.png。
只需打开Photoshop,创建一个2x2像素的图像(选择1x1可能会导致Internet Explorer错误!),用绿色填充,并将“图层选项卡”中的不透明度设置为60%。然后保存它并使其成为背景图像:
<p style="background: url(green.png);">any text</p>
当然,它很酷,除了在可爱的InternetExplorer6中。有更好的修复方法可用,但这里有一个快速破解方法:
p {
_filter: expression((runtimeStyle.backgroundImage != 'none') ? runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+currentStyle.backgroundImage.split('\"')[1]+', sizingMethod=scale)' : runtimeStyle.filter,runtimeStyle.backgroundImage = 'none');
}
有一个技巧可以最小化标记:使用伪元素作为背景,可以在不影响主元素及其子元素的情况下设置其不透明度:
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和更高版本。
伪元素不透明度
如果你使用的是Less,你可以使用褪色(颜色,30%)。
<div align="center" style="width:100%;height:100%;background:white;opacity:0.5;position:absolute;z-index:1001">
<img id="search_img" style="margin-top:20%;" src="../resources/images/loading_small.gif">
</div>
http://jsfiddle.net/x2ukko7u/?