I am new to coding. I am unable to create a navbar icon that changes on small screen when it is clicked.
I have tried using a bootstrap navbar, when I click the navbar on small screen, it only shows the navbar list but the icon doesn’t change.
actual result
I expect to have the navbar icon to change when clicked on small screen as in the image below.
expected outcome
Here is my code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Office <span class="sr-only">(current)</span></a
>
</li>
<li class="nav-item active">
<a class="nav-link" href="#"
>Windows<span class="sr-only">(current)</span></a
>
</li>
<li class="nav-item active">
<a class="nav-link" href="#"
>Xbox<span class="sr-only">(current)</span></a
>
</li>
<li class="nav-item active">
<a class="nav-link" href="#"
>Support<span class="sr-only">(current)</span></a
>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">All Microsoft</a
>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li>
<a href="#" class="user"><i class="fas fa-search"></i></a>
</li>
<li>
<a href="#" class="user"><i class="fas fa-shopping-cart"></i></a>
</li>
<li>
<a href="#" class="user"><i class="far fa-user-circle"></i></a>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>
I need help with this, I am new to Dev and Stackoverflow.
3
Answers
It’s not a default behavior of bootstrap navbar. Add cross icon in your html code near the
navbar-toggler-icon
For example:
Add some styles for it:
And add class
collapsed
for<button class="navbar-toggler navbar-toggler-right">
by defaultFull example:
To change the navbar icon when it is clicked on small screen, you can add some JavaScript to toggle a class on the navbar-toggler-icon element.
This is not supported by default in Bootstrap 4. You will have to write some additional CSS to display the respective icons when the
.navbar-toggler
is or isn’t.collapsed
.