Bootstrap 5 is the most widely used CSS framework in the world, and it is particularly popular among PHP developers because it requires no JavaScript build tools — you can include it via CDN and start building responsive layouts immediately. This guide covers everything PHP developers need to use Bootstrap 5 effectively in their projects.
Table of Contents
Including Bootstrap 5 in a PHP Project
Method 1: CDN (Simplest)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My PHP App</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Your PHP-generated content here -->
<?php echo $content; ?>
<!-- Bootstrap JS (includes Popper for dropdowns/tooltips) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Method 2: Composer + Laravel Mix / Vite
npm install bootstrap @popperjs/core
// resources/js/app.js (Laravel Vite):
import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
The Bootstrap Grid System
<div class="container"> <!-- Max-width centered container -->
<div class="row"> <!-- Flex row -->
<div class="col-md-8"> <!-- 8/12 columns on medium+ screens -->
<!-- Main content -->
</div>
<div class="col-md-4"> <!-- 4/12 columns sidebar -->
<!-- Sidebar -->
</div>
</div>
</div>
<!-- Responsive: full width on mobile, half on tablet, third on desktop -->
<div class="col-12 col-md-6 col-lg-4">Card</div>
PHP with Bootstrap: User Listing Example
<?php
$stmt = $pdo->query("SELECT * FROM users ORDER BY name");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="container mt-4">
<div class="d-flex justify-content-between align-items-center mb-3">
<h2>Users <span class="badge bg-primary"><?= count($users) ?></span></h2>
<a href="create.php" class="btn btn-success">+ Add User</a>
</div>
<div class="table-responsive">
<table class="table table-striped table-hover align-middle">
<thead class="table-dark">
<tr><th>Name</th><th>Email</th><th>Role</th><th>Actions</th></tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= htmlspecialchars($user['name']) ?></td>
<td><?= htmlspecialchars($user['email']) ?></td>
<td><span class="badge bg-<?= $user['role'] === 'admin' ? 'danger' : 'secondary' ?>">
<?= htmlspecialchars($user['role']) ?></span></td>
<td>
<a href="edit.php?id=<?= $user['id'] ?>" class="btn btn-sm btn-outline-primary">Edit</a>
<button class="btn btn-sm btn-outline-danger"
data-bs-toggle="modal" data-bs-target="#deleteModal"
data-id="<?= $user['id'] ?>">Delete</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
Bootstrap Forms with PHP Validation
<?php
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (empty($_POST['email'])) $errors['email'] = 'Email is required.';
if (strlen($_POST['password']) < 8) $errors['password'] = 'Password must be at least 8 characters.';
if (empty($errors)) {
// Process form
}
}
?>
<form method="POST" action="">
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control <?= isset($errors['email']) ? 'is-invalid' : '' ?>"
id="email" name="email" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>">
<?php if (isset($errors['email'])): ?>
<div class="invalid-feedback"><?= $errors['email'] ?></div>
<?php endif; ?>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control <?= isset($errors['password']) ? 'is-invalid' : '' ?>"
id="password" name="password">
<?php if (isset($errors['password'])): ?>
<div class="invalid-feedback"><?= $errors['password'] ?></div>
<?php endif; ?>
</div>
<button type="submit" class="btn btn-primary">Sign In</button>
</form>
Confirmation Delete Modal
<!-- Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Delete</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">Are you sure you want to delete this user?</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<form id="deleteForm" method="POST" action="delete.php">
<input type="hidden" name="id" id="deleteId">
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
</div>
</div>
</div>
<script>
// Pass the user ID to the modal when triggered:
document.getElementById('deleteModal').addEventListener('show.bs.modal', function(e) {
document.getElementById('deleteId').value = e.relatedTarget.dataset.id;
});
</script>
Bootstrap in Laravel (Blade)
composer require laravel/ui
php artisan ui bootstrap --auth
npm install && npm run build
This scaffolds Bootstrap 5 with authentication views (login, register, dashboard). The Blade templates use Bootstrap classes throughout, giving you a production-quality starting point.
Frequently Asked Questions
Bootstrap vs Tailwind CSS for PHP projects?
Bootstrap provides pre-built components (cards, tables, modals, forms) — ideal for admin panels and traditional web apps. Tailwind provides utility classes without pre-built components — better for custom designs. For PHP CRUD applications and admin interfaces, Bootstrap is faster. For unique designs matching a brand, Tailwind is more flexible. Laravel’s official UI scaffolding supports both.
Conclusion
Bootstrap 5 is the fastest way to add professional styling to PHP projects. Include it via CDN, apply responsive grid classes, use pre-built form validation styles with PHP error variables, and add modals for confirm dialogs. For Laravel, the laravel/ui package scaffolds Bootstrap authentication views in one command.
Next Step for Your Project Journey
Pick your next action to keep building: download complete source code, continue with practical tutorials, or explore more project systems.

