css中与设置透明效果相关的属性有两个:opacity和rgba()。下面我们就用这两个属性来分别设置图片透明度的效果。
首先我们来看css中opacity属性设置图片透明度的例子
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> .opacity1 { opacity: 0.5; filter: none; } .opacity2 { opacity: 0.2; filter: none; } </style> </head> <body> <div> 正常图片:<br /><br /> <img src="1.jpg-600" width="300px" /></div><br /> <div> 设置透明度的图片:<br /><br /> <img class="opacity1" src="1.jpg-600" width="300px" /> <img class="opacity2" src="1.jpg-600" width="300px" /> </div> </body> </html>
效果图:
注意:目前主流的浏览器都支持opacity:value写法,value取值为0-1,0为完全透明,1为完全不透明。但是在IE8及之前的版本中是不支持这种写法,那么我们可以通过滤镜来解决 filter:alpha(opacity=value),value取值为0-100,0为完全透明,100为完全不透明。就像上面例子那样。
我们再来看看css中rgba设置图片透明的例子:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> .demo2-bg { background: url(1.jpg-600) no-repeat; background-size: cover; width: 400px; height: 200px; position: relative; } .demo2 { position: absolute; left: 0; right: 0; top: 0; bottom: 0; width: 400px; height: 200px; line-height: 50px; text-align: center; background: rgba(255, 255, 255, 0.3); } </style> </head> <body> <div class="demo2-bg"> <div class="demo2">背景图半透明,文字不透明<br /> 方法:定位+ background:rgba(255,255,255,0.3)</div> </div> </body> </html>
效果如下:
说明:此方法浏览器兼容性好,图片和内容都能很好分离实现背景图片半透明效果,而文字、边框等样式与内容不受影响。只是多了一层div,使用绝对定位样式来实现重叠层叠。
设置背景颜色值与透明度。前3个255为代表RGB黑色,0.4代表透明度为30%。
以上就是css怎么设置图片透明度?的详细内容,更多请关注0133技术站其它相关文章!