Bootstrap Collapse Sidebar
Posted by Superadmin on April 30 2023 15:47:24

Bootstrap Collapse Sidebar

By Priya PedamkarPriya Pedamkar
  

 

Bootstrap Collapse Sidebar

Introduction to Bootstrap Collapse Sidebar

Collapse Side Bar in Bootstrap is defined as a list of links or buttons or content on any side of the page when we click on the button sidebar hidden or shown simultaneously. Collapse Side Bar can be on the left side or right side depending upon the user requirement. Make the user feel full-page display without showing additional features until click on the Collapse button.

Real-time Example:

If we want to display the main content like Home, Register, Login, Order details, etc. preferred on the left side collapsible sidebar. Filters like Brand name, cost, color, size, new products, etc. preferred on the right side collapsible sidebar. This we can observe in Amazon or Flipkart e-commerce websites. Collapse features can also be applied to top and bottom also.

Why Bootstrap over HTML?

Note: Bootstrap files also saved with.html extension.

How does Collapse Side Bar Work in Bootstrap?

Syntax:

<button type="button" class="btn btn-info" data-toggle="collapse"
data-target="#collapseText">Collapse</button>
<div id="collapseText">Hide and Show Me by Clicking above button</div>

Explanation:

Include bootstrap feature in our application we must specify some pre-defined libraries inside our application.

They are as follows:

1. Includes bootstrap view.

<meta name="viewport" content="width=device-width, initial-scale=1">

2. Includes ajax and jQuery libraries.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

3. Includes bootstrap libraries.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

4. Includes bootstrap libraries.

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

Examples of Bootstrap Collapse Sidebar

Given below are the examples of Bootstrap Collapse Sidebar:

Example #1

Collapse on bottom content.

Code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<h2>Bootstrap Collapse Demo</h2>
<button type="button" class="btn btn-info" data-toggle="collapse"
data-target="#collapseText">Click Me To Collapse</button>
<div id="collapseText" class="collapseClass">I am displayed
because of bootstrap collapse feature. Please click me again to hide.
</div>
</body>
</html>

Output:

bootstrap collapse sidebar 1

bootstrap collapse sidebar 2

Explanation:

Example #2

Collapse on bottom content with href.

Code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<h2>Bootstrap Collapse Demo</h2>
<a href="#buttonRef" class="btn btn-primary" data-toggle="collapse">Click
Me To Collapse</a>
<div id="buttonRef" class="collapse">I am displayed because of
bootstrap collapse feature. Please click me again to hide.</div>
</body>
</html>

Output:

Content with href.

bootstrap collapse sidebar 4

Explanation:

Example #3

Collapse on bottom content class=“collapse show”.

Code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<h2>Bootstrap Collapse Demo</h2>
<button type="button" data-target="#targetText" data-toggle="collapse" class="btn btn-primary">Click Me To Collapse</button>
<div id="targetText" class="collapse show">I am
displayed because of bootstrap collapse feature. Please click me again
to hide.</div>
</body>
</html>

Output:

content class=“collapse show”

bootstrap collapse sidebar 6

Explanation:

Example #4

Collapse on side bar content.

Code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="SideBarCollapse.css">
<script type="text/javascript" src="SideBarCollapse.js"></script>
<script>function showNagination() {
document.getElementById("sideBarId").style.width = "260px";
document.getElementById("click").style.marginLeft = "260px";
}
function hideNavigation() {
document.getElementById("sideBarId").style.width = "0";
document.getElementById("click").style.marginLeft = "0";
}
</script>
<style>
.sideBarClass {
height: 100%;
z-index: 1;
width: 0;
transition: 0.5s;
position: fixed;
top: 0;
padding-top: 60px;
left: 0;
background-color: #111;
overflow-x: hidden;
}
.sideBarClass button {
padding: 7px 7px 7px 31px;
font-size: 24px;
display: block;
transition: 0.25s;
color: gray;
}
.button {
font-size: 21px;
background-color: purple;
cursor: pointer;
color: silver;
border: none;
padding: 11px 16px;
}
.sideBarClass button:hover {
color: fuchsia;
}
.sideBarClass .hideButton {
position: absolute;
font-size: 37px;
top: 0;
margin-left: 55px;
right: 26px;
}
.clickClass {
padding: 16px;
transition: margin-left .5s;
}
.button:hover {
background-color: #444;
}
@media screen and (max-height: 450px) {
.sideBarClass {
padding-top: 15px
}
.sideBarClass button {
font-size: 18px;
}
}
body {
font-family: serif;
}
</style>
</head>
<body>
<div id="click" class="clickClass">
<button class="button" onclick="showNagination()">Click On This Side Bar</button>
<h2>Collapsed Side Bar Demo</h2>
<p>Click on Side Bar to Hide and Open Side Bar.</p>
</div>
<div id="sideBarId" class="sideBarClass">
<a href="javascript:void(0)" class="hideButton"
onclick="hideNavigation()">Close</a>
<button type="button">First Link</button>
<button type="button">Second Link</button>
<button type="button">Third Link</button>
<button type="button">Fourth Link</button>
<button type="button">Fifth Link</button>
<button type="button">Sixth Link</button>
</div>
</body>
</html>

Output:

bootstrap collapse sidebar 7

Example 8

Explanation: