假设我有
<div class="myDiv">Hi there</div>
我想要设置一个带有 background-image
的元素,并且将其透明度设置为 0.5
,但我希望元素中的文字保持完全不透明(1
)。如果我按照如下方式编写 CSS:
.myDiv { opacity:0.5 }
everything会变成低透明度——我不想要那样。
因此我的问题是 - 如何获得低透明度背景图像和完全不透明文本?
假设我有
<div class="myDiv">Hi there</div>
我想要设置一个带有 background-image
的元素,并且将其透明度设置为 0.5
,但我希望元素中的文字保持完全不透明(1
)。.myDiv { opacity:0.5 }
everything会变成低透明度——我不想要那样。
因此我的问题是 - 如何获得低透明度背景图像和完全不透明文本?
所以这里是另一种方法:
background-image: linear-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0.5)), url("your_image.png");
linear-gradient
的支持非常有限。我写的被接受的答案已经超过4年了 ;) - mekwall不行,这是因为opacity
影响整个元素,包括其内容,没有办法改变这种行为。你可以用以下两种方法解决这个问题。
在容器中添加另一个div
元素来承载背景。这是最兼容各种浏览器的方法,甚至可以在IE6上使用。
HTML
<div class="myDiv">
<div class="bg"></div>
Hi there
</div>
CSS
.myDiv {
position: relative;
z-index: 1;
}
.myDiv .bg {
position: absolute;
z-index: -1;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url(test.jpg) center center;
opacity: .4;
width: 100%;
height: 100%;
}
查看在 jsFiddle 上的测试用例:链接
另一个技巧是使用 CSS 2.1 的 :before
或 CSS 3 的 ::before
伪元素。IE 8 及以上版本支持 :before
伪元素,而 ::before
伪元素则完全不被支持。希望在版本 10 中能够得到纠正。
HTML
<div class="myDiv">
Hi there
</div>
CSS
.myDiv {
position: relative;
z-index: 1;
}
.myDiv:before {
content: "";
position: absolute;
z-index: -1;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url(test.jpg) center center;
opacity: .4;
}
由于 z-index
的行为,您不仅需要为容器设置一个 z-index,还需要为背景图像设置一个负的 z-index
。
请在 jsFiddle 上查看测试用例:
.bg
中添加width: 100%; height: 100%;
,以便ie6可以将背景扩展到父div内。http://jsfiddle.net/sbGb4/2/ - Joonasz-index
很麻烦,但只要给父元素一个正的索引,这应该是相对安全的。 - mekwallz-index
或者:before
。请参见下面的答案。 - abalterdiv {
width: 200px;
height: 200px;
display: block;
position: relative;
}
div::after {
content: "";
background: url(image.jpg);
opacity: 0.5;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
<div> put your div content</div>
大家好,我做了这个,它运行得很好。
var canvas, ctx;
function init() {
canvas = document.getElementById('color');
ctx = canvas.getContext('2d');
ctx.save();
ctx.fillStyle = '#bfbfbf'; // #00843D // 118846
ctx.fillRect(0, 0, 490, 490);
ctx.restore();
}
section{
height: 400px;
background: url(https://images.pexels.com/photos/265087/pexels-photo-265087.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
position: relative;
}
canvas {
width: 100%;
height: 400px;
opacity: 0.9;
}
#text {
position: absolute;
top: 10%;
left: 0;
width: 100%;
text-align: center;
}
.middle{
text-align: center;
}
section small{
background-color: #262626;
padding: 12px;
color: whitesmoke;
letter-spacing: 1.5px;
}
section i{
color: white;
background-color: grey;
}
section h1{
opacity: 0.8;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Metrics</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body onload="init();">
<section>
<canvas id="color"></canvas>
<div class="w3-container middle" id="text">
<i class="material-icons w3-highway-blue" style="font-size:60px;">assessment</i>
<h1>Medimos las acciones de tus ventas y disenamos en la WEB tu Marca.</h1>
<small>Metrics & WEB</small>
</div>
</section>
我实现了Marcus Ekwall的解决方案,但是我能够删除一些东西使它变得更简单,并且它仍然有效。也许是html / css的2017版本?
html:
<div id="content">
<div id='bg'></div>
<h2>What is Lorem Ipsum?</h2>
<p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
CSS:
#content {
text-align: left;
width: 75%;
margin: auto;
position: relative;
}
#bg {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url('https://static.pexels.com/photos/6644/sea-water-ocean-waves.jpg') center center;
opacity: .4;
width: 100%;
height: 100%;
}
可以通过为文本使用不同的 div 类来实现,你好...
<div class="myDiv">
<div class="bg">
<p> Hi there</p>
</div>
</div>
标签。否则使用bg类。我相信它能正常工作。
<!DOCTYPE html>
<html>
<head></head>
<body>
<div style=" background-color: #00000088"> Hi there </div>
<!-- #00 would be r, 00 would be g, 00 would be b, 88 would be a. -->
</body>
</html>
没有一个解决方案适用于我。如果其他方法都失败了,将图片导入 Photoshop 并应用一些效果。5 分钟对比这么长时间……