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




