bootstrap-v6-install
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdd Bootstrap 6 to a Project
将 Bootstrap 6 添加到项目中
This skill sets up Bootstrap 6 in a user's project. Pick the path that matches the project: CDN for the fastest start with no build step, or package manager (npm) for customizing Sass and bundling JS. For a full bundler-specific setup, use the dedicated , , , or skills instead.
bootstrap-npmbootstrap-vitebootstrap-webpackbootstrap-parcel本技能用于在用户项目中配置 Bootstrap 6。请选择符合项目需求的安装方式:CDN 方式无需构建步骤,可快速上手;包管理器(npm) 方式支持自定义 Sass 和 JS 打包。如果需要针对特定打包工具的完整配置,请改用专门的 、、 或 技能。
bootstrap-npmbootstrap-vitebootstrap-webpackbootstrap-parcelWorkflow
工作流程
- Step 1: Choose an install path (CDN vs. package manager)
- Step 2: Install Bootstrap and dependencies
- Step 3: Include Bootstrap's CSS
- Step 4: Include Bootstrap's JavaScript
- Step 5: Verify
- 步骤1:选择安装方式(CDN 与包管理器)
- 步骤2:安装 Bootstrap 及依赖项
- 步骤3:引入 Bootstrap 的 CSS
- 步骤4:引入 Bootstrap 的 JavaScript
- 步骤5:验证配置
Step 1: Choose an install path
步骤1:选择安装方式
- CDN — no build step. Best for static pages, demos, and prototypes. Skip to the CDN section below.
- Package manager (npm) — required to customize Bootstrap's Sass tokens or tree-shake JS. Requires Node.js and a Sass compiler. Continue with the npm section.
Bootstrap 6 depends on two optional peer packages:
- — positioning for menus, popovers, and tooltips. Omit if you don't use them.
@floating-ui/dom - — the datepicker. Omit if you don't use it.
vanilla-calendar-pro
- CDN — 无需构建步骤,最适合静态页面、演示和原型项目。请直接跳转到下方的 CDN 章节。
- 包管理器(npm) — 自定义 Bootstrap 的 Sass tokens 或对 JS 进行 tree-shaking 时必须使用此方式。需要 Node.js 和 Sass 编译器。请继续阅读 npm 章节。
Bootstrap 6 依赖两个可选的 peer 依赖包:
- — 用于菜单、popover 和 tooltip 的定位。如果不使用这些组件可省略。
@floating-ui/dom - — 日期选择器组件。如果不使用可省略。
vanilla-calendar-pro
Step 2 & 3 (CDN): Include via CDN
步骤2 & 3(CDN 方式):通过 CDN 引入
Add Bootstrap's CSS in and the JS bundle before the closing . The bundle includes Floating UI and Vanilla Calendar Pro.
<head></body>html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-B/GM4XqrwHnWXNOWMbloTmrYXZg10cakYGmpfsR/bbzQ6JAJI4ihuyADKLnBgrCe" crossorigin="anonymous">
</head>
<body>
<div class="container py-4 px-3 mx-auto">
<h1>Hello, Bootstrap!</h1>
<button class="btn-solid theme-primary">Primary button</button>
</div>
<script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-1a/pXj49ZQ1aHEmrJ+gMw1otqoVsYwlEnlD8mIfY2TV03r20Y0CN7uqx1tQogjPL" crossorigin="anonymous"></script>
</body>
</html>Copy the exact URLs and hashes from the Install docs when you use a different version. Never guess an SRI hash. The attribute is required for SRI to work.
crossorigin="anonymous"To load Floating UI and Vanilla Calendar Pro separately (smaller payload), use the non-bundle JS with an import map:
html
<script type="importmap">
{
"imports": {
"@floating-ui/dom": "https://cdn.jsdelivr.net/npm/@floating-ui/dom/+esm",
"vanilla-calendar-pro": "https://cdn.jsdelivr.net/npm/vanilla-calendar-pro/+esm"
}
}
</script>
<script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/js/bootstrap.min.js" integrity="sha384-BJf/nQgxZJU68GxBse97Vdlc8Rh+z9PzzXiS3/BjV+1nQPR7IKn+iJnD3DJvCMaU" crossorigin="anonymous"></script>For a CDN setup you're done — skip to Step 5: Verify.
将 Bootstrap 的 CSS 添加到 标签中,将 JS bundle 添加到 闭合标签之前。该 bundle 已包含 Floating UI 和 Vanilla Calendar Pro。
<head></body>html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-B/GM4XqrwHnWXNOWMbloTmrYXZg10cakYGmpfsR/bbzQ6JAJI4ihuyADKLnBgrCe" crossorigin="anonymous">
</head>
<body>
<div class="container py-4 px-3 mx-auto">
<h1>Hello, Bootstrap!</h1>
<button class="btn-solid theme-primary">Primary button</button>
</div>
<script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-1a/pXj49ZQ1aHEmrJ+gMw1otqoVsYwlEnlD8mIfY2TV03r20Y0CN7uqx1tQogjPL" crossorigin="anonymous"></script>
</body>
</html>如果使用其他版本,请从官方安装文档中复制准确的 URL 和哈希值。切勿猜测 SRI 哈希值。 属性是 SRI 正常工作的必要条件。
crossorigin="anonymous"如果想要单独加载 Floating UI 和 Vanilla Calendar Pro(减小加载体积),请使用非 bundle 版 JS 配合 import map:
html
<script type="importmap">
{
"imports": {
"@floating-ui/dom": "https://cdn.jsdelivr.net/npm/@floating-ui/dom/+esm",
"vanilla-calendar-pro": "https://cdn.jsdelivr.net/npm/vanilla-calendar-pro/+esm"
}
}
</script>
<script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/js/bootstrap.min.js" integrity="sha384-BJf/nQgxZJU68GxBse97Vdlc8Rh+z9PzzXiS3/BjV+1nQPR7IKn+iJnD3DJvCMaU" crossorigin="anonymous"></script>CDN 方式的配置到此完成 —— 请跳转到 步骤5:验证配置。
Step 2 (npm): Install
步骤2(npm 方式):安装
sh
npm install bootstrap@6.0.0-alpha.1 @floating-ui/dom vanilla-calendar-proBootstrap 6 requires a Sass compiler (Dart Sass) and PostCSS/Autoprefixer to match the official compiled output. Node Sass is not supported. Install the build tooling your setup needs, e.g.:
sh
npm install --save-dev sass autoprefixer postcss postcss-clish
npm install bootstrap@6.0.0-alpha.1 @floating-ui/dom vanilla-calendar-proBootstrap 6 需要 Sass 编译器(Dart Sass) 和 PostCSS/Autoprefixer 才能生成与官方一致的编译产物。不支持 Node Sass。请根据你的配置安装所需的构建工具,例如:
sh
npm install --save-dev sass autoprefixer postcss postcss-cliStep 3 (npm): Include Bootstrap's CSS
步骤3(npm 方式):引入 Bootstrap 的 CSS
Import Bootstrap's source Sass with the modern rule (never — it's removed in Dart Sass and unsupported in v6):
@use@importscss
// Import all of Bootstrap's CSS
@use "bootstrap/scss/bootstrap";Customize tokens with the syntax. Only include the keys you want to change — Bootstrap merges overrides with its defaults:
with ()scss
@use "bootstrap/scss/bootstrap" with (
$root-tokens: (
--border-radius: .25rem,
--spacer: 1.5rem,
)
);Override CSS custom properties through the token maps ( and each component's map), following Customize › Sass. Token names are written unprefixed in Sass (e.g. , ); Bootstrap's dist/CDN CSS runs PostCSS to add the prefix, so compiling the source yourself keeps them unprefixed.
$root-tokens$*-tokens--border-radius--spacer--bs-Compile the Sass to CSS with a so resolves, then run Autoprefixer via PostCSS.
--load-path=node_modulesbootstrap使用现代的 规则引入 Bootstrap 的 Sass 源码(切勿使用 —— Dart Sass 已移除该语法,Bootstrap v6 也不再支持):
@use@importscss
// Import all of Bootstrap's CSS
@use "bootstrap/scss/bootstrap";使用 语法自定义 tokens。仅需填写你想要修改的键 —— Bootstrap 会将你的覆盖配置与默认值合并:
with ()scss
@use "bootstrap/scss/bootstrap" with (
$root-tokens: (
--border-radius: .25rem,
--spacer: 1.5rem,
)
);通过 token 映射( 以及各组件的 映射)覆盖 CSS 自定义属性,可参考 自定义 › Sass。在 Sass 中,token 名称不带前缀(例如 、);Bootstrap 的 dist/CDN 版 CSS 会通过 PostCSS 添加 前缀,因此自行编译源码时不会带有此前缀。
$root-tokens$*-tokens--border-radius--spacer--bs-使用 参数将 Sass 编译为 CSS,以正确解析 路径,然后通过 PostCSS 运行 Autoprefixer。
--load-path=node_modulesbootstrapStep 4 (npm): Include Bootstrap's JavaScript
步骤4(npm 方式):引入 Bootstrap 的 JavaScript
Bootstrap 6 JavaScript is ESM-only. Import all plugins onto a object:
bootstrapjs
import * as bootstrap from 'bootstrap'Or import only what you need for automatic tree-shaking:
js
import { Tooltip, Toast, Popover } from 'bootstrap'You can also import a single plugin directly from its file under .
bootstrap/js/dist/Floating UI and Vanilla Calendar Pro are imported automatically through Bootstrap when a bundler is used.
Bootstrap 6 的 JavaScript 仅支持 ESM 格式。你可以将所有插件导入到 对象中:
bootstrapjs
import * as bootstrap from 'bootstrap'或者仅导入你需要的模块,以自动启用 tree-shaking:
js
import { Tooltip, Toast, Popover } from 'bootstrap'你也可以直接从 目录下的对应文件中导入单个插件。
bootstrap/js/dist/使用打包工具时,Floating UI 和 Vanilla Calendar Pro 会通过 Bootstrap 自动导入。
Step 5: Verify
步骤5:验证配置
- Load the page (or run the dev server) and confirm the sample button and
.btn-solid.theme-primaryare styled — that proves Bootstrap's CSS is loaded..container - Trigger a JS component (e.g. a tooltip or dialog) to confirm the JavaScript loaded without console errors.
- For CDN setups, confirm the hashes match the version you linked (mismatched hashes block the file from loading).
integrity - If Sass fails to compile, confirm you used (not
@use) and passed@import.--load-path=node_modules - Bootstrap 6 requires a browser that supports and
oklch().color-mix()
- 加载页面(或运行开发服务器),确认示例中的 按钮和
.btn-solid.theme-primary已应用样式 —— 这证明 Bootstrap 的 CSS 已成功加载。.container - 触发一个 JS 组件(例如 tooltip 或对话框),确认 JavaScript 加载正常,控制台无报错。
- 对于 CDN 方式的配置,请确认 哈希值与你引用的版本匹配(哈希不匹配会阻止文件加载)。
integrity - 如果 Sass 编译失败,请确认你使用的是 (而非
@use),并且添加了@import参数。--load-path=node_modules - Bootstrap 6 需要浏览器支持 和
oklch()函数。color-mix()