- 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 - Clearfix
- 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 - 偽元素
CSS **偽元素** 用於為元素的特定部分設定樣式。瀏覽網頁時,您可能注意到某些段落首字母比其餘字母大。這種針對元素特定部分的樣式設定是使用 CSS 中的偽元素完成的。在本教程中,我們將解釋所有偽元素及其功能。
目錄
什麼是偽元素?
CSS 中的偽元素用於為元素的特定部分設定樣式,這些部分不屬於 DOM(文件物件模型)並且不存在於 HTML 標記中。例如,段落首字母、輸入元素中的佔位符文字或文件中的選定部分。
- 偽元素用雙冒號 (::) 表示法表示。
- 選擇器中只能使用一個偽元素。
- 選擇器中的偽元素必須出現在所有其他元件之後。例如,**p::last-line:hover** 是無效的。
- 偽元素可以用來新增裝飾性樣式,建立特殊效果,並修改已經應用了狀態的元素的某些部分的外觀。例如,**p:hover::last-line** 是一個有效的語句,它在將滑鼠懸停在段落上時選擇段落的最後一行。
語法
selector::pseudo-element {
property: value;
}
瀏覽器支援四個原始偽元素的單冒號語法,即 ::before、::after、::first-line 和 ::first-letter。
內容插入偽元素
在 CSS 中,偽元素 **::before** 和 **::after** 用於在任何元素之前和之後插入文字內容或影像。
示例
此示例演示如何使用 CSS 在段落的開頭和結尾插入文字和影像。
<!DOCTYPE html>
<html>
<head>
<style>
p:before {
content: "NOTE:";
font-weight: bold;
}
p:after {
content: url(/css/images/smiley.png);
}
</style>
</head>
<body>
<p>
We inserted intro at start and emoji at end.
</p>
</body>
</html>
CSS 背景偽元素
在 CSS 中,**::backdrop** 偽元素用於設定處於模態上下文中的元素的背景樣式,例如在顯示 **<dialog>** 元素時其後面的背景。
示例
以下示例演示了 **::backdrop** 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
body{
height: 200px;
}
dialog {
padding: 20px;
border: 2px solid black;
border-radius: 10px;
}
dialog::backdrop {
/* Semi-transparent black */
background-color: rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<h3> Backdrop Example </h3>
<dialog id="myDialog">
<p> This is a dialog with a styled backdrop. </p>
<button id="closeButton"> Close </button>
</dialog>
<button id="openButton">Open Dialog</button>
<script>
const dialog = document.getElementById('myDialog');
const openButton = document.getElementById('openButton');
const closeButton = document.getElementById('closeButton');
openButton.addEventListener('click', () => {
dialog.showModal();
});
closeButton.addEventListener('click', () => {
dialog.close();
});
</script>
</body>
</html>
CSS 提示偽元素
在 CSS 中,偽元素 **::cue** 與 Web 影片文字軌道一起使用,為媒體元素(如 **<video>** 和 **<audio>**)的字幕或標題等文字軌道的特定部分設定樣式。
示例
以下示例演示了 ::cue 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
video {
width: 100%;
}
video::cue {
font-size: 1rem;
color: peachpuff;
}
</style>
</head>
<body>
<video controls src="/css/foo.mp4">
<track default kind="captions"
srclang="en" src="/css/cue-sample.vtt" />
</video>
</body>
</html>
CSS 首字母偽元素
在 CSS 中,**::first-letter** 偽元素用於定位任何元素(如 **div**、**段落**、**span** 等)文字內容的首字母。
示例
以下示例演示了 **::first-letter** 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
text-transform: uppercase;
font-size: 2em;
color: darkred;
font-style: italic;
}
</style>
</head>
<body>
<p>
this is a paragraph with first letter in lowercase,
we used ::first-letter pseudo-element to capitalize
first-letter of paragraph with a larger font size
and a different color.
</p>
</body>
</html>
CSS 首行偽元素
在 CSS 中,**::first-line** 偽元素用於定位任何元素(如 **div**、**段落**、**span** 等)文字內容的首行。
示例
以下示例演示了 **::first-line** 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
p::first-line {
background-color: #f0f0f0;
color: darkred;
font-style: italic;
}
</style>
</head>
<body>
<p>
This is a normal paragraph with no stylings, we used
::first-line pseudo-element to only style first-line of
paragraph by adding a background color, font-style and
text color
</p>
</body>
</html>
CSS 檔案選擇器按鈕偽元素
在 CSS 中,**::file-selector-button** 偽元素用於在現代瀏覽器中設定檔案 **input** 元素 (<input type="file">) 的按鈕樣式。
示例
以下示例演示了 **::file-selector-button** 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: block;
height: 100px;
}
input::file-selector-button {
background-image:url(/css/images/border.png);
background-size: 200%;
border: 2px solid black;
border-radius: 8px;
font-weight: 600;
color: rgb(6, 1, 9);
padding: 15px;
transition: all 0.25s;
}
</style>
</head>
<body>
<h2> Select a file </h2>
<input type="file">
</body>
</html>
CSS 標記偽元素
在 CSS 中,**::marker** 偽元素用於設定 **有序列表** 和 **無序列表** 的標記樣式。
示例
以下示例演示了 **::marker** 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
ol li::marker {
color: rgb(11, 38, 241);
font-weight: bold;
}
ul li::marker {
content: url('/css/images/smiley.png')
}
</style>
</head>
<body>
<h2>Numbered list</h2>
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
<h2>Bulleted list</h2>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</body>
</html>
CSS 佔位符偽元素
在 CSS 中,**::placeholder** 偽元素用於設定文字 **input** 元素 (<input type="text">) 內的預設文字樣式。
示例
以下示例演示了 **::placeholder** 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
.form {
border: 2px solid black;
background: lightgray;
padding: 25px;
display: flex;
flex-direction: column;
gap: 10px;
}
input{
padding: 10px;
background-color: cornsilk;
}
input::placeholder {
color: grey;
font-style: italic;
font-size: 20px;
}
</style>
</head>
<body>
<div class="form">
<h2> Your Details:</h2>
<input type="text" placeholder="First Name">
<input type="text" placeholder="Last Name">
<input type="text" placeholder="Address">
<input type="text" placeholder="Phone">
</div>
</body>
</html>
CSS 選擇偽元素
在 CSS 中,**::selection** 偽元素用於設定任何元素(如 **div**、**段落**、**span** 等)內使用者選中文字的樣式。
示例
以下示例演示了 **::selection** 偽元素的使用。
<!DOCTYPE html>
<html>
<head>
<style>
.highlight::selection {
color: yellow;
background: brown;
}
</style>
</head>
<body>
<p class="highlight">
Select Me!!! to see the effect.
</p>
<p> No style applied to me. </p>
</body>
</html>
多個偽元素
我們還可以向選擇器新增多個偽元素,檢視示例。
示例
以下示例演示了多個偽元素 (::first-line 和 ::first-letter) 的用法。
<!DOCTYPE html>
<html>
<head>
<style>
p::first-line {
text-decoration: underline;
}
p::first-letter {
text-transform: uppercase;
font-size: 2em;
color: red;
}
</style>
</head>
<body>
<p>
the first line of this paragraph will be underlined and
first letter is uppercase, 2em and red in color, as the
pseudo-element ::first-line & ::first-letter is applied
on p. The other lines are not underlined.
</p>
</body>
</html>
所有 CSS 偽元素
下表顯示了 CSS 中的所有偽元素
| 偽元素 | 描述 | 示例 |
|---|---|---|
| ::after | 新增一個作為所選元素的最後一個子元素的偽元素。 | |
| ::backdrop | 用於設定對話方塊等元素的背景樣式。 | |
| ::before | 新增一個作為所選元素的第一個子元素的偽元素。 | |
| ::cue | 用於設定帶有影片文字軌道的媒體中的字幕和提示的樣式。 | |
| ::first-letter | 將樣式應用於塊級元素第一行的首字母。 | |
| ::first-line | 將樣式應用於塊級元素的第一行。 | |
| ::file-selector-button | 表示 type="file" 的 <input> 的按鈕。 | |
| ::marker | 選擇列表項的標記框。 | |
| ::part() | 表示具有匹配 part 屬性的影子樹中的元素。 | |
| ::placeholder | 表示 <input> 或 <textarea> 元素中的佔位符文字。 | |
| ::selection | 將樣式應用於文件的選定部分(透過單擊並拖動滑鼠跨越文字進行選擇)。 | |
| ::slotted() | 表示已放置到 HTML 模板內的插槽中的元素。 | |
| ::grammar-error | 用於設定瀏覽器內建語法檢查工具已識別為語法錯誤的文字的樣式。 | |
| ::spelling-error | 用於設定瀏覽器內建拼寫檢查工具已識別為拼寫錯誤的文字的樣式。 |