2026-Css忘掉一切、归零再启-alpha和opacity

alpha通道和opacity半透明效果

区别

1. alpha通道控制的更为精细,控制"单个元素""单个属性"的透明度(例如:单独控制文字透明或背景透明)
2. opacity控制"整个"标签内的所有元素的透明度
左示例 复制代码
.left {
        background-color: rgba(52, 152, 219, 0.5);
    }
    
右示例 复制代码
.right {
        background-color: rgba(52, 152, 219);
        opacity: 0.5;
    }
源码 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="container">
        <div class="content left">
            alpha 通道
        </div>
        <div class="content right">
            opacity
        </div>
    </div>
</body>
<style>
    * {
        height: 100%;
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }



    .container {
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 10px;

    }


    .content {
        align-content: center;
        width: 200px;
        height: 200px;
        font-size: 30px;
        text-align: center;
        padding: 5px;
        border: 10px solid orange;


    }

    .left {
        background-color: rgba(52, 152, 219, 0.5);
    }

    .right {
        background-color: rgba(52, 152, 219);
        opacity: 0.5;
    }
</style>

</html>
相关推荐
用户059540174461 小时前
把 AI Agent 记忆验证从手工比对换成 Pytest + 向量数据库,测试效率提升 10 倍
前端·css
光影少年1 小时前
前端如何和蓝牙物联网进行通信和兼容性问题
前端·物联网·掘金·金石计划
星栈1 小时前
我把售后模块砍到只剩 64 行:Rust 全栈 CRM 的 MVP 取舍实录
前端·后端·开源
玉宇夕落1 小时前
懒加载与Suspense的学习
前端
用户1733598075371 小时前
纯前端实现PDF合并、拆分、压缩:技术方案与踩坑记录
前端
工会代表1 小时前
frps配置,以linux服务器以及windows客户端进行远程桌面内网穿透为例。
前端
用户713874229001 小时前
Promise 与 Async Await 深度解析
前端
董董灿是个攻城狮1 小时前
5分钟入门卷积算法
前端
用户58048170029281 小时前
我用 MCP 给小程序开发做了个 AI 副驾驶,开源了
前端