Bootstrap 4.1.1中Glyphicon无法在模板渲染的问题求助
Hey there! I’ve run into this exact snag before—let’s break down what’s going on and how to fix it.
The Core Problem
Bootstrap 4.x completely removed Glyphicons from its core package. Your project is using Bootstrap 4.1.1, which doesn’t include any Glyphicon files at all. That’s why even though you’ve imported Bootstrap correctly, the icon won’t render. Glyphicons were only included in Bootstrap 3 and earlier versions.
Fix Options to Try
1. Switch to Font Awesome (Official Recommendation)
Bootstrap’s docs now point to Font Awesome as the go-to icon library replacement. Here’s how to set it up:
- Install the package via npm:
npm install @fortawesome/fontawesome-free - Import it in your
styles.cssright after Bootstrap:@import '~bootstrap/dist/css/bootstrap.css'; @import '~@fortawesome/fontawesome-free/css/all.css'; - Replace your Glyphicon code with the Font Awesome equivalent:
<i class="fas fa-star"></i>
2. Manually Add Glyphicons (Not Recommended)
If you really need to stick with Glyphicons, you’ll have to add them separately:
- Download the Glyphicon files (CSS and font assets) from their official source.
- Place the font files in your project’s public/assets folder.
- Import the Glyphicon CSS in your
styles.css(make sure the path matches where you placed the files).
Note: This is a hassle to maintain since Glyphicons aren’t actively updated anymore.
3. Downgrade to Bootstrap 3
If your project can tolerate switching versions, downgrading to Bootstrap 3 will bring Glyphicons back:
- Update your
package.jsonto use a Bootstrap 3 version, e.g.:"dependencies": { "bootstrap": "^3.4.1" } - Run
npm installto update the dependency. - Your original Glyphicon code will work again, but keep in mind Bootstrap 3 has different syntax and components compared to 4—you might need to adjust other parts of your project.
内容的提问来源于stack exchange,提问作者Alpha Bravo Charlie ...

