- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 行内边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 行内块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 定位
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 焦点
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 溢出滚动
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块尺寸
- CSS - 最小块尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移量
- CSS - 重音色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文字效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - !important
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多重背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2D 变换
- CSS - 3D 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - opacity 属性
CSS opacity 属性控制元素的不透明度。它决定了隐藏元素内容的可见程度。此属性可用于各种元素,无论它们包含文本、图像还是用作背景。
语法
opacity: number | percentage | initial | inherit;
属性值
| 值 | 描述 |
|---|---|
| 数字 | 可以使用 0 到 1(包括 0 和 1)之间的数值指定元素的不透明度。0 表示完全透明,1 表示完全不透明,0 到 1 之间的数值表示中间透明度。 |
| 百分比 | 可以使用百分比值指定元素的不透明度。0% 表示完全透明,100% 表示完全不透明,0% 到 100% 之间的数值表示中间透明度。 |
| initial | 将属性设置为其默认值。 |
| inherit | 从父元素继承属性。 |
CSS 不透明度属性示例
以下示例使用不同的值解释了opacity 属性。
使用数值的不透明度属性
为了设置元素的透明度,我们可以将 0 到 1(包括 0 和 1)之间的数值指定给opacity 属性。0 表示完全透明,而 1 表示完全不透明,0 到 1 之间的数值显示中间透明度效果。以下示例中使用了这些值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.props {
height: 50px;
padding: 20px;
text-align: center;
font-weight: bold;
font-size: 20px;
background-color: lightgreen;
}
.first {
opacity: 0.3;
}
.second {
opacity: 0.6;
}
.third {
opacity: 1;
}
</style>
</head>
<body>
<h2>
CSS opacity property
</h2>
<h4>
opacity: 0.3
</h4>
<div class="props first">
This div has opacity: 0.3
</div>
<h4>
opacity: 0.6
</h4>
<div class="props second">
This div has opacity: 0.6
</div>
<h4>
opacity: 1
</h4>
<div class="props third">
This div has opacity: 1
</div>
</body>
</html>
使用百分比值的不透明度属性
为了设置元素的透明度,我们可以将 0% 到 100%(包括 0% 和 100%)之间的百分比值指定给opacity 属性。0% 表示完全透明,而 100% 表示完全不透明,0% 到 100% 之间的数值显示中间透明度效果。以下示例中使用了这些值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.props {
height: 50px;
padding: 20px;
text-align: center;
font-weight: bold;
font-size: 20px;
background-color: lightgreen;
}
.first {
opacity: 30%;
}
.second {
opacity: 60%;
}
.third {
opacity: 100%;
}
</style>
</head>
<body>
<h2>
CSS opacity property
</h2>
<h4>
opacity: 30%
</h4>
<div class="props first">
This div has opacity: 30%
</div>
<h4>
opacity: 60%
</h4>
<div class="props second">
This div has opacity: 60%
</div>
<h4>
opacity: 100%
</h4>
<div class="props third">
This div has opacity: 100%
</div>
</body>
</html>
图像中的不透明度属性
透明度效果也可以用于图像。在以下示例中,图像使用了不同的opacity 值来显示效果。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.props {
width: 300px;
height: 150px;
}
.first-img {
opacity: 0.1;
}
.second-img {
opacity: 50%;
}
.third-img {
opacity: 1;
}
</style>
</head>
<body>
<h2>
CSS opacity property
</h2>
<h4>
opacity: 0.1
</h4>
<img class="first-img props" src="/css/images/logo.png"
alt="Tutorialspoint">
<h4>
opacity: 50%
</h4>
<img class="second-img props" src="/css/images/logo.png"
alt="Tutorialspoint">
<h4>
opacity: 1
</h4>
<img class="third-img props" src="/css/images/logo.png"
alt="Tutorialspoint">
</body>
</html>
使用 RGBA 颜色值的不透明度属性
opacity 属性将透明度应用于元素及其子元素。在许多情况下,这可能并不理想,因为所有子元素也变得透明。为了避免这种情况并仍然实现透明度,我们可以使用rgba 值。这些值仅指定元素的颜色和不透明度,而不是其子元素。以下示例中使用了这些值。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 200px;
padding: 10px;
text-align: center;
}
.first-color {
background-color: rgba(77, 77, 255);
}
.decimal-opacity1 {
opacity: 0.1;
}
.decimal-opacity2 {
opacity: 0.3;
}
.decimal-opacity3 {
opacity: 0.6;
}
.decimal-opacity4 {
opacity: 0.9;
}
.rgba-opacity1 {
background-color: rgba(77, 77, 255, 0.1);
}
.rgba-opacity2 {
background-color: rgba(77, 77, 255, 0.3);
}
.rgba-opacity3 {
background-color: rgba(77, 77, 255, 0.6);
}
.rgba-opacity4 {
background-color: rgba(77, 77, 255, 0.9);
}
.container {
font-weight: bolder;
margin-left: 50px;
float: left;
}
</style>
</head>
<body>
<h2>
CSS opacity property
</h2>
<p>
It is clear from the
<strong>
transparent element
</strong>
portion below, that the opacity property is
applied to all other elements present within
the element that uses opacity property.
</p>
<p>
To prevent this, we can make use of the
<strong>
rgba values
</strong>
which define the color along with opacity. The
opacity is applied only to the particular element
and not to the elements present within it.
</p>
<div class="container">
<h4>
Tranparent element
</h4>
<div class=" first-color decimal-opacity1">
CSS Opacity 0.1
</div>
<div class="first-color decimal-opacity2">
CSS Opacity 0.3
</div>
<div class="first-color decimal-opacity3">
CSS Opacity 0.6
</div>
<div class="first-color decimal-opacity4">
CSS Opacity 0.9
</div>
</div>
<div class="container">
<h4>
With RGBA color values
</h4>
<div class="rgba-opacity1">
CSS Opacity 10%
</div>
<div class="rgba-opacity2">
CSS Opacity 30%
</div>
<div class="rgba-opacity3">
CSS Opacity 60%
</div>
<div class="rgba-opacity4">
CSS Opacity 90%
</div>
</div>
</body>
</html>
支持的浏览器
| 属性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| opacity | 4.0 | 9.0 | 2.0 | 3.1 | 9.0 |
css_properties_reference.htm
广告




