opacity属性的运用方法
形貌以下
opacity:值;
在值部份中,能够运用0.0到1.0的数值指定不通明度。数字越大,不通明度越高,显现的越清楚。相反,数字越小,不通明度越低,显现的越隐约。另外,假如为值输入inherit,则继续父元素的值。
纵然将其设置在0.0到1.0的局限以外,也能够在此局限内运用(比方:-1→ 0/2 →1)将不通明度设置为0使其完整通明,并从屏幕上消逝。
当不通明度设置为1时,它变得完整不通明。(初始值)
接下来让我们来看opacity属性的详细示例
当值是小数点时
代码以下
HTML代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="sample.css" type="text/css"> </head> <body> <div> <p>当值是小数的情况下</p> </div> </body> </html>
CSS代码
div {background-color:#b0e0e6; } p {opacity:0.5; }
在这里,我们在<div>地区的<p>段落中输入文本,背景色彩为#b0e0e6,并将<p>的不通明度值设置为0.5。
在浏览器上显现结果以下:
段落的字符部份的不通明度已转变,它显现出稍微通明。另外,由于<div>地区中未设置不通明度,因而背景色彩的不通明度未变动。
当值是0或1时
代码以下
HTML代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="sample.css" type="text/css"> </head> <body> <div> <p class="text1">当属性值是0的时刻</p> </div> <div> <p class="text2">当属性值是1的时刻</p> </div> </body> </html>
CSS代码
div {background-color:#b0e0e6; } p.text1 {opacity:0; } p.text2 {opacity:1; }
在浏览器上显现结果以下
第一行的笔墨是完整通明的,因而不会在页面上显现,第二行上的笔墨显现为原样,由于它完整不通明。
由于1是初始值,因而假如未在父元素中指定不通明度,则其不通明度稳定。
下面我们就来看看运用opacity属性对图象不通明度的设置
我们来直接看代码
HTML代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="sample.css" type="text/css"> </head> <body> <div class="cover"> <h2 class="cover-title"> <span class="inline"> <i class="fa fa-code fa-l"></i> ki4网</span> </h2> <p class="cover-caption">opacity属性的运用方法</p> </div> </body> </html>
CSS代码
.cover { background-image:url("img/tupian.jpg"); width: 100%; position: relative; overflow: hidden; background-position: center; background-size: cover; background-repeat: no-repeat; }
在浏览器上显现结果以下
在不通明属性值为1的情况下,图象的显现并不会转变,这里就不多说了。
当不通明属性值为0.5时,CSS代码以下
.cover { background-image:url("img/tupian.jpg"); width: 100%; position: relative; overflow: hidden; background-position: center; background-size: cover; background-repeat: no-repeat; opacity: 0.5; }
结果以下
当不通明属性值为0.2时,将会变得越发隐约,结果以下
以上就是CSS的opacity属性该怎样运用的细致内容,更多请关注ki4网别的相干文章!