是否有可能,仅使用CSS来制作 background
元素半透明但元素的内容(文本和图像)是不透明的?
我想在不将文本和背景作为两个单独元素的情况下实现此目的。
尝试时:
p {
position: absolute;
background-color: green;
filter: alpha(opacity=60);
opacity: 0.6;
}
span {
color: white;
filter: alpha(opacity=100);
opacity: 1;
}
<p>
<span>Hello world</span>
</p>
起源
答案:
要么使用半透明 PNG 图像或使用CSS3:
background-color:rgba(255,0,0,0.5);
这是css3.info的一篇文章, 不透明度,RGBA和妥协 (2007-06-03)。
在Firefox 3和Safari 3中,您可以使用RGBA GeorgSchölly提到。
一个鲜为人知的技巧是您可以使用渐变过滤器在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)";
}
这是来自 CSS背景透明度,不影响子元素,通过RGBa和过滤器。
截图结果证明:
这是在使用以下代码时:
<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 3.就我所见,它在Firefox,Chrome和Internet Explorer上运行良好。
将容器DIV和两个子DIV放在同一级别,一个用于内容,一个用于背景。 并使用CSS,自动调整背景大小以适应内容,并使用z-index将背景实际放在后面。
.container {
position: relative;
}
.content {
position: relative;
color: White;
z-index: 5;
}
.background {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background-color: Black;
z-index: 1;
/* These three lines are for transparency in all browsers. */
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
filter: alpha(opacity=50);
opacity: .5;
}
<div class="container">
<div class="content">
Here is the content.
<br />Background should grow to fit.
</div>
<div class="background"></div>
</div>
最好使用半透明的 .png
。
刚开 Photoshop中, 创建一个 2x2
像素图像(选择 1x1
可能导致Internet Explorer错误!),用绿色填充,并将“图层选项卡”中的不透明度设置为60%。然后保存并使其成为背景图像:
<p style="background: url(green.png);">any text</p>
当然,除了可爱之外,它很酷 Internet Explorer 6中。有更好的修复可用,但这是一个快速的黑客:
p {
_filter: expression((runtimeStyle.backgroundImage != 'none') ? runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+currentStyle.backgroundImage.split('\"')[1]+', sizingMethod=scale)' : runtimeStyle.filter,runtimeStyle.backgroundImage = 'none');
}
对于简单的半透明背景颜色,上述解决方案(CSS3或bg图像)是最佳选择。但是,想要做更好的事情(例如动画,多个背景等),或者如果你不想依赖CSS3,你可以尝试“窗格技术”:
.pane, .pane > .back, .pane > .cont { display: block; }
.pane {
position: relative;
}
.pane > .back {
position: absolute;
width: 100%; height: 100%;
top: auto; bottom: auto; left: auto; right: auto;
}
.pane > .cont {
position: relative;
z-index: 10;
}
<p class="pane">
<span class="back" style="background-color: green; opacity: 0.6;"></span>
<span class="cont" style="color: white;">Hello world</span>
</p>
该技术通过在外部窗格元素内使用两个“图层”来工作:
- 一个(“后面”)适合窗格元素的大小而不影响内容的流动,
- 和一个(“cont”)包含内容,并帮助确定窗格的大小。
该 position: relative
在窗格上很重要;它告诉后面层适合窗格的大小。 (如果你需要的话 <p>
标记为绝对标记,从a更改窗格 <p>
到了 <span>
并将所有这些包裹在绝对位置 <p>
标签。)
该技术相对于上面列出的类似技术的主要优点是窗格不必是指定的大小;如上所述,它将适合全宽(正常的块元素布局)并且仅与内容一样高。外部窗格元素的大小可以任意方式,只要它是矩形的(即内联块可以工作;普通的内联不会)。
此外,它为您提供了很多自由背景;你可以自由地在后面的元素中添加任何东西,并且它不会影响内容的流动(如果你想要多个全尺寸的子图层,只要确保它们也有位置:绝对,宽度/高度:100%,和上/下/左/右:自动)。
允许背景插入调整(通过顶部/底部/左/右)和/或背景固定(通过移除左/右或顶部/底部对之一)的一种变体是使用以下CSS:
.pane > .back {
position: absolute;
width: auto; height: auto;
top: 0px; bottom: 0px; left: 0px; right: 0px;
}
正如所写,这适用于Firefox,Safari,Chrome,IE8 +和Opera,虽然IE7和IE6需要额外的CSS和表达式,IIRC,并且上次我检查时,第二个CSS变体在Opera中不起作用。
需要注意的事项:
- 不包含cont图层内的浮动元素。你需要确保它们被清除或以其他方式包含,否则它们会从底部滑落。
- 边距位于窗格元素上,填充位于cont元素上。不要使用相反的方法(窗格上的cont或padding上的边距),否则你会发现奇怪的事情,例如页面总是略宽于浏览器窗口。
- 如上所述,整个事情需要是块或内联块。随意使用
<div>
而不是<span>
s来简化你的CSS。
一个更全面的演示,通过与它一起使用来展示这种技术的灵活性 display: inline-block
和两者兼而有之 auto
具体 width
S /min-height
S:
.pane, .pane > .back, .pane > .cont { display: block; }
.pane {
position: relative;
width: 175px; min-height: 100px;
margin: 8px;
}
.pane > .back {
position: absolute; z-index: 1;
width: auto; height: auto;
top: 8px; bottom: 8px; left: 8px; right: 8px;
}
.pane > .cont {
position: relative; z-index: 10;
}
.debug_red { background: rgba(255, 0, 0, 0.5); border: 1px solid rgba(255, 0, 0, 0.75); }
.debug_green { background: rgba(0, 255, 0, 0.5); border: 1px solid rgba(0, 255, 0, 0.75); }
.debug_blue { background: rgba(0, 0, 255, 0.5); border: 1px solid rgba(0, 0, 255, 0.75); }
<p class="pane debug_blue" style="float: left;">
<span class="back debug_green"></span>
<span class="cont debug_red">
Pane content.<br/>
Pane content.
</span>
</p>
<p class="pane debug_blue" style="float: left;">
<span class="back debug_green"></span>
<span class="cont debug_red">
Pane content.<br/>
Pane content.<br/>
Pane content.<br/>
Pane content.<br/>
Pane content.<br/>
Pane content.<br/>
Pane content.<br/>
Pane content.<br/>
Pane content.
</span>
</p>
<p class="pane debug_blue" style="float: left; display: inline-block; width: auto;">
<span class="back debug_green"></span>
<span class="cont debug_red">
Pane content.<br/>
Pane content.
</span>
</p>
<p class="pane debug_blue" style="float: left; display: inline-block; width: auto; min-height: auto;">
<span class="back debug_green"></span>
<span class="cont debug_red">
Pane content.<br/>
Pane content.
</span>
</p>
这是一个 现场演示 广泛使用的技术:
有一个技巧可以最小化标记:使用a 伪元素 作为背景,你可以设置不透明度而不影响主元素及其子元素:
输出:
相关代码:
p {
position: relative;
}
p:after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #fff;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
opacity: .6;
z-index: -1;
}
/*** The following is just for demo styles ***/
body {
background: url('http://i.imgur.com/k8BtMvj.jpg') no-repeat;
background-size: cover;
}
p {
width: 50%;
padding: 1em;
margin: 10% auto;
font-family: arial, serif;
color: #000;
}
img {
display: block;
max-width: 90%;
margin: .6em auto;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a ligula ut nunc dignissim molestie.
<img src="http://i.imgur.com/hPLqUtN.jpg" alt="" />
</p>
浏览器支持 Internet Explorer 8中 然后。
最简单的方法是使用半透明背景 PNG图片。
您可以使用JavaScript使其工作 Internet Explorer 6
如果你需要。
我用的是方法 Internet Explorer 6中的透明PNG。
除此之外,
你可以伪造它 two side-by-side sibling elements
- 制作 一个半透明的, 然后 absolutely position the other over the top
?
此方法允许您在背景中拥有图像,而不仅仅是纯色,并且可以用于在其他属性(如边框)上具有透明度。不需要透明的PNG图像。
使用 :before
(要么 :after
)在CSS中给它们不透明度值,使元素保持原始的不透明度。因此,您可以使用:before之前制作一个人造元素,并为其提供所需的透明背景(或边框),并将其移动到您想要保持不透明的内容之后 z-index
。
一个例子 (小提琴)(注意 DIV
与班级 dad
只是为了提供一些上下文和颜色的对比,实际上不需要这个额外的元素,并且红色矩形向下移动一点向右移动以留下可见的背景。 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属性(在此示例中为红色背景,但可以是图像或边框)。它被定位为绝对移动它 .fancyBg
(使用零或任何更适合您需求的值)。
问题是,文本实际上在您的示例中完全不透明。它内部完全不透明 p
标签,但是 p
标签只是半透明的。
您可以添加半透明的PNG背景图像,而不是在CSS中实现它,或者将文本和div分成2个元素并在框上移动文本(例如,负边距)。
否则就不可能。
编辑:
就像Chris提到的那样:如果你使用具有透明度的PNG文件,你必须使用JavaScript解决方法使其在讨厌的Internet Explorer中工作...