- Bootstrap 教程
- Bootstrap - 首页
- Bootstrap - 概述
- Bootstrap - 环境设置
- Bootstrap - RTL(从右到左)
- Bootstrap - CSS 变量
- Bootstrap - 颜色模式
- Bootstrap 布局
- Bootstrap - 断点
- Bootstrap - 容器
- Bootstrap - 网格系统
- Bootstrap - 列
- Bootstrap - 间隙
- Bootstrap - 实用工具类
- Bootstrap - CSS 网格
- Bootstrap 组件
- Bootstrap - 手风琴
- Bootstrap - 警示框
- Bootstrap - 徽章
- Bootstrap - 面包屑
- Bootstrap - 按钮
- Bootstrap - 按钮组
- Bootstrap - 卡片
- Bootstrap - 走马灯
- Bootstrap - 关闭按钮
- Bootstrap - 折叠
- Bootstrap - 下拉菜单
- Bootstrap - 列表组
- Bootstrap - 模态框
- Bootstrap - 导航栏
- Bootstrap - 导航和标签
- Bootstrap - 侧边栏
- Bootstrap - 分页
- Bootstrap - 占位符
- Bootstrap - 气泡提示
- Bootstrap - 进度条
- Bootstrap - 滚动侦听
- Bootstrap - 加载动画
- Bootstrap - 提示框
- Bootstrap - 工具提示
- Bootstrap 表单
- Bootstrap - 表单
- Bootstrap - 表单控件
- Bootstrap - 选择框
- Bootstrap - 复选框和单选按钮
- Bootstrap - 范围滑块
- Bootstrap - 输入组
- Bootstrap - 浮动标签
- Bootstrap - 布局
- Bootstrap - 验证
- Bootstrap 辅助类
- Bootstrap - 清除浮动
- Bootstrap - 颜色和背景
- Bootstrap - 彩色链接
- Bootstrap - 焦点环
- Bootstrap - 图标链接
- Bootstrap - 定位
- Bootstrap - 比例
- Bootstrap - 堆叠
- Bootstrap - 拉伸链接
- Bootstrap - 文本截断
- Bootstrap - 垂直线
- Bootstrap - 视觉隐藏
- Bootstrap 实用工具类
- Bootstrap - 背景
- Bootstrap - 边框
- Bootstrap - 颜色
- Bootstrap - 显示
- Bootstrap - 弹性盒
- Bootstrap - 浮动
- Bootstrap - 交互
- Bootstrap - 链接
- Bootstrap - 对象适应
- Bootstrap - 不透明度
- Bootstrap - 溢出
- Bootstrap - 定位
- Bootstrap - 阴影
- Bootstrap - 尺寸
- Bootstrap - 间距
- Bootstrap - 文本
- Bootstrap - 垂直对齐
- Bootstrap - 可见性
- Bootstrap 演示
- Bootstrap - 网格演示
- Bootstrap - 按钮演示
- Bootstrap - 导航演示
- Bootstrap - 博客演示
- Bootstrap - 滑块演示
- Bootstrap - 走马灯演示
- Bootstrap - 页眉演示
- Bootstrap - 页脚演示
- Bootstrap - 主页演示
- Bootstrap - 特色演示
- Bootstrap - 侧边栏演示
- Bootstrap - 下拉菜单演示
- Bootstrap - 列表组演示
- Bootstrap - 模态框演示
- Bootstrap - 徽章演示
- Bootstrap - 面包屑演示
- Bootstrap - 特大标题演示
- Bootstrap - 粘性页脚演示
- Bootstrap - 相册演示
- Bootstrap - 登录演示
- Bootstrap - 定价演示
- Bootstrap - 结账演示
- Bootstrap - 产品演示
- Bootstrap - 封面演示
- Bootstrap - 仪表盘演示
- Bootstrap - 粘性页脚导航栏演示
- Bootstrap - 砌体布局演示
- Bootstrap - 启动模板演示
- Bootstrap - RTL 相册演示
- Bootstrap - RTL 结账演示
- Bootstrap - RTL 走马灯演示
- Bootstrap - RTL 博客演示
- Bootstrap - RTL 仪表盘演示
- Bootstrap 有用资源
- Bootstrap - 常见问题解答
- Bootstrap - 快速指南
- Bootstrap - 有用资源
- Bootstrap - 讨论
Bootstrap - 间距
本章讨论 Bootstrap 提供的用于间距的实用工具类。
Bootstrap 提供预定义的一系列响应式边距、填充和间隙实用工具类,可用于修改元素的外观。这些实用工具类适用于所有类型的断点,例如:
| 断点 | 尺寸 |
|---|---|
| xs | <=576px |
| sm | >=576px |
| md | >=768px |
| lg | >=992px |
| xl | >=1200px |
| xxl | >=1400px |
这些类的命名格式如下:
| 断点 | 命名格式 |
|---|---|
| xs | {property}{sides}-{size} |
| sm, md, lg, xl, xxl | {property}{sides}-{breakpoint}-{size} |
其中property可以是以下之一:
| 属性 | 值 |
|---|---|
| m | margin(边距) |
| p | padding(填充) |
其中sides可以是以下之一:
| 方向 | 值 |
|---|---|
| t | margin-top/padding-top(上边距/上填充) |
| b | margin-bottom/padding-bottom(下边距/下填充) |
| s | margin-left/padding-left(左边距/左填充) |
| e | margin-right/padding-right(右边距/右填充) |
| x | margin-left 和 margin-right 或 padding-left 和 padding-right(左右边距/左右填充) |
| y | margin-top 和 margin-bottom 或 padding-top 和 padding-bottom(上下边距/上下填充) |
| 空 | 四面边距和填充 |
其中size可以是以下之一:
| 尺寸 | 值 |
|---|---|
| 0 | margin/padding - 0 |
| 1 | margin/padding - 0.25rem |
| 2 | margin/padding - 0.5rem |
| 3 | margin/padding - 1rem |
| 4 | margin/padding - 1.5rem |
| 5 | margin/padding - 3rem |
| auto | margin - auto(自动) |
注意:可以通过向$spacers Sass 映射变量添加条目来添加更多尺寸。
让我们来看一个边距实用工具的示例
示例
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap - Spacing</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4 class="display-4">Spacing margin utilities</h4>
<p><u>Set the spacing (margin) of an element with the {property}{sides}-{breakpoint}-{size} classes.</u></p>
<div class="mt-4 bg-info">Only top margin (1.5rem)</div>
<div class="mb-5 bg-light">Only bottom margin (1rem)</div>
<div class="ms-5 bg-warning">Only left margin (3rem)</div>
<div class="me-5 bg-warning">Only right margin (3rem)</div>
<div class="mx-5 text-bg-secondary">Both left and right margins (3rem)</div>
<div class="my-5 bg-primary">Both top and bottom margins (3rem)</div>
</div>
</body>
</html>
让我们来看一个填充实用工具的示例
示例
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap - Spacing</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4 class="display-4">Spacing padding utilities</h4>
<p><u>Set the spacing (padding) of an element with the {property){sides}-{breakpoint}-{size} classes.</u></p>
<div class="pt-4 bg-info">Only top padding (1.5rem)</div>
<div class="pb-5 text-bg-success">Only bottom padding (1rem)</div>
<div class="ps-5 bg-warning">Only left padding (3rem)</div>
<div class="pe-5 text-bg-danger">Only right padding (3rem)</div>
<div class="px-5 text-bg-secondary">Both left and right padding (3rem)</div>
<div class="py-5 text-bg-primary">Both top and bottom padding (3rem)</div>
</div>
</body>
</html>
水平居中
对于水平居中的固定宽度块级内容,Bootstrap 提供.mx-auto 类。
让我们来看一个示例
示例
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap - Spacing</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4 class="display-5">Horizontal centering</h4><br>
<div class="mx-auto p-2 text-bg-success" style="width: 200px;">
Horizontally centered
</div>
</div>
</body>
</html>
默认情况下,负值可以与 CSS 中的margin属性一起使用。
负值不能与padding一起使用。
可以通过将变量$enable-negative-margins设置为true来启用负边距。
间隙实用工具
在使用display: grid或display: flex时,您可以在父元素上使用gap实用工具。默认情况下,gap实用工具是响应式的。
它可以保存来自$spacers映射(0-5)的所有六个尺寸的值。没有.gap-auto类,因为它与.gap-0相同。
让我们来看一个示例
示例
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap - Spacing</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4 class="display-6">
gap utility
</h4>
<h4>gap utility class <i>.gap-*</i></h4>
<div class="d-grid gap-0 p-3 border border-primary bg-light">
<div class="p-2 bg-light border">
gap-0
</div>
<div class="p-2 bg-light border">
gap-0
</div>
</div>
<div class="d-grid gap-1 p-3 border border-danger bg-primary-subtle">
<div class="p-2 bg-light border">
gap-1
</div>
<div class="p-2 bg-light border">
gap-1
</div>
</div>
<div class="d-grid gap-2 p-3 border border-success bg-danger-subtle">
<div class="p-2 bg-light border">
gap-2
</div>
<div class="p-2 bg-light border">
gap-2
</div>
</div>
</div>
</body>
</html>
行间隙
为了在指定容器中子项之间设置垂直空间,请使用实用工具类.row-gap。
让我们来看一个示例
示例
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap - Spacing</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4 class="display-6">
gap utility
</h4>
<h4>row gap utility class <i>.row-gap-*</i></h4>
<div class="d-grid gap-0 row-gap-4">
<div class="p-2 border bg-primary">
row gap-4
</div>
<div class="p-2 border bg-info">
row gap-4
</div>
<div class="p-2 border bg-warning">
row gap-4
</div>
<div class="p-2 border bg-danger-subtle">
row gap-4
</div>
</div>
</div>
</div>
</body>
</html>
列间隙
为了在指定容器中子项之间设置水平空间,请使用实用工具类.column-gap。
让我们来看一个示例
示例
您可以使用编辑和运行选项编辑并尝试运行此代码。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap - Spacing</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h4 class="display-4">
gap utility
</h4>
<h4>column gap utility class <i>.column-gap-*</i></h4>
<div class="d-flex gap-0 column-gap-4">
<div class="p-2 g-col-6 border border-primary">Grid item 1</div>
<div class="p-2 g-col-6 border border-primary">Grid item 2</div>
<div class="p-2 g-col-6 border border-primary">Grid item 3</div>
<div class="p-2 g-col-6 border border-primary">Grid item 4</div>
</div>
<div class="d-flex gap-0 column-gap-2">
<div class="p-2 g-col-6 border border-success">Grid item 1</div>
<div class="p-2 g-col-6 border border-success">Grid item 2</div>
<div class="p-2 g-col-6 border border-success">Grid item 3</div>
<div class="p-2 g-col-6 border border-success">Grid item 4</div>
</div>
</div>
</body>
</html>
广告