- 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 - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- 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 - margin-inline 屬性
CSS 的 margin-inline 屬性設定元素的 inline-start 和 inline-end 邊緣的外邊距。它是一個簡寫屬性,用於在一個宣告中定義以下屬性的值:margin-inline-start 和 margin-inline-end。 writing-mode 和 direction 屬性定義內聯方向。
語法
margin-inline: auto | length | percentage | initial | inherit;
屬性值
| 值 | 描述 |
|---|---|
| auto | 瀏覽器自動設定 inline-start 和 inline-end 邊緣的外邊距。預設值。 |
| length | 使用長度單位(例如 px、em、rem 等)設定 inline-start 和 inline-end 邊緣的外邊距。允許使用負值。 |
| percentage | 使用百分比值(例如 10%)設定 inline-start 和 inline-end 邊緣的外邊距,相對於包含元素的內聯方向。 |
| initial | 將屬性設定為其預設值。 |
| inherit | 從父元素繼承屬性。 |
CSS Margin Inline 屬性示例
以下示例說明了使用不同值的 margin-inline 屬性。
使用 Auto 值的 Margin Inline 屬性
要允許瀏覽器根據可用空間自動計算元素的 inline-start 和 inline-end 邊緣的外邊距,我們使用 auto 值。這在以下示例中顯示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
height: 200px;
padding: 20px;
}
.container>div {
width: 150px;
height: 200px;
float: left;
box-sizing: border-box;
}
.props {
text-align: center;
padding: 10px;
}
.auto-box {
background-color: lightblue;
border: 3px solid blue;
margin-inline: auto;
}
.demo-box {
background-color: #04bf52;
}
</style>
</head>
<body>
<h2>
CSS margin-inline property
</h2>
<h4>
margin-inline: auto
</h4>
<div class="container">
<div class="demo-box props">
sample box
</div>
<div class="auto-box props">
This box has margin-inline: auto
</div>
<div class="demo-box props">
sample box
</div>
</div>
</body>
</html>
使用 Length 值的 Margin Inline 屬性
要設定元素的 inline-start 和 inline-end 邊緣的外邊距,我們可以使用長度單位(例如 px、em、rem 等)指定外邊距大小。它最多接受兩個值。根據指定的值的數量,外邊距大小將應用於邊緣。這在以下示例中顯示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
height: 200px;
width: 490px;
padding: 20px;
}
.container>div {
width: 150px;
height: 200px;
float: left;
box-sizing: border-box;
}
.props {
text-align: center;
padding: 10px;
}
.center {
background-color: lightblue;
border: 3px solid blue;
}
.px-box {
margin-inline: 10px;
}
.em-box {
margin-inline: 10px 30px;
}
.demo-box {
background-color: #04bf52;
}
</style>
</head>
<body>
<h2>
CSS margin-inline property
</h2>
<h4>
margin-inline: 10px
</h4>
<div class="container">
<div class="demo-box props">
sample box
</div>
<div class="px-box props center">
This box has margin-inline: 10px
(for both inline-start and inline-end edges)
</div>
<div class="demo-box props">
sample box
</div>
</div>
<h4>
margin-inline: 10px 30px
</h4>
<div class="container">
<div class="demo-box props">
sample box
</div>
<div class="em-box props center">
This box has margin-inline: 10px 30px
(for inline-start edge- 10px, for
inline-end edge-30px)
</div>
<div class="demo-box props">
sample box
</div>
</div>
</body>
</html>
使用 Percentage 值的 Margin Inline 屬性
要設定元素的 inline-start 和 inline-end 邊緣的外邊距,我們可以使用百分比值(例如 10%(包含元素寬度的 10%))指定外邊距大小。它最多接受兩個值。根據指定的值的數量,外邊距大小將應用於邊緣。這在以下示例中顯示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
height: 200px;
width: 490px;
padding: 20px;
}
.container>div {
width: 150px;
height: 200px;
float: left;
box-sizing: border-box;
}
.props {
text-align: center;
padding: 10px;
}
.center {
background-color: lightblue;
border: 3px solid blue;
}
.example1 {
margin-inline: 3%;
}
.example2 {
margin-inline: 2% 3%;
}
.demo-box {
background-color: #04bf52;
}
</style>
</head>
<body>
<h2>
CSS margin-inline property
</h2>
<h4>
margin-inline: 3%
</h4>
<div class="container">
<div class="demo-box props">
sample box
</div>
<div class="example1 props center">
This box has margin-inline: 3% for
both the inline-start and inline-end
edges of the width of the container.
</div>
<div class="demo-box props">
sample box
</div>
</div>
<h4>
margin-inline: 2% 3%
</h4>
<div class="container">
<div class="demo-box props">
sample box
</div>
<div class="example2 props center">
This box has margin-inline: 2% for
inline-start edge and 3% for inline-end
edge of the width of the container.
</div>
<div class="demo-box props">
sample box
</div>
</div>
</body>
</html>
帶有方向的 Margin Inline 屬性
margin-inline 屬性可以與 direction 屬性結合使用,該屬性確定文字和內聯元素的水平方向。如果 direction 為 ltr,則 inline-start 在左側,inline-end 在右側。如果 direction 為 rtl,則 inline-start 在右側,inline-end 在左側。這些在以下示例中顯示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
height: 200px;
width: 490px;
padding: 20px;
}
.container>div {
width: 150px;
height: 200px;
float: left;
box-sizing: border-box;
}
.props {
text-align: center;
padding: 10px;
}
.center {
background-color: lightblue;
border: 3px solid blue;
margin-inline: 5px 12px;
}
.demo-box {
background-color: #04bf52;
}
.direction-rtl {
direction: rtl;
}
.direction-ltr {
direction: ltr;
}
</style>
</head>
<body>
<h2>
CSS margin-inline property
</h2>
<h4>
margin-inline: 5px 10px; direction: rtl
</h4>
<div class="container">
<div class="demo-box props">
sample box
</div>
<div class="direction-rtl props center">
This box has margin-inline: 5px for
inline-start edge and 12px for inline-end
edge with rtl direction.
</div>
<div class="demo-box props">
sample box
</div>
</div>
<h4>
margin-inline: 5px 10px; direction: ltr
</h4>
<div class="container">
<div class="demo-box props">
sample box
</div>
<div class="direction-ltr props center">
This box has margin-inline: 5px for
inline-start edge and 12px for inline-end
edge with ltr direction.
</div>
<div class="demo-box props"> sample box</div>
</div>
</body>
</html>
帶有書寫模式的 Margin Inline 屬性
margin-inline 屬性可以與 writing-mode 屬性結合使用,該屬性確定文件中文字流的方向和方向。例如,在垂直模式(如 vertical-rl)中,inline-start 為頂部,inline-end 為底部。這在以下示例中顯示。
示例
<!DOCTYPE html>
<html>
<head>
<style>
.container {
height: 50%;
width: 50%;
}
.container > div {
writing-mode: vertical-rl;
block-size: 100%;
box-sizing: border-box;
}
.demo-box {
background-color: #04bf52;
inline-size: 20%;
}
.main-box {
background-color: lightblue;
inline-size: 50%;
border: solid black 1px;
margin-inline: 10px 30px;
}
</style>
</head>
<body>
<h2>
CSS margin-inline property
</h2>
<h4>
margin-inline: 10px 30px;
writing-mode: vertical-rl
</h4>
<div class="container">
<div class="demo-box">
sample box
</div>
<div class="main-box">
<p>
This box has margin-inline: 10px for
inline-start and 30px for inline-end edges
with writing-mode: vertical-rl;
</p>
</div>
<div class="demo-box">
sample box
</div>
</div>
</body>
</html>
支援的瀏覽器
| 屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| margin-inline | 87.0 | 87.0 | 66.0 | 14.1 | 73.0 |




