How to Make the Navbar Collapse on Smaller Screens in React Bootstrap ?
Last Updated :
26 Jul, 2024
Earlier, websites were mainly designed for desktop computers because that's what most people used. But now, with so many devices that can access the internet, websites need to be responsive on all sorts of screen sizes and types. So, In this article, we will see how to make Navbar collapse on a smaller screen using libraries like React-Bootstrap.
Prerequisites:
Steps to create the project:
Step1: Create a React application using the following command
npx create-react-app folderName
Step2: Create a React application using the following command
cd folderName
Step 3: Now install React-bootstrap and bootstrap
Step 3: Now install React-bootstrap and bootstrap
Step 4: Add Bootstrap CSS to index.js
import 'bootstrap/dist/css/bootstrap.min.css';
Step 5: Create a folder "Components" and create a file named "NavBarComponent" inside this folder.
Project Structure:
Project Structure The updated dependencies in package.json will look like this :
{
"name": "responsivenavbar",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.3.2",
"react": "^18.2.0",
"react-bootstrap": "^2.9.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}
Approach:
- Navbar component is used to create the main navigation bar. and props like collapseOnSelect is used to ensure the navbar collapses on selecting an item. and another prop expand is used to expand the navbar on larger screens.
- Container Component is used to wrap the navbar content within a responsive container.
- Navbar Brand Component is used to add a brand logo to the navbar.
- Navbar Toggle Button is used to Include a responsive navbar toggle button.
- Collapsible Navbar Content is used to Define the collapsible content of the navbar within Navbar.Collapse.
- Nav component is used at two places one for placing items to left and another to place item to right and inside this Nav.Linkis used to add navigation.
- NavDropdown Component is used to include a dropdown menu.
Example: App.js file imports NavBarComponent and exports App component. NavBarComponent.js file imports Container, Nav, Navbar and NavDropdown component from react-bootstrap library and after making UI exports NavBarComponent.
JavaScript
// App.js
import NavbarComponent from
"./components/NavBarComponent";
function App() {
return <NavbarComponent>
</NavbarComponent>;
}
export default App;
JavaScript
// NavBarComponent.js
import Container from 'react-bootstrap/Container';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import NavDropdown from 'react-bootstrap/NavDropdown';
function NavbarComponent() {
return (
<Navbar collapseOnSelect expand="lg"
className="bg-secondary">
<Container>
<Navbar.Brand href="#home">
GeeksForGeeks
</Navbar.Brand>
<Navbar.Toggle
aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="me-auto">
<Nav.Link href="#DataStructures">
Data Structures
</Nav.Link>
<Nav.Link href="#CompetitiveProgramming">
Competitive Programming
</Nav.Link>
<NavDropdown title="Courses"
id="collapsible-nav-dropdown">
<NavDropdown.Item href="#action/3.1">
backend
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.2">
frontend
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.3">
DSA
</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.4">
cp
</NavDropdown.Item>
</NavDropdown>
</Nav>
<Nav>
<Nav.Link href="#contactus">
Contact Us
</Nav.Link>
<Nav.Link eventKey={2} href="#community">
Community
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
);
}
export default NavbarComponent;
Steps to run the application:
Step 1: Run the Application using following command from the root directory of the project :
npm start
Step 2: Open web-browser and type the following URL
http://localhost:3000/
Output:
Similar Reads
How to make columns stack vertically on smaller screens in React Bootstrap? In the article, we used different components like rows, columns, etc. to display and deliver the information to the user. However, on smaller screen devices, there is an issue with the representation of these components. So, to handle the columns on the smaller screens, we can use grid classes and F
3 min read
How to make rounded corner for navbar using react-bootstrap? In single-page applications, Navigation Bars are the most important components of web applications to provide national access to different sections and features of the applications. We can customize the navigation bar by adjusting the colors, width, and we can also make the rounded corners for the n
3 min read
Create Responsive Sidebar Menu to Top Navbar in Bootstrap Bootstrap's responsive sidebar streamlines navigation across devices. It features links to the Home, About Us, and Contact Us sections. The page displays sample content like images and welcoming messages, ensuring user-friendly browsing experiences. By toggling seamlessly, users can access informati
5 min read
How to disable Responsive (mobile) Navbar in Bootstrap 5? Bootstrap 5 Navbar Nav is used to make the navbar according to the requirement of the project like navbar navigation links built on .nav options with their own modifier class. Bootstrap 5 Navbar Responsive behaviors help us determine when the content will hide behind a button but in this article, we
3 min read
How to replace container class in react-bootstrap? The container class provides a responsive fixed-width container. In this article, we will learn How to replace container class in react-bootstrap. We will wrap the outermost content so that the page has margins. Table of Content Using React Bootstrap Container Using Traditional BootstrapSteps to cre
2 min read