- 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 - 製表符大小
- 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 - 重要
- 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 - display 屬性
CSS 的 display 屬性用於指定元素在網頁上如何顯示。它控制元素的佈局和可見性。display 屬性在設定元素的內部和外部顯示型別時非常有用。
語法
display: value;
屬性值
| 值 | 描述 |
|---|---|
| inline | 將元素顯示為內聯元素,寬度和高度屬性對其無效。預設值。 |
| block | 將元素顯示為塊級元素,從新行開始,佔據整行寬度。 |
| contents | 使元素從佈局中消失,同時保持其子元素可見並保持其在佈局中的原始位置。 |
| flex | 將元素顯示為塊級彈性容器。 |
| grid | 將元素顯示為塊級網格容器。 |
| inline-block | 允許元素與其他內聯元素一起流動,同時具有塊級特性,如寬度和高度。 |
| inline-flex | 將元素顯示為內聯級彈性容器。 |
| inline-grid | 將元素顯示為內聯級網格容器。 |
| inline-table | 將元素顯示為內聯級表格。 |
| run-in | 根據上下文將元素顯示為塊級或內聯級。 |
| table | 使元素像 <table> 元素一樣。 |
| table-caption | 使元素像 <caption> 元素一樣。 |
| table-column-group | 使元素像 <colgroup> 元素一樣。 |
| table-header-group | 使元素像 <thead> 元素一樣。 |
| table-footer-group | 使元素像 <tfoot> 元素一樣。 |
| table-row-group | 使元素像 <tbody> 元素一樣。 |
| table-cell | 使元素像 <td> 元素一樣。 |
| table-column | 使元素像 <col> 元素一樣。 |
| table-row | 使元素像 <tr> 元素一樣。 |
| none | 完全移除元素。 |
| initial | 將屬性設定為其預設值。 |
| inherit | 從父元素繼承屬性。 |
CSS Display 屬性示例
以下示例說明了使用不同值的 display 屬性。
使用 Inline 值的 Display 屬性
要使元素表現得像內聯級元素,以便元素在文字和其他內聯元素內流動,而不是強制換行,只佔用所需的寬度,我們使用 inline 值。在這種情況下,寬度和高度屬性沒有任何影響。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.inline-item {
display: inline;
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 5px;
}
.container {
border: 3px solid #ccc;
padding: 15px;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: inline
</h4>
<div class="container">
<span class="inline-item">
Item 1
</span>
<span class="inline-item">
Item 2
</span>
<span class="inline-item">
Item 3
</span>
</div>
</body>
</html>
使用 Block 值的 Display 屬性
要使元素表現得像塊級元素,以便它從新行開始並佔據可用的完整寬度,延伸到其容器的左右邊緣,我們使用 block 值。寬度和高度屬性會影響元素。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.block-item {
display: block;
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 5px;
}
.container {
border: 3px solid #ccc;
padding: 15px;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: block
</h4>
<div class="container">
<span class="block-item">
Item 1
</span>
<span class="block-item">
Item 2
</span>
<span class="block-item">
Item 3
</span>
</div>
</body>
</html>
使用 Contents 值的 Display 屬性
要使元素本身從佈局中消失,但其子元素保留在佈局中,就像它們是元素父元素的直接子元素一樣,我們使用 contents 值。它對於刪除不必要的包裝器元素並保持其子元素可見很有用。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.wrapper {
display: contents;
}
.child {
text-align: center;
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 5px;
}
.parent {
display: block;
background-color: #f0f0f0;
padding: 10px;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: contents
</h4>
<div class="parent">
<div class="wrapper">
<div class="child">
Child 1
</div>
<div class="child">
Child 2
</div>
<div class="child">
Child 3
</div>
</div>
</div>
</body>
</html>
使用 Flex 值的 Display 屬性
要將元素設定為彈性容器,使其子元素(彈性專案)以靈活且響應的方式佈局,我們使用 flex 值。容器使用 Flexbox 佈局模型,該模型允許沿單個軸輕鬆對齊、分佈和排序專案。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
background-color: #f0f0f0;
padding: 10px;
}
.flex-item {
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 5px;
flex: 1;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: flex
</h4>
<div class="flex-container">
<div class="flex-item">
Item 1
</div>
<div class="flex-item">
Item 2
</div>
<div class="flex-item">
Item 3
</div>
</div>
</body>
</html>
使用 Grid 值的 Display 屬性
要將元素設定為網格容器,該容器使用網格佈局模型,允許建立具有行和列的二維佈局,我們使用 grid 值。網格專案可以顯式或自動放置和調整大小跨網格。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
background-color: #f0f0f0;
padding: 10px;
gap: 10px;
}
.grid-item {
background-color: #4CAF50;
color: white;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: grid
</h4>
<div class="grid-container">
<div class="grid-item">
Item 1
</div>
<div class="grid-item">
Item 2
</div>
<div class="grid-item">
Item 3
</div>
</div>
</body>
</html>
使用 Inline Block 值的 Display 屬性
要使元素表現得像內聯級元素(允許它與文字和其他內聯內容一起流動),同時保留塊級屬性(如寬度和高度),我們使用 inline-block 值。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.inline-block-item {
text-align: center;
display: inline-block;
width: 200px;
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 5px;
}
.container {
border: 3px solid #ccc;
padding: 15px;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: inline-block
</h4>
<div class="container">
<span class="inline-block-item">
Item 1
</span>
<span class="inline-block-item">
Item 2
</span>
<span class="inline-block-item">
Item 3
</span>
</div>
</body>
</html>
使用 Inline Flex 屬性的 Display 屬性
要將元素設定為內聯級彈性容器,使容器表現得像內聯元素一樣,與周圍的文字或內聯元素一起流動,同時仍然對子元素應用 Flexbox 佈局規則,我們使用 inline-flex 屬性。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.inline-flex-container {
display: inline-flex;
background-color: #f0f0f0;
padding: 10px;
}
.flex-item {
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 5px;
flex: 1;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: inline-flex
</h4>
<div class="inline-flex-container">
<div class="flex-item">
Item 1
</div>
<div class="flex-item">
Item 2
</div>
<div class="flex-item">
Item 3
</div>
</div>
</body>
</html>
使用 Inline Grid 值的 Display 屬性
要將元素設定為內聯級網格容器,使其表現得像內聯元素一樣(與文字和其他內聯內容一起流動),同時使用網格佈局模型來排列其子元素,我們使用 inline-grid 值。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: inline-grid;
background-color: #f0f0f0;
padding: 10px;
gap: 10px;
}
.inline-grid-item {
background-color: #4CAF50;
color: white;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: inline-grid
</h4>
<div class="grid-container">
<div class="inline-grid-item">
Item 1
</div>
<div class="inline-grid-item">
Item 2
</div>
<div class="inline-grid-item">
Item 3
</div>
</div>
</body>
</html>
使用 Run In 值的 Display 屬性
要使元素根據上下文表現得像塊級元素或內聯級元素,我們使用 run-in 值。它旨在允許元素“執行到”周圍的文字或其他元素中。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.block-container {
display: block;
background-color: #f0f0f0;
padding: 10px;
}
.run-in {
display: run-in;
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 5px;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: run-in
</h4>
<div class="block-container">
<div class="run-in">
Run-In Element
</div>
<p>
This paragraph follows the run-in element. Depending on
the browser support, the run-in element might appear
as a block or inline element here.
</p>
</div>
</body>
</html>
使用 List Item 值的 Display 屬性
要使元素表現得像列表項一樣,以便它顯示列表標記(專案符號或數字)並使用縮排進行格式化,類似於專案在無序 <ul> 或有序列表 <ol> 中的顯示方式,我們使用 list-item 值。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.list-item {
display: list-item;
background-color: #4CAF50;
padding: 10px;
margin: 10px;
text-align: center;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h3>
display: list item
</h3>
<div class="list-item">
Item 1
</div>
<div class="list-item">
Item 2
</div>
<div class="list-item">
Item 3
</div>
</body>
</html>
使用 Table 值的 Display 屬性
要使用 CSS 建立類似表格的佈局,而不使用 HTML 表格元素,我們可以對錶格使用不同的顯示方式。在以下示例中,一些值 table、table-row、table-cell 和 table-caption 已被使用。
- table: 建立一個像 <table> 一樣的容器,
- table-cell: 像 <td> 單元格一樣設定元素的樣式,
- table-row: 將元素定義為像 <tr> 一樣的行,
- table-caption: 像 <caption> 元素一樣工作,為表格定位標題。
示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #4CAF50;
color: white;
display: flex;
border: 1px solid black;
}
.table {
display: table;
}
.row {
display: table-row;
padding: 3px;
}
.cell {
display: table-cell;
padding: 3px;
}
.caption {
display: table-caption;
text-align: center;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>
display: table, table-row, table-cell, table-caption
</h4>
<div class="table">
<div class="caption">
Sample Table
</div>
<div class="row">
<div class="cell">
Row1-Cell1
</div>
<div class="cell">
Row1-Cell2
</div>
<div class="cell">
Row1-Cell3
</div>
</div>
<div class="row">
<div class="cell">
Row2-Cell1
</div>
<div class="cell">
Row2-Cell2
</div>
<div class="cell">
Row2-Cell3
</div>
</div>
<div class="row">
<div class="cell">
Row3-Cell1
</div>
<div class="cell">
Row3-Cell2
</div>
<div class="cell">
Row3-Cell3
</div>
</div>
</div>
</body>
</html>
使用 None 值的 Display 屬性
要完全隱藏元素,使其從佈局和頁面中消失,將其從文件流中移除,使其不佔用任何空間,並且不可見或不可互動,我們使用 none 值。以下示例中顯示了這一點。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.hidden {
display: none;
}
.visible {
background-color: #4CAF50;
color: white;
padding: 10px;
margin: 10px;
}
</style>
</head>
<body>
<h2>
CSS display property
</h2>
<h4>display: none</h4>
<div class="visible">
This is visible
</div>
<div class="hidden">
This is hidden
</div>
<div class="visible">
This is also visible
</div>
</body>
</html>
支援的瀏覽器
| 屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| display | 4.0 | 8.5 | 3.0 | 3.1 | 7.0 |




