萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

Bootstrap 框架檔案目錄結構深度解析:核心檔案、引入順序與最佳實踐

官方發布包目錄結構 (v3.x 為例)

bootstrap/
├── css/
│   ├── bootstrap.css          /* 完整樣式 (含響應式),開發/除錯用 */
│   ├── bootstrap.min.css      /* 生產環境:壓縮、無註解、最小體積 */
│   ├── bootstrap-theme.css    /* 可選主題:立體/漸層/陰影風格 */
│   ├── bootstrap-theme.min.css
│   └── bootstrap.map / .min.map  /* Source Map,支援 DevTools 對應源碼 */
├── js/
│   ├── bootstrap.js           /* 完整 JS 插件 (含所有元件),開發/除錯用 */
│   ├── bootstrap.min.js       /* 生產環境:壓縮版 */
│   └── npm.js                 /* npm 發布用進入點 (不含 jQuery) */
└── fonts/
    ├── glyphicons-halflings-regular.eot
    ├── glyphicons-halflings-regular.svg
    ├── glyphicons-halflings-regular.ttf
    ├── glyphicons-halflings-regular.woff
    └── glyphicons-halflings-regular.woff2

核心檔案功能對照

檔案內容使用時機
bootstrap.css所有元件樣式:網格、排版、表格、表單、按鈕、導航、元件、工具類、響應式斷點開發環境、需閱讀/調試樣式
bootstrap.min.css同上,經壓縮 (移除空白、註解、縮短選擇器)生產環境,**必須使用**
bootstrap-theme.css可選 3D/立體風格主題 (按鈕、面板、進度條等)需預設立體風格時引入,**在核心 CSS 之後**
bootstrap.js所有 jQuery 插件:Modal、Dropdown、Tab、Tooltip、Popover、Carousel、Collapse、Affix、Scrollspy、Button、Transition開發環境
bootstrap.min.js同上,壓縮版生產環境,**必須使用**
字體檔案Glyphicons Halflings 圖標字體 (5 種格式跨瀏覽器)自動由 CSS @font-face 載入,**fonts/ 必須與 css/ 同層或正確配置路徑**

正確引入順序 (關鍵!)

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- 1. 核心樣式 -->
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <!-- 2. 可選主題 (若需) -->
  <link rel="stylesheet" href="css/bootstrap-theme.min.css">
  <!-- 3. 自定義樣式 (覆蓋/擴充 Bootstrap) -->
  <link rel="stylesheet" href="css/custom.css">
</head>
<body>
  <!-- 頁面內容 -->

  <!-- 1. jQuery (Bootstrap JS 依賴,v3 需 1.9+ / v4+ 不需) -->
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <!-- 2. Bootstrap JS -->
  <script src="js/bootstrap.min.js"></script>
  <!-- 3. 自定義腳本 -->
  <script src="js/app.js"></script>
</body>
⚠ 常見錯誤:
  1. 自定義 CSS 放在 Bootstrap 之前 → 會被覆蓋,無法生效。
  2. JS 放在 <head> 且無 defer → 執行時 DOM 未就緒,選擇器失效。
  3. 未引入 jQuery 直接引入 Bootstrap JS → Uncaught Error: Bootstrap's JavaScript requires jQuery。
  4. 字體檔案路徑錯誤 → 圖標顯示為方框;檢查 @font-face src 路徑相對 bootstrap.min.css 位置。

響應式檔案說明 (v2.x 遺留認知修正)

v2.x 有獨立 bootstrap-responsive.css;**v3.x 起響應式樣式已整合進核心 CSS**,**不再需要**額外引入響應式檔案。若專案仍引用 v2.x 風格雙檔案,請確認版本並調整引入策略。

客製化建構流程 (進階)

  1. 源碼編譯:下載源碼 (Less/SCSS),修改 variables.less/_variables.scss 設計令牌,執行 grunt dist / npm run build 產出客製化版本。
  2. 模組化引入:Webpack/Vite 環境下,import 'bootstrap/scss/bootstrap.scss' 或按需 import 'bootstrap/js/dist/modal' 實現 Tree Shaking。
  3. 主題包:Bootswatch、Bootstrap Themes 等第三方主題,僅替換 CSS 檔案即可換膚。

理解 Bootstrap 目錄結構與引入順序,是避開「樣式不生效、JS 報錯、圖標缺失」等低級錯誤的前提,也是客製化主題、優化打包體積的基礎。

copyright © 萬盛學電腦網 all rights reserved