bootstrap-v4-v6-migration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBootstrap v4 to v6 Migration
Bootstrap v4 至 v6 迁移
There is no direct v4 to v6 path. Migrate in two stages — v4 to v5 first (this skill), then v5 to v6 (the skill). Skipping the middle stage doesn't work, because many v4 names were renamed in v5 and renamed again in v6: becomes only after passing through v5, and becomes by way of .
bootstrap-v5-v6-migration.text-primary.fg-primary.badge-primary.badge.theme-primary.badge.bg-primary不存在从v4直接升级到v6的路径。需要分两个阶段迁移——先从v4升级到v5(即本技能),再从v5升级到v6( 技能)。跳过中间阶段是行不通的,因为很多v4的命名在v5中被重命名,到v6又会再次被重命名: 要先经过v5的过渡才会变成 ,而 则要经由 最终变为 。
bootstrap-v5-v6-migration.text-primary.fg-primary.badge-primary.badge.bg-primary.badge.theme-primaryWorkflow
工作流程
Work through each step in order. After each step, search the codebase for remaining v4 patterns before moving on.
- Step 1: Plan the two-stage migration
- Step 2: v4 to v5 — dependencies, build, and Sass
- Step 3: v4 to v5 — JavaScript
- Step 4: v4 to v5 — classes and attributes
- Step 5: v5 to v6 — hand off to the v6 migration skill
- Step 6: Verify
请按顺序完成每个步骤。每完成一步后,先在代码库中搜索残留的v4模式,再继续下一步。
- 步骤1:规划两阶段迁移
- 步骤2:v4 到 v5 —— 依赖、构建与Sass
- 步骤3:v4 到 v5 —— JavaScript
- 步骤4:v4 到 v5 —— 类名与属性
- 步骤5:v5 到 v6 —— 移交至v6迁移技能
- 步骤6:验证
Step 1: Plan the two-stage migration
步骤1:规划两阶段迁移
- Get the project onto Bootstrap 5 and working — build passing, pages rendering, tests green — before touching v6. Landing the v5 stage as its own commit (or PR) keeps the two rename waves separable when something breaks.
- Inventory what you're dealing with first. The v4 features that cost the most time are custom forms, /
.input-group-append, jumbotrons,.input-group-prependobjects,.media/.card-deck, and any jQuery that calls Bootstrap plugins..card-columns - Decide about jQuery separately. Bootstrap 5 dropped it, but your app may use it for other things; removing Bootstrap's dependency on it does not require removing jQuery itself.
- Note the browser floor. Bootstrap 5 drops IE. Bootstrap 6 requires Chrome and Edge 130, Firefox 132, and Safari 18 on macOS and iOS.
- 先让项目升级到Bootstrap 5并正常运行——构建通过、页面正常渲染、测试全部通过——再开始处理v6的升级。将v5阶段的改动作为单独的提交(或PR)提交,这样当出现问题时,可以将两次重命名的改动分开排查。
- 先清点你需要处理的内容。v4中最耗时的特性包括自定义表单、/
.input-group-append、jumbotron组件、.input-group-prepend对象、.media/.card-deck,以及所有调用Bootstrap插件的jQuery代码。.card-columns - 单独决定jQuery的去留。Bootstrap 5不再依赖jQuery,但你的应用可能还在其他地方使用它;移除Bootstrap对jQuery的依赖并不意味着必须完全移除jQuery。
- 注意浏览器最低版本要求。Bootstrap 5不再支持IE。Bootstrap 6要求Chrome和Edge版本≥130、Firefox版本≥132,macOS和iOS端的Safari版本≥18。
Step 2: v4 to v5 — dependencies, build, and Sass
步骤2:v4 到 v5 —— 依赖、构建与Sass
-
Update the dependency and drop the jQuery/Popper v1 pair:sh
npm i --save bootstrap@5 @popperjs/core npm uninstall popper.js(v1) is replaced bypopper.js(v2). Bootstrap 5's bundle includes Popper; the standalone@popperjs/coredoes not.bootstrap.js -
Switch the Sass compiler to Dart Sass (). Node Sass / LibSass is not supported. Keep using
npm i --save-dev sassfor now — v5 still uses it; the move to@importhappens in the v6 stage.@use -
Sass maps no longer merge automatically. In v4, adding one key towas enough; in v5 you must define the complete map, including the keys you're keeping.
$theme-colors -
Update renamed functions and mixins:
v4 v5 color-yiq()color-contrast(),theme-color()gray()Removed — reference /$theme-colorsdirectly$graystheme-color-level()/tint-color()shade-color()/lighten()darken()/tint-color()shade-color()media-breakpoint-down(md)— takes the breakpoint itself, not the next one downmedia-breakpoint-down(lg)media-breakpoint-between(sm, md)— same shift on the upper boundmedia-breakpoint-between(sm, lg)The breakpoint parameter change is silent: nothing errors, your media queries just apply one breakpoint off. Audit every call. -
Print styles,flags, and the utility API all changed shape in v5. If you generated custom utilities with v4 mixins, rewrite them through v5's
$enable-*map.$utilities
-
更新依赖,移除jQuery/Popper v1组合:sh
npm i --save bootstrap@5 @popperjs/core npm uninstall popper.js(v1)已被popper.js(v2)取代。Bootstrap 5的打包版本包含Popper;但独立的@popperjs/core不包含。bootstrap.js -
将Sass编译器切换为Dart Sass(执行)。不再支持Node Sass / LibSass。目前继续使用
npm i --save-dev sass——v5仍然支持该语法;迁移到@import的工作会在v6阶段进行。@use -
Sass映射(map)不再自动合并。在v4中,只需向添加一个键即可;但在v5中,你必须定义完整的映射,包括你想要保留的原有键。
$theme-colors -
更新被重命名的函数和混入(mixin):
v4 v5 color-yiq()color-contrast(),theme-color()gray()已移除——直接引用 /$theme-colors$graystheme-color-level()/tint-color()shade-color()/lighten()darken()/tint-color()shade-color()media-breakpoint-down(md)—— 参数是断点本身,而非下一级断点media-breakpoint-down(lg)media-breakpoint-between(sm, md)—— 上限参数同样偏移media-breakpoint-between(sm, lg)断点参数的变化不会报错:不会有任何错误提示,只是你的媒体查询会偏移一个断点。请逐一检查所有调用。 -
打印样式、标志位以及工具类API在v5中都有变化。如果你之前用v4的混入生成自定义工具类,请通过v5的
$enable-*映射重写。$utilities
Step 3: v4 to v5 — JavaScript
步骤3:v4 到 v5 —— JavaScript
-
Namespace every data attribute with. This is a mechanical but wide-reaching change:
bsv4 v5 data-toggledata-bs-toggledata-targetdata-bs-targetdata-dismissdata-bs-dismissdata-parentdata-bs-parentdata-ridedata-bs-ride,data-slidedata-slide-to,data-bs-slidedata-bs-slide-todata-spydata-bs-spy,data-backdropdata-keyboard,data-bs-backdropdata-bs-keyboard,data-content,data-placementdata-trigger,data-bs-content,data-bs-placementdata-bs-triggerAny option passed as an attribute follows the same rule:becomesdata-<option>.data-bs-<option> -
Replace jQuery plugin calls with constructors or static getters:js
// v4 $('#myModal').modal('show') $('#myTooltip').tooltip() // v5 const modal = new bootstrap.Modal(document.querySelector('#myModal')) modal.show() bootstrap.Tooltip.getOrCreateInstance(document.querySelector('#myTooltip')) -
Replace jQuery event handlers with native listeners. Bootstrap 5 dispatches natives, so
CustomEventstops firing:$(el).on('shown.bs.modal', …)js// v5 document.querySelector('#myModal').addEventListener('shown.bs.modal', event => { // event.relatedTarget replaces event.relatedTarget from the jQuery event }) -
Smaller API renames:
- →
_getInstance(), and prefer the newgetInstance().getOrCreateInstance() - The sanitizer option →
whiteList.allowList - Tooltip and Popover /
titlestill accept functions, butcontentis no longer the jQuery element.this
-
If you relied on jQuery'sor on
noConflict-style calls, rewrite them against the class API — there is no jQuery interface left to shim.$.fn.button('toggle')
-
为所有数据属性添加命名空间。这是一项机械但影响范围很广的改动:
bsv4 v5 data-toggledata-bs-toggledata-targetdata-bs-targetdata-dismissdata-bs-dismissdata-parentdata-bs-parentdata-ridedata-bs-ride,data-slidedata-slide-to,data-bs-slidedata-bs-slide-todata-spydata-bs-spy,data-backdropdata-keyboard,data-bs-backdropdata-bs-keyboard,data-content,data-placementdata-trigger,data-bs-content,data-bs-placementdata-bs-trigger任何作为属性传递的选项都遵循同样的规则:变为data-<option>。data-bs-<option> -
用构造函数或静态获取方法替换jQuery插件调用:js
// v4 $('#myModal').modal('show') $('#myTooltip').tooltip() // v5 const modal = new bootstrap.Modal(document.querySelector('#myModal')) modal.show() bootstrap.Tooltip.getOrCreateInstance(document.querySelector('#myTooltip')) -
用原生监听器替换jQuery事件处理器。Bootstrap 5派发原生的事件,因此
CustomEvent会失效:$(el).on('shown.bs.modal', …)js// v5 document.querySelector('#myModal').addEventListener('shown.bs.modal', event => { // event.relatedTarget replaces event.relatedTarget from the jQuery event }) -
其他较小的API重命名:
- →
_getInstance(),推荐使用新增的getInstance()。getOrCreateInstance() - sanitizer配置项 →
whiteList。allowList - Tooltip和Popover的 /
title仍然支持函数,但函数内的content不再指向jQuery元素。this
-
如果你之前依赖jQuery的或者
noConflict风格的调用,请基于类API重写——v5已经没有jQuery接口可以兼容了。$.fn.button('toggle')
Step 4: v4 to v5 — classes and attributes
步骤4:v4 到 v5 —— 类名与属性
Directional utilities become logical (RTL support)
方向类工具变为逻辑属性(支持RTL)
| v4 | v5 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
The responsive variants follow the same rename ( → ).
.ml-md-3.ms-md-3| v4 | v5 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
响应式变体也遵循同样的重命名规则(例如 → )。
.ml-md-3.ms-md-3Typography and helpers
排版与辅助类
| v4 | v5 |
|---|---|
| |
| |
| |
| |
| |
| Removed |
| |
| v4 | v5 |
|---|---|
| |
| |
| |
| |
| |
| 已移除 |
| |
Forms
表单
This is the largest v4 to v5 rewrite: custom form controls were merged into the standard classes.
| v4 | v5 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Spacing utilities, e.g. |
| |
| Grid or flex utilities ( |
| |
Input groups lose their wrappers: delete and and place , buttons, and controls as direct children of . Validation feedback moves out of the removed wrappers too.
.input-group-append.input-group-prepend.input-group-text.input-group这是v4到v5改动最大的部分:自定义表单控件已合并到标准类中。
| v4 | v5 |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 间距工具类,例如 |
| |
| 栅格或flex工具类( |
无类名的 | |
输入组不再需要包裹层:删除 和 ,将 、按钮和控件直接作为 的子元素。校验反馈内容也需要从已移除的包裹层中移出来。
.input-group-append.input-group-prepend.input-group-text.input-groupComponents dropped or restructured
被移除或重构的组件
| v4 | v5 |
|---|---|
| Removed — compose with |
| Removed — use |
| Removed — use the grid, or CSS columns for masonry |
| |
| |
| |
| A |
| |
| |
| |
Badges also lost their link styling — a no longer gets hover/focus states.
<a class="badge">| v4 | v5 |
|---|---|
| 已移除——用 |
| 已移除——使用 |
| 已移除——使用栅格,或CSS columns实现瀑布流 |
| |
| |
| |
| 外层包裹 |
| 在 |
| |
| |
Badge组件也不再自带链接样式—— 不再有hover/focus状态。
<a class="badge">Grid, navbars, and navs
栅格、导航栏与导航
- →
.no-gutters, and gutters are now controlled by the.g-0,.g-*,.gx-*utilities..gy-* - A new breakpoint (1400px) exists; existing
xxlclasses keep their meaning.xl - Navbars now require a container inside (
.navbar).<nav class="navbar"><div class="container">… - For tabs and pills, moves from
.activeto.nav-item..nav-linkis optional as a wrapper..nav-item - items can be
.dropdown-menuor<button>;<a>was added for plain text..dropdown-item-text
- →
.no-gutters,现在栅格间距由.g-0、.g-*、.gx-*工具类控制。.gy-* - 新增了 断点(1400px);原有的
xxl类含义保持不变。xl - 导航栏现在要求在 内部有一个container元素(
.navbar)。<nav class="navbar"><div class="container">… - 对于标签页和胶囊导航,类从
.active移到了.nav-item上。.nav-link作为包裹元素现在是可选的。.nav-item - 的子项可以是
.dropdown-menu或<button>;新增了<a>用于纯文本项。.dropdown-item-text
Step 5: v5 to v6 — hand off to the v6 migration skill
步骤5:v5 到 v6 —— 移交至v6迁移技能
Once the project builds and renders on Bootstrap 5, switch to the skill and work through its steps. Do not attempt those renames while v4 patterns are still present — its find-and-replace steps assume v5 names.
bootstrap-v5-v6-migrationThat stage covers, among other things:
- to
@import, and the move from Sass scalars to CSS token maps (@usemerges theme maps again in v6)defaults() - Popper to Floating UI, plus Vanilla Calendar Pro for Datepicker. The bundled build includes both peers; the standalone build leaves them external.
- The responsive prefix syntax replacing
md:infixes-md- - Modal to Dialog, Offcanvas to Drawer, Dropdown to Menu. Dialog and Drawer use native elements.
<dialog> - to
.btn-primary, and.btn-solid .theme-primaryto.text-*.fg-* - ESM-only JavaScript with no global
window.bootstrap - Container-query contexts for responsive stacks, CSS Grid, navbars, list groups, card groups, and stacked tables
Two v4-era details worth flagging before you start it: the → element you produced in Step 4 keeps that name in v6 (but must stay empty, with no child SVG), and the markup you just wrote gets rebuilt again into v6's / / structure.
.close.btn-close.form-check.check.radio.switch当项目在Bootstrap 5下能够正常构建和渲染后,切换到**** 技能并按其步骤操作。在还存在v4模式的情况下不要尝试进行这些重命名——该技能的查找替换步骤默认代码已经是v5命名规范。
bootstrap-v5-v6-migration该阶段主要涵盖以下内容:
- 从 迁移到
@import,以及从Sass标量迁移到CSS token映射(v6中@use会再次合并主题映射)defaults() - 从Popper迁移到Floating UI,以及Datepicker改用Vanilla Calendar Pro。打包版本包含这两个依赖;独立版本则将它们作为外部依赖。
- 响应式前缀语法取代
md:中缀-md- - Modal改为Dialog,Offcanvas改为Drawer,Dropdown改为Menu。Dialog和Drawer使用原生 元素。
<dialog> - 改为
.btn-primary,.btn-solid .theme-primary改为.text-*.fg-* - JavaScript仅支持ESM,不再提供 全局变量
window.bootstrap - 为响应式堆叠、CSS Grid、导航栏、列表组、卡片组和堆叠表格提供容器查询上下文
在开始v6迁移之前,有两个v4时代的细节需要提醒:你在步骤4中修改得到的 → 元素在v6中保留该名称(但必须保持为空,不能有子SVG元素);而你刚写好的 标记在v6中会被再次重构为 / / 结构。
.close.btn-close.form-check.check.radio.switchStep 6: Verify
步骤6:验证
- Build the project and fix compilation errors before looking at the rendered output.
- Search for leftover v4 patterns:
- ,
data-toggle=,data-target=,data-dismiss=,data-ride=,data-slide(should all bedata-spy=)data-bs-* - ,
class="close"inside a close button (should be×).btn-close - ,
badge-(should bebadge-pill/.badge .bg-*).rounded-pill - ,
custom-select,custom-control,custom-switch,custom-rangecustom-file - ,
form-group,form-row,form-inline,input-group-appendinput-group-prepend - ,
\bm[lr]-,\bp[lr]-,float-left,float-right,text-left,text-right,border-leftborder-right - ,
font-weight-,font-italictext-monospace - ,
no-gutters,sr-only,embed-responsive,jumbotron,media-body,card-deck,card-columns,btn-block,thead-light,thead-dark,pre-scrollabletext-justify - ,
\$(near Bootstrap plugin names (jQuery(,.modal(,.tooltip().collapse( - ,
theme-color(,color-yiq(,theme-color-level(media-breakpoint-down(
- Click through every interactive component — the data attribute renames fail silently, so a missed looks like a dead button rather than an error.
data-toggle - Check forms especially closely: custom controls, input groups, and validation feedback all changed structure.
- Then move to Step 5 and repeat this discipline for the v6 stage.
- 先构建项目并修复所有编译错误,再检查渲染输出。
- 搜索残留的v4模式:
- 、
data-toggle=、data-target=、data-dismiss=、data-ride=、data-slide(都应该是data-spy=格式)data-bs-* - 、关闭按钮内的
class="close"(应该使用×).btn-close - 、
badge-(应该是badge-pill/.badge .bg-*).rounded-pill - 、
custom-select、custom-control、custom-switch、custom-rangecustom-file - 、
form-group、form-row、form-inline、input-group-appendinput-group-prepend - 、
\bm[lr]-、\bp[lr]-、float-left、float-right、text-left、text-right、border-leftborder-right - 、
font-weight-、font-italictext-monospace - 、
no-gutters、sr-only、embed-responsive、jumbotron、media-body、card-deck、card-columns、btn-block、thead-light、thead-dark、pre-scrollabletext-justify - Bootstrap插件名附近的 、
\$((例如jQuery(、.modal(、.tooltip().collapse( - 、
theme-color()、color-yiq()、theme-color-level()media-breakpoint-down()
- 逐个点击所有交互组件——数据属性的重命名不会报错,因此漏掉的 只会表现为按钮点击无反应,而不会抛出错误。
data-toggle - 要特别仔细检查表单:自定义控件、输入组和校验反馈的结构都发生了变化。
- 之后进入步骤5,在v6迁移阶段重复同样的验证流程。