css设置背景透明文字不透明,以及字体透明背景不透明的写法

实际展示效果图如下

1、背景透明,字体不透明的是第一个divNotOpacity

样式:background: rgba(0, 0, 0, 0.7);

前三个是rgb的色素值,最后的是背景色的透明度(0-1)

2、字体透明divOpacity

样式:color:rgba(255,255,255,0.5);

字体透明使用color,前三个是rgb的色素值,后面是字体的透明度0-1

谨记:字体透明用的是color,而背景透明,字体不透明使用的是background

示例:

<!DOCTYPE html>
<html lang="zh">
<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>h5结构</title>
    <style type="text/css">
        .divNotOpacity{
            left: 200px;
            top: 200px; 
            position: absolute;/* 绝对定位 */
            /* 以下为本身div的样式 */
            height: 50px;
            width: 400px;
            bottom: 0;
            color: #ffffff;
            background: rgba(0, 0, 0, 0.7); /* 背景透明,字体不透明(前三个值为rgb色素,后面的值为背景的透明度0-1) */    
            display: flex;/* 设置为弹性布局 */
            justify-content: space-around;/* 内容左右居中显示 */
            align-items: center; /* 内容上下居中显示 */
        }
        .divOpacity{
            left: 200px;
            top: 300px;
            position: absolute;/* 绝对定位 */
            /* 以下为本身div的样式 */
            height: 50px;
            width: 400px;
            background:black;
            color:rgba(255,255,255,0.5); /* 字体透明使用color,前三个是rgb,后面是字体的透明度0-1 */
            display: flex;/* 设置为弹性布局 */
            justify-content: space-around;/* 内容左右居中显示 */
            align-items: center; /* 内容上下居中显示 */
        }
    </style>
</head>
<body>
    <div class="divNotOpacity">
        背景透明,字体显示在最上面,不透明
    </div>
    <div class="divOpacity">
        字体是透明的
    </div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/www1056481167/article/details/104819762