You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap在create-react-app创建的React项目中无法正常工作

Troubleshooting Bootstrap Not Working in Create React App

Hey there! Let's walk through why Bootstrap might not be functioning correctly in your Create React App project, even after adding the CDN links to public/index.html. Here are the most common fixes:


1. Double-Check CDN Loading Order & Placement

Bootstrap's JavaScript depends entirely on jQuery, so the load order and placement of these scripts in index.html can easily break functionality. Here's the correct setup:

  • Put the Bootstrap CSS in the <head> section: This ensures styles load before your React content renders, preventing flash-of-unstyled-content issues.
  • Place jQuery and Bootstrap JS at the END of the <body> tag, right before React's auto-injected scripts: This guarantees jQuery is fully loaded and available when Bootstrap's JS tries to initialize.

Example index.html snippet:

<head>
  <!-- Your existing meta tags, favicon, etc. -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body>
  <noscript>You need to enable JavaScript to run this app.</noscript>
  <div id="root"></div>
  
  <!-- Load jQuery FIRST, then Bootstrap JS -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
  <!-- React's bundle scripts will be injected here automatically by CRA -->
</body>

2. Use className Instead of class in React Components

React uses className (not the standard HTML class attribute) to apply CSS classes. This is a super common mistake that makes it look like Bootstrap isn't working:

// ✅ Correct: Uses className for Bootstrap classes
<button className="btn btn-primary">Submit</button>

// ❌ Wrong: class won't work in React JSX
<button class="btn btn-primary">Submit</button>

3. Try Installing Bootstrap via npm (Alternative to CDN)

If CDNs still aren't playing nice, installing Bootstrap and jQuery locally via npm can avoid network or load-order conflicts:

  1. Install the dependencies:
npm install bootstrap@3.3.7 jquery@3.3.1
  1. Import them in your src/index.js file (this replaces the CDN links in index.html):
import 'bootstrap/dist/css/bootstrap.min.css';
import $ from 'jquery';
import 'bootstrap/dist/js/bootstrap.min.js';

After making these changes, restart your development server (npm start) and test a Bootstrap component (like a dropdown, modal, or styled button) to see if it works as expected.


内容的提问来源于stack exchange,提问作者Sangram Badi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:30:01