CDN 快速上手

关于如何使用 CDN 在项目中引入 Bootstrap 的 CSS 和 JavaScript 的官方指南。

使用 CDN引入我们生产就绪的 CSS 和 JavaScript,无需任何构建步骤,即可在几秒内开始使用 Bootstrap。CDN 提供 Bootstrap 代码库的副本,可在任何环境中使用,你只需做极少的代码更改。

在线演示

已经熟悉 HTML 页面的搭建? 前往这个 Bootstrap CodePen 演示查看最终结果。

步骤

  1. 在项目根目录中创建一个新的 index.html 文件。 同时引入 <meta name="viewport"> 标签,以便在移动设备上获得正确的响应式行为。

    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>
      </head>
      <body>
        <h1>Hello, world!</h1>
      </body>
    </html>
            
  2. 引入 Bootstrap 的 CSS 和 JS。 将 CSS 的 <link> 标签放入 <head> 中,并将 JavaScript 包(包含用于定位菜单、弹出框和工具提示的 Floating UI,以及用于日期选择器的 Vanilla Calendar Pro)的 <script> 标签放在结束的 </body> 之前。在我们的 CDN 链接中了解更多。

    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.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MKn5NWVBEuQXJE7TSKSgPUmMiSCmVTfCsR+VSSeQpHnoyJQZZFn/kBn+Ht5jZDPQ" crossorigin="anonymous">
      </head>
      <body>
        <h1>Hello, world!</h1>
        <script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-+PW0IDVGGwmpaBQvcXY+W0BcpHpITFB3dSfSoGvDdHtLbrLpSbpGcvtQRM/BJpz+" crossorigin="anonymous"></script>
      </body>
    </html>
            

    选择适合你配置的 JavaScript 文件:

    • bootstrap.bundle.js 包含 Floating UI 和 Vanilla Calendar Pro。你可以直接使用 <script type="module"> 加载它。
    • bootstrap.js 引用这两个依赖项。当你在浏览器中直接加载它时,必须通过 import map 提供这两个依赖项。

    将以下 import map 与非 bundle 文件配合使用:

    HTML
              <script type="importmap">
    {
      "imports": {
        "@floating-ui/dom": "https://cdn.jsdelivr.net/npm/@floating-ui/dom@1.8.0/dist/floating-ui.dom.esm.min.js",
        "vanilla-calendar-pro": "https://cdn.jsdelivr.net/npm/vanilla-calendar-pro@3.4.0/index.mjs"
      }
    }
    </script>
    <script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.2/dist/js/bootstrap.min.js" integrity="sha384-3Yo3YCoxUQZoPH8he5dQIEAZ/k/vBTLYf1CFU5hE7GYPzQZu3dYpWvBwWKxN+HJZ" crossorigin="anonymous"></script>
            

    要省略任一依赖项,请仅导入你使用的 Bootstrap 组件,或使用支持 tree shaking 的打包工具。

  3. Hello, world! 在你选择的浏览器中打开该页面,即可看到应用了 Bootstrap 的页面。现在你可以开始使用 Bootstrap 进行构建了,创建自己的布局、添加数十种组件,并利用我们的官方示例。

可选字体

Bootstrap 的源代码默认使用原生字体栈,但我们为托管的文档使用了自定义字体,以便在各平台上获得更一致的样式。我们使用 Geist 和 Geist Mono,并建议你的项目使用相同或类似的字体。

  1. 从 Google Fonts 添加 Geist 网页字体。 将以下内容添加到你的 <head> 中,位于 Bootstrap 的 CSS 之前:

    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 rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Geist+Mono:wght@100..900&family=Geist:wght@100..900&display=swap" rel="stylesheet">
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MKn5NWVBEuQXJE7TSKSgPUmMiSCmVTfCsR+VSSeQpHnoyJQZZFn/kBn+Ht5jZDPQ" crossorigin="anonymous">
      </head>
      <body>
        <h1>Hello, world!</h1>
        <script type="module" src="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-+PW0IDVGGwmpaBQvcXY+W0BcpHpITFB3dSfSoGvDdHtLbrLpSbpGcvtQRM/BJpz+" crossorigin="anonymous"></script>
      </body>
    </html>
            
  2. 通过覆盖 Bootstrap 的 CSS 变量来应用字体。 你可以在 <head> 中的 <style> 标签里或自定义的 CSS 文件中完成此操作。

    HTML
              <style>
      :root {
        --bs-body-font-family: "Geist", system-ui, sans-serif;
        --bs-font-mono: "Geist Mono", ui-monospace, monospace;
      }
    </style>
            

    如果你从 CDN 配置升级为通过包管理器引入 Bootstrap,也可以在自定义的 Sass 文件中进行同样的操作。

在排版和 Reboot中了解更多关于 Bootstrap 排版默认值的信息。