Introduction
Bootstrap helps you build websites faster. Avoiding common mistakes makes your site look good and work well on all devices.
Bootstrap helps you build websites faster. Avoiding common mistakes makes your site look good and work well on all devices.
No special syntax, but follow Bootstrap class rules and structure carefully.
<div class="container"> <div class="row"> <div class="col-md-6">Content</div> <div class="col-md-6">Content</div> </div> </div>
<button class="btn btn-primary">Click me</button>
<!-- Incorrect: Missing 'row' div --> <div class="container"> <div class="col-md-6">Content</div> <div class="col-md-6">Content</div> </div>
<!-- Incorrect: Using custom class without Bootstrap base --> <button class="btn-primary">Click me</button>
This example shows correct and incorrect Bootstrap usage side by side. The first button uses both btn and btn-success classes and looks styled. The second button misses the btn class and looks plain. The last two columns are outside a row container, so the grid layout breaks.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Mistakes Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <header class="bg-primary text-white p-3"> <h1 class="text-center">Welcome</h1> </header> <main class="container mt-4"> <div class="row"> <div class="col-md-6"> <button class="btn btn-success w-100">Correct Button</button> </div> <div class="col-md-6"> <!-- Mistake: Missing 'btn' class --> <button class="btn-success w-100">Wrong Button</button> </div> </div> <!-- Mistake: Columns outside row --> <div class="row"> <div class="col-md-6 bg-light p-3">Wrong Grid</div> <div class="col-md-6 bg-light p-3">Wrong Grid</div> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
row div. This keeps the grid working correctly.btn and specific button style classes like btn-primary. Missing btn breaks button styles.btn with btn-primary.btn plus a style class like btn-primary to work properly.btn-primary alone without btn will not style the button correctly.btn-primary without the base btn class -> Option Bbtn with style classes [OK]btn class with button styles [OK]btn classcol without a number divides space equally among columns.<div class='row'><div class='col'>...</div><div class='col'>...</div><div class='col'>...</div></div> uses col three times inside a row, which creates three equal columns responsively.col inside a row for equal columns -> Option Acol inside row [OK]col for equal columns inside a row [OK]col-4 without a rowrow container<div class='container'>
<div class='row'>
<div class='col-md-6 col-sm-12'>Left</div>
<div class='col-md-6 col-sm-12'>Right</div>
</div>
</div>col-md-6 means half width on medium (≥768px) and larger screens; col-sm-12 means full width on small (≥576px) screens.col-sm and col-md breakpoints<nav class='navbar navbar-expand-lg'>
<div class='container-fluid'>
<a class='navbar-brand' href='#'>Brand</a>
<button class='navbar-toggler' type='button' data-toggle='collapse' data-target='#navbarNav'>
<span class='navbar-toggler-icon'></span>
</button>
<div class='collapse navbar-collapse' id='navbarNav'>
<ul class='navbar-nav'>
<li class='nav-item'><a class='nav-link' href='#'>Home</a></li>
</ul>
</div>
</div>
</nav>data-toggle and data-target to data-bs-toggle and data-bs-target.data-toggle and data-target, which won't work in Bootstrap 5.data-toggle and data-target instead of data-bs-toggle and data-bs-target -> Option Cdata-bs-* attributes [OK]data-bs-toggle and data-bs-target in Bootstrap 5 [OK]mx-auto applies automatic horizontal margins, centering the element inside its container.max-width: 24rem; limits the card's width but allows it to shrink on smaller screens, avoiding overflow.<div class='card mx-auto' style='max-width: 24rem;'>...</div> uses mx-auto and max-width, which is the best practice to center and limit width responsively.mx-auto and limit width with max-width [OK]mx-auto and max-width for centered responsive cards [OK]text-center which centers text, not the cardPass the test and this pattern moves unknown