- 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 - 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 属性
CSS margin 属性用于在元素周围创建空间,位于其边框之外。它是一个简写属性,用于在一个语句中定义以下属性的值:margin-top,margin-right,margin-bottom 和 margin-left。
语法
margin: auto | length | percentage | initial | inherit;
属性值
值 | 描述 |
---|---|
auto | 浏览器会自动设置所有四个边框的边距。 |
长度 | 使用长度单位(例如 px、em、rem 等)设置边距的宽度。 |
百分比 | 使用百分比值(例如 10%)相对于包含元素设置边距的宽度。 |
initial | 它将属性设置为其默认值。 |
inherit | 它从父元素继承属性。 |
CSS Margin 属性示例
以下示例说明了具有不同值的margin 属性。
使用 Auto 值的 Margin 属性
要允许浏览器自动计算边框的边距,我们使用auto 值。这在以下示例中显示。
示例
<!DOCTYPE html> <html> <head> <style> p { background-color: lightblue; padding: 10px; margin: auto; } </style> </head> <body> <h2> CSS margin property </h2> <h4> margin: auto </h4> <p> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> </body> </html>
使用长度值的 Margin 属性
要设置边框的边距,我们可以使用长度单位(例如 px、em、rem 等)指定边距的宽度。它最多接受四个值。根据提供的数值数量,相应的边框将受到影响。这在以下示例中显示。
示例
<!DOCTYPE html> <html> <head> <style> .container { background-color: #f2f2f2; padding: 10px; } p { background-color: lightblue; padding: 10px; } .one { margin: 10px; } .two { margin: 10px 25px; } .three { margin: 10px 15px 30px; } .four { margin: 10px 15px 30px 45px; } </style> </head> <body> <h2> CSS margin property </h2> <div class="container"> <h4> margin: 10px (applied to all four borders) </h4> <p class="one"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> <h4> margin: 10px 25px (10px applied to top and bottom borders, 25px applied to left and right borders) </h4> <p class="two"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> <h4> margin: 10px 15px 30px (10px applied to top border, 15px applied to left and right borders, 30px applied to bottom border) </h4> <p class="three"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> <h4> margin: 10px 15px 30px 45px (10px applied to top border, 15px applied to right border, 45px applied to bottom border and 45px applied to left border) </h4> <p class="four"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> </div> </body> </html>
使用百分比值的 Margin 属性
要设置边框的边距,我们可以使用百分比值(例如,相对于包含元素大小的 10%)指定边距的宽度。它最多接受四个值。根据提供的数值数量,相应的边框将受到影响。这在以下示例中显示。
示例
<!DOCTYPE html> <html> <head> <style> .container { background-color: #f2f2f2; padding: 10px; } p { background-color: lightgreen; padding: 10px; } .one { margin: 6%; } .two { margin: 6% 4%; } .three { margin: 6% 8% 5%; } .four { margin: 6% 5% 7% 8%; } </style> </head> <body> <h2> CSS margin property </h2> <div class="container"> <h4> margin: 6% (applied to all four borders) </h4> <p class="one"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> <h4> margin: 6% 4% (6% applied to top and bottom borders, 4% applied to left and right borders) </h4> <p class="two"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> <h4> margin: 6% 8% 5% (6% applied to top border, 8% applied to left and right borders, 5% applied to bottom border) </h4> <p class="three"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> <h4> margin: 6% 5% 7% 8% (6% applied to top border, 5% applied to right border, 7% applied to bottom border and 8% applied to left border) </h4> <p class="four"> TutorialsPoint is an online educational platform offering a wide range of resources and tutorials for learners at various levels. It provides comprehensive, self-paced learning materials across numerous subjects, including programming, web development, data science, and many other technical and non-technical fields. </p> </div> </body> </html>
注意:margin 属性最多接受四个值,因此根据指定的值的数量,相应的边框将受到影响。
- 一个值:如果给出一个值,则相同的边距宽度将应用于所有四个边框。
- 两个值:如果给出两个值,则第一个值设置顶部和底部边距宽度,第二个值设置左侧和右侧边距宽度。
- 三个值:如果给出三个值,则第一个值设置顶部边距宽度,第二个值设置右侧和左侧边距宽度,第三个值设置底部边距宽度。
- 四个值:如果给出四个值,则第一个值设置顶部边距宽度,第二个值设置右侧边距宽度,第三个值设置底部边距宽度,第四个值设置左侧边距宽度。
支持的浏览器
属性 | |||||
---|---|---|---|---|---|
margin | 1.0 | 6.0 | 1.0 | 1.0 | 3.5 |
css_properties_reference.htm
广告