Top 3 features of Bootstrap5 - Removed Jquery dependency
Last updated Aug 10, 2021
Bootstap, what is bootstrap the world's most popular framework for building responsive, mobile-first sites
Bootstrap home page changed with their major release v5.1.0
Now here we will know what are the major changes in bootstrap 5
New Logo
New Offcanvas
New Accordian
RTL feature
New and Updated Forms
New Components Utilities API
Enhanced Grid and Layout
Jquery isn't required any more
Enhanced Modularity
Enhanced Icons
To work with new Bootstrap 5 features we need to add related css and js file into the file
New Logo
Now they changed the logo ass curl brases and added B inside these braces
New Offcanvas
One of the major feature of the bootstrap5 is Offcanvas
What is Offcanvas component - It is a sidebar component that can be toggled via javascript to appear from the left, right or bottom edge of the viewport
Now let's create simple Offcanvas example with bootstrap5
Create simple HTML file and add Bootstrap5 css and js files
Now add our example code which will show offcanvas when we tap on toogle button
let's see the complete code
html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container min-vh-100 py-2">
<div class="row">
<div class="col">
<h2 class="font-weight-light">Bootstrap 5 Sidebar</h2>
<p> This is a Bootstrap 5 off-canvas menu example! </p>
<div class="d-flex justify-content-end form-check form-switch form-control-lg">
<input class="form-check-input" type="checkbox" id="toggleSidebar">
<label class="form-check-label ms-1" for="toggleSidebar">Toggle Offcanvas</label>
</div>
<div class="text-center">
<img src="https://icons.getbootstrap.com/assets/img/icons-hero.png" />
</div>
<div class="offcanvas offcanvas-start bg-light" tabindex="-1" id="offcanvasExample">
<div class="offcanvas-header">
<h5 class="offcanvas-title">Offcanvas</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div> Some text as placeholder. In real life you can have the elements you have chosen. Like, text, images, lists, etc. </div>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown"> Menu </button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js">
</script>
<script>
var toggle = document.getElementById("toggleSidebar")
var offcanvas_el = document.getElementById("offcanvasExample")
var offcanvas = new bootstrap.Offcanvas(offcanvas_el, {backdrop: false})
toggle.addEventListener("change", function(){
toggle.checked ? offcanvas.show() : offcanvas.hide()
})
// handle case when sidebar is closed internally using `X`
offcanvas_el.addEventListener('hide.bs.offcanvas', function () {
toggle.checked = false
})
</script>
</body>
</html>
New Accordian
Bootstrap5 has one more feature called Accordian. Previously it has .card accordian now they changed to new .accordian component. This new accordian still uses Javascript plugin with some custom css and HTML.
This Accordian includes bootstrap Icons as chevron icons indicating state and clickability.
This new accordian has properties
Flush
To remove default background color by add .accordion-flush
Always Open
To open accrodin item when other item opens we need to remove the data-bs-parent attribute from each item
Simple Bootstrap Accordian example from official website
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
<div class="accordion-body">
<strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow. </div> </div> </div> <div class="accordion-item">
<h2 class="accordion-header" id="headingTwo">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
<div class="accordion-body">
<strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingThree">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3 </button>
</h2>
<div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
<div class="accordion-body"> <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow. </div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js">
</script>
<script> var toggle = document.getElementById("toggleSidebar") var offcanvas_el = document.getElementById("offcanvasExample") var offcanvas = new bootstrap.Offcanvas(offcanvas_el, {backdrop: false})