温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用CSS3怎么实现背景透明文字不透明

发布时间:2021-05-14 16:48:19 来源:亿速云 阅读:167 作者:Leah 栏目:web开发

本篇文章为大家展示了使用CSS3怎么实现背景透明文字不透明,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <meta http-equiv="X-UA-Compatible" content="ie=edge">     <title>背景透明,文字也透明</title>     <style>         * {             padding: 0;             margin: 0;         }         .container {             width: 600px;             height: 400px;             background: url('https://img1.dongqiudi.com/fastdfs3/M00/18/56/ChOxM1stHByARuNmAAGsJDKXtuM269.jpg') no-repeat;             background-size: cover;             -webkit-background-size: cover;             -o-background-size: cover;             background-position: center 0;         }         .demo {             position: absolute;             width: 260px;             height: 60px;             top: 260px;             line-height: 60px;             text-align: center;             background-color: black;             opacity: 0.5;         }         .demo p {             color: #FFF;             font-size: 18px;             font-weight: 600;         }     </style> </head> <body>     <div class="container">         <div class="demo">             <p>2018世界杯已开幕:10天</p>         </div>     </div> </body> </html>

效果如下:

使用CSS3怎么实现背景透明文字不透明

背景透明,文字也透明.png

这样貌似也满足了需求,不过并不完美,设置opacity之后,整个元素都半透明了,造成文字显得模糊,这样的解决方式并不可取。

其实实现透明的CSS方法并不只有设置opacity一种方式。还有另外两种:

  • css3的rgba(red, green, blue, alpha),alpha的取值从 0 到 1,如rgba(255,255,255,0.8)

  • IE专属滤镜 filter:Alpha(opacity=x),x 的取值从 0 到 100,如filter:Alpha(opacity=80)

在这里我采用了设置rgba的方式:

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <meta http-equiv="X-UA-Compatible" content="ie=edge">     <title>背景透明,文字不透明</title>     <style>         * {             padding: 0;             margin: 0;         }         .container {             width: 600px;             height: 400px;             background: url('https://img1.dongqiudi.com/fastdfs3/M00/18/56/ChOxM1stHByARuNmAAGsJDKXtuM269.jpg') no-repeat;             background-size: cover;             -webkit-background-size: cover;             -o-background-size: cover;             background-position: center 0;         }         .demo {             position: absolute;             width: 260px;             height: 60px;             top: 260px;             line-height: 60px;             text-align: center;             background-color: rgba(0,0,0,0.5);         }         .demo p {             color: #FFF;             font-size: 18px;             font-weight: 600;         }     </style> </head> <body>     <div class="container">         <div class="demo">             <p>2018世界杯已开幕:10天</p>         </div>     </div> </body> </html>

上述内容就是使用CSS3怎么实现背景透明文字不透明,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI