加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

用CSS3怎么制作带半透明背景效果的文字

发布时间:2023-10-14 14:47:12 所属栏目:语言 来源:
导读:看到这个需求之后,第一反应是使用CSS3中的opacity设置元素的透明度。

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device
看到这个需求之后,第一反应是使用CSS3中的opacity设置元素的透明度。
 
<!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>
 
背景透明,文字也透明.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>
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章