One of my friends requested me to post this article. Though the problem was easy to solve for me, it was difficult for him as he has just plunged into the Bootstrap world. It is exciting to get things work in the first attempt, but that is not the case when things are not configured correctly and this is what happened with my friend.
So, my friend wanted to create a sample Bootstrap application and to see how responsive website looks. He created an Angular (not Angular JS) project using Angular CLI. The setup was ready and he could see the application running fine on Chrome. Then he visited the Bootstrap CDN to reference the bootstrap.min.css and bootstrap.min.js minified files and his index.html file looked something like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Azilen</title>
<base href="/">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root>Loading...</app-root>
</body>
</html>
Now, to save time, he opened Bootswatch and copy pasted the code of NavBar in index.html's body section. Then, he ran the command ng serve --open from the command prompt and the application fired up in browser with the header as shown below:
And now he clicked on Dropdown menu with the expectation that it would show up, but unfortunately it didn't pull out. What was he missing in index.html file? It was JQuery file. Import the file or it can be referenced through JQuery CDN . As shown below, just copy the url and paste it in index.html.
And now we can see that the Dropdown menu is working.
Great! But now when he tested his original purpose of using Bootstrap, that is the responsiveness of the page by shrinking the window and clicking the bar icon, there was no effect. It was shocking. So, what is missing now when we have all javascripts and CSS references in place? Culprit is the data-toggle="collapse" property in the button.
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" aria-expanded="false" data-target="#bs-example-navbar-collapse-1">
Once the toggle property was added, the bar button started functioning. Hurray!!!
Comments
Post a Comment