How to Fix Bootstrap Not Working: Common Causes and Solutions
<link> for Bootstrap CSS and <script> tags for Bootstrap JS and its dependencies like Popper.js in the right order.Why This Happens
Bootstrap may not work if you forget to link its CSS or JavaScript files, or if you link them in the wrong order. Sometimes, missing dependencies like Popper.js cause components like dropdowns or tooltips to fail. Also, using incorrect file paths or missing the required viewport meta tag can break Bootstrap's responsive design.
<!DOCTYPE html> <html lang="en"> <head> <title>Broken Bootstrap Example</title> <!-- Missing Bootstrap CSS link --> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <button class="btn btn-primary">Click me</button> <!-- Missing Bootstrap JS and dependencies --> </body> </html>
The Fix
Include the Bootstrap CSS file inside the <head> section using a correct <link> tag. Add Bootstrap's JavaScript and its dependencies like Popper.js just before the closing </body> tag. Also, ensure the viewport meta tag is present for responsive design.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Fixed Bootstrap Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <button class="btn btn-primary">Click me</button> <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Prevention
Always use the official Bootstrap CDN links or local files with correct paths. Check the order: CSS in <head>, JS and dependencies before </body>. Use the viewport meta tag for mobile responsiveness. Test your page in the browser and use developer tools to check if files load correctly. Consider using a starter template from Bootstrap's official site to avoid missing parts.
Related Errors
- JavaScript components not working: Usually caused by missing Popper.js or incorrect script order.
- Bootstrap styles not applying: Check if CSS file is linked and path is correct.
- Responsive layout broken: Missing or incorrect
viewportmeta tag.