- 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
廣告




