- 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 - 疊層
疊層是一個透明的內容層,放置在另一個元素的頂部。它可以用來建立不同的效果,例如模態視窗、工具提示或彈出視窗。
疊層元素應該設定為絕對定位,並且具有比內容元素更高的z-index值。這將確保疊層顯示在內容之上。
疊層效果示例
當您將滑鼠懸停在 TutorialsPoint 徽標上時,您可以看到疊層效果。這種樣式為訪問您網頁的使用者提供了動態體驗。
目錄
如何建立疊層效果?
要使用 CSS 建立疊層,請按照以下步驟操作
- **建立容器:** 使用容器元素來容納您想要疊加的內容。容器可以是 div 元素、span 元素甚至是影像。
- **設定定位:** 將容器設定為**position: relative**,以便其中的任何絕對定位元素都將相對於此容器進行定位。
- **新增疊層元素:** 在容器內新增另一個元素(疊層)並設定position: absolute以確保它覆蓋整個容器。還要確保疊層的 top、left 屬性設定為 0,並且 width、height 屬性設定為 100%,以便它完全覆蓋容器。
- **設定疊層樣式:** 使用rgba()函式設定疊層的背景顏色以獲得半透明效果。最初,將疊層的不透明度設定為 0,使其預設情況下不可見。
- **新增懸停效果:** 對疊層容器使用hover偽類,以便在使用者將滑鼠懸停在容器上時將疊層的不透明度從 0 更改為 1。
CSS 疊層基本示例
以下示例顯示如何使用上述步驟在 CSS 中建立簡單的疊層效果。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
height: 150px;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.overlay-container {
position: relative;
width: auto;
height: auto;
display: inline-block;
}
img {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 128, 0, 0.5);
opacity: 0;
transition: opacity 0.5s ease;
display: flex;
justify-content: center;
align-items: center;
}
.overlay-container:hover .overlay {
opacity: 1;
}
.login-button {
padding: 10px 20px;
background-color: #ffffff;
color: #228B22;
border: 2px solid #228B22;
border-radius: 5px;
text-decoration: none;
font-size: 16px;
font-weight: bold;
transition: background-color 0.3s, color 0.3s;
}
.login-button:hover {
background-color: #228B22;
color: #ffffff;
}
</style>
</head>
<body>
<div class="container">
<div class="overlay-container">
<img src="/html/images/test.png" alt="Image">
<div class="overlay">
<a href="/css/index.htm"
target="_blank" class="login-button">
Learn CSS
</a>
</div>
</div>
</div>
</body>
</html>
使用 JavaScript 的疊層效果
以下示例使用 JavaScript 建立疊層效果,該效果在您單擊按鈕時出現,在您單擊頁面上的任何位置時消失。
JavaScript 可以使用 'querySelector()' 方法獲取疊層元素來顯示和隱藏疊層 div 元素。當單擊按鈕時,將執行一個函式,該函式在塊(可見)和無(隱藏)之間切換疊層容器的 display 屬性。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
position: fixed;
display: none;
width: 100%;
height: 100%;
text-align: center;
background-color: rgba(213, 86, 207, 0.3);
z-index: 999;
cursor: pointer;
}
.content {
padding: 20px;
}
.button {
background-color: #38dc29;
color: white;
border: none;
padding: 20px;
cursor: pointer;
font-size: 20px;
text-align: center;
display: block;
margin: 50px auto;
}
</style>
</head>
<body>
<div class="container" onclick="overlay()">
<h1>TutorialsPoint CSS Overlay</h1>
</div>
<div style="padding:20px">
<button class="button" onclick="overlay()">
Click on Button
</button>
</div>
<script>
let Visible = false;
function overlay() {
const overlay = document.querySelector(".container");
overlay.style.display = Visible ? "none" : "block";
Visible = !Visible;
}
</script>
</body>
</html>
CSS 疊層自上而下滑動
以下示例顯示一個影像,當用戶將滑鼠懸停在其上時,該影像的疊層會從影像頂部向下滑動。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
img {
width: 200px;
height: 200px;
margin-left: 40%;
}
.overlay-container {
position: relative;
width: 25%;
height: auto;
}
.overlay-container:hover .top-bottom {
opacity: 1;
height: 200px;
}
.top-bottom{
position: absolute;
transition: 0.9s ease;
opacity: 0.3;
width: 200px;
border-radius: 5px;
height: 0;
top: 0;
left: 40%;
background-color: #d346e6;
text-align: center;
color: #ffffff;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<h2> Hover your cursor over the image.</h2>
<div class="overlay-container">
<img src= "/css/images/tutimg.png">
<div class="top-bottom">
<h2> Top to Bottom Image Overlay </h2>
</div>
</div>
</body>
</html>
CSS 疊層自下而上滑動
以下示例顯示一個影像,當用戶將滑鼠懸停在其上時,該影像的疊層會從影像底部向上滑動。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
img {
width: 200px;
height: 200px;
}
.overlay-container {
position: relative;
width: 25%;
height: auto;
}
.overlay-container:hover .bottom-top {
opacity: 1;
height: 200px;
}
.bottom-top {
position: absolute;
transition: 0.9s ease;
opacity: 0.3;
width: 200px;
border-radius: 5px;
height: 0;
bottom: 0;
background-color: #d346e6;
text-align: center;
color: #ffffff;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<h2>Hover your cursor over the image.</h2>
<div class="overlay-container">
<img src= "/css/images/tutimg.png">
<div class="bottom-top">
<h2>Bottom to Top Image Overlay</h2>
</div>
</div>
</body>
</html>
CSS 疊層自左向右滑動
以下示例顯示一個影像,當您將滑鼠懸停在其上時,該影像的疊層會從左向右滑動。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
img {
width: 200px;
height: 200px;
}
.overlay-container {
position: relative;
width: 25%;
height: auto;
}
.overlay-container:hover .left-right {
opacity: 1;
width: 200px;
}
.left-right {
position: absolute;
transition: 0.9s ease;
opacity: 0.3;
width: 0;
border-radius: 5px;
height: 200px;
top: 0;
left: 0;
background-color: #d346e6;
text-align: center;
color: #ffffff;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<h2>Hover your cursor over the image.</h2>
<div class="overlay-container">
<img src="/css/images/tutimg.png">
<div class="left-right">
<h2>Left to Right Image Overlay</h2>
</div>
</div>
</body>
</html>
CSS 疊層自右向左滑動
以下示例顯示一個影像,當您將滑鼠懸停在其上時,該影像的疊層會從右向左滑動。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
img {
width: 200px;
height: 200px;
}
.overlay-container {
position: relative;
width: 67%;
height: auto;
}
.overlay-container:hover .right-left {
opacity: 1;
width: 200px;
}
.right-left {
position: absolute;
transition: 0.9s ease;
opacity: 0.3;
width: 0;
border-radius: 5px;
height: 200px;
top: 0;
background-color: #d346e6;
text-align: center;
color: #ffffff;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<h2>Hover your cursor over the image.</h2>
<div class="overlay-container">
<img src="/css/images/tutimg.png">
<div class="right-left">
<h2>Right to Left Image Overlay</h2>
</div>
</div>
</body>
</html>
CSS 疊層淡入淡出效果
以下示例顯示如何在使用者將滑鼠懸停在其上時,在影像頂部建立一個疊層。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
img {
width: 200px;
height: 200px;
}
.overlay-container {
position: relative;
width: 25%;
}
.overlay-container:hover .fade-effect {
opacity: 0.9;
height: 200px;
}
.fade-effect {
position: absolute;
transition: 0.9s ease;
opacity: 0;
width: 200px;
height: 200px;
border-radius: 5px;
top: 0;
background-color: #d346e6;
text-align: center;
color: #ffffff;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<h2>Hover your cursor over the image.</h2>
<div class="overlay-container">
<img src= "/css/images/tutimg.png">
<div class="fade-effect">
<h2>Fade Overlay Effect</h2>
</div>
</div>
</body>
</html>
CSS 疊層圖片標題
這是一個疊層示例,當用戶將滑鼠懸停在其上時,它會顯示影像的標題。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
img {
width: 200px;
height: 200px;
}
.overlay-container {
position: relative;
width: 25%;
}
.overlay-container:hover .title-overlay {
opacity: 0.9;
height: 80px;
}
.title-overlay {
position: absolute;
transition: 0.9s ease;
opacity: 0;
width: 200px;
height: 80px;
border-radius: 5px;
bottom: 0;
background-color: #f0f0f0;
text-align: center;
}
h1 {
text-align: center;
color: black;
}
</style>
</head>
<body>
<h2>Hover your cursor over the image.</h2>
<div class="overlay-container">
<img src= "/css/images/tutimg.png">
<div class="title-overlay">
<h1>TutorialsPoint</h1>
</div>
</div>
</body>
</html>
CSS 圖片圖示疊層
這是一個疊層示例,當用戶將滑鼠懸停在其上時,它會顯示影像上的圖示。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet"
href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.overlay-container {
position: relative;
width: 200px;
height: 200px;
}
img {
width: 100%;
height: 100%;
}
.icon-overlay {
position: absolute;
transition: 0.9s ease;
opacity: 0;
width: 100%;
height: 100%;
top: 0;
background-color: rgba(211, 70, 230, 0.9);
text-align: center;
color: #ffffff;
display: flex;
justify-content: center;
align-items: center;
}
.overlay-container:hover .icon-overlay {
opacity: 1;
}
.display-icon {
color: rgb(60, 235, 50);
font-size: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<h2>Hover your cursor over the image.</h2>
<div class="overlay-container">
<img src="/css/images/tutimg.png">
<div class="icon-overlay">
<a href="#" class="display-icon">
<i class="fa fa-star"></i>
</a>
</div>
</div>
</body>
</html>
廣告