Bootstrap navigation

Bootstrap navigation is one of the most widely used navigation templates and there are a lot of practical points that help get familiar with frontend. In this post, we are going to have a close look at code structure, navigation toggling mechanism, browser rendering process, browser inspection, and more.

how navigation bar is toggled

responsive Bootstrap navigation toggling example
responsive Bootstrap navigation toggling example


By default, the Bootstrap navigation bar toggles (show or hide/collapse) its links on device width changes. As seen in the example above, it shows all the links horizontally at desktop/laptop width, whereas it hides #link1 and #link2 as well as displaying the hamburger button at tablet/mobile device width.
What we want to have a particular look at is the toggling mechanism at tablet/mobile width and the following highlights the Bootstrap navigation codes where relevant.

Bootstrap navigation code
BS-nav code


Look at the first boxed code snippet above.
The key role of the <button> element, represented as the hamburger , is to toggle a target element’s display state between ‘show’ and ‘collapse’ every time user clicks , and let’s check what attributes in the <button> element are set to carry out it.

Now, let’s move on to the data target <div class="collapse navbar-collapse" id="navbarNav"> and see what elements are in it referring to the second boxed code snippet above.

<div class="collapse navbar-collapse" id="navbarNav">
  <div class="navbar-nav mr-auto">
    <div class="nav-item">
      <a class="navbar-brand nav-link" href="#">#link1</a>
    </div>
    <div class="nav-item">
      <a class="navbar-brand nav-link" href="#">#link2</a>
    </div>
  </div>
</div>


Taken all together, say you are using a mobile, and Bootstrap navigation bar will display the hamburger button hiding #link1 and #link2.
Whenever you click on , it toggles a display state of the data target <div id="navbarNav"> between ‘show’ and ‘collapse’ by selecting the opposite display state to current state, meaning that the collapsible contents #link1 and #link2 in the data target apply to the same effect automatically.

Browser inspection

All modern web browsers support an inspection (developer tool) that enables you to view, edit, or inspect source code of any websites within browser. FYI, you can open an inspection page of any website by typing Ctrl + Shift + i or press F12 (Windows OS).
Using Chrome’s browser inspection, we are going to inspect Bootstrap navigation source code in order to verify runtime toggling behaviors at tablet/mobile device size where hamburger button appears.

As seen in the capture below, the entire page width is less than 992px (tablet/mobile width) and, as a result, the Bootstrap navigation bar responsively shows as well as collapsing #link1 and #link2 except Home.

browser inspection (Chrome)
browser inspection (Chrome)


Now, let’s zoom into codes in <nav>; the <button> element () and the target element <div> with id="navbarNav" where dynamic changes will apply on toggling .
The following attributes in <button> and in the target element <div> with id="navbarNav" are where we can verify an interaction between Bootstrap HTML code and its API at runtime.

browser inspection (Chrome)
browser inspection (Chrome)


Look at the picture below.
On toggling , the attribute aria-expanded="false" becomes aria-expanded="true" in <button>.
At the same time, the class show is added into the target element; <div class="navbar-collapse collapse show" id="navbarNav">.

browser inspection (Chrome)
browser inspection (Chrome)


On toggling again, here’s what happens in <button>

At the same time, the existing class show is removed from the target element; <div class="navbar-collapse collapse show" id="navbarNav">.

browser inspection (Chrome)
browser inspection (Chrome)


As proved via Chorme browser inspection, the Bootstrap navigation source code instantly gets updated interacting with its API whenever a toggling is detected.

Browser rendering process

What do you think is browser rendering?
In a nutshell, when you visit a webpage, web browser reads the source code in HTML/CSS/JS and turns the codes into a viewable page in the web browser. That process is called a browser/page rendering.

Now, let’s verify how a web browser renders a page based on the source code we’ve gone thtough in the previous headings.

intex.html (full source code link)

Internal CSS code

<head>
  <!-- Bootstrap css reference -->
  <link 
    rel="stylesheet" 
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" 
    integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" 
    crossorigin="anonymous">
    
  <!-- Bootstrap js reference -->
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

  <!-- internal css -->
  <style>
    body {
      font-family: sans-serif;
      background-color: #fff6df;
    }

    .myclass {
      background-color: #ffffff;
      width: 600px;
      height: auto;
      margin: 0 auto;
      padding: 20px;
    }

    footer {
      background-color: #fff6df;
      width: 600px;
      height: auto;
      margin: 0 auto;
      padding: 20px;
    }
  </style>
</head>

HTML codes (element signatures in <body> only)

<body>

  <div class="myclass mt-5">

    <!-- bootstrap nav -->
    <nav class="p-3 container navbar navbar-expand-lg navbar-light"></nav>

    <!-- lorem itsum placeholder -->
    <div class="text-center mt-3 mb-5"><h5></h5></div>
  
  </div>

  <!-- footer -->
  <footer class="text-center"></footer>

</body>

<div class="myclass"> within <body> is the main UI container having several UIs (inner elements) and is set with width: 600px making it fit for mobile device size, and here’s the rendered page in Chrome browser.

page rendering example in Chrome browser
page rendering example in Chrome browser


As expected, the main UI container <div class="myclass"> is rendered with its width at 600px as part of the entire page width in the browser, regardless of any width changes at device level. That means, it keeps its width looking the same on all devices.

Now, let’s briefly go through the navigation toggling behavior on device size changes.
The sample source code references the Bootstrap library in <head> of index.html and only uses the navigation template in <body> without any further codes, which means the navigation toggling has nothing to do with any UI component’s width (e.g., the main container <div class="myclass">) defined in the HTML document, but has to do with the default page width in the browser by device size.

As seen on the bottom of the picture above, the navigation toggling automatically applies when the page’s max-width in the browser is at 991px by default. Consequently, the hamburger appears and the links are hidden (collapsed) as well as being toggled between ‘show’ and ‘collapse’ every time clicking on as long as the page’s max-width is less than 992px.
What we can see from these behaviors is that Bootstrap seems to deliver a tablet/mobile-first approach so that the navigation toggling can loosely be operated on all devices at average responsive level.


convert your manuscript to epub (fiverr gig @whatthehekkist) convert your manuscript to epub (fiverr gig @whatthehekkist) dedicated assistance to your jekyll website, blog, portfolio, or project (fiverr gig @whatthehekkist) dedicated assistance to your jekyll website, blog, portfolio, or project (fiverr gig @whatthehekkist)



What The Hekk is HTML/CSS/JS? a complete guide to creating a web app in 5 min from scratch!

Kindle Edition

What The Hekk is HTML/CSS/JS? a complete guide to creating a web app in 5 min from scratch!

PDF






You May Also Enjoy

  • Web Dev

    how to use git

    2023-04-09

    Git is a version control system where you can upload, update, and track the source code of your project. Also, you can easily deploy codes in your Git repository to a deployment/hosting platform. There are so many benefits from...

  • Web Dev

    Live CSS trainer v1.0

    2023-06-22

    <h1>validate your css codes LIVE</h1> <h1>without auto-completion</h1> <p>    ✔️ write css code manually!    ✔️ example #1: type   * {color: blue;}    ✔️ example #2: type   html {background-color: grey;}    ✔️...

  • Web Dev

    what is if-else statement?

    2023-05-14

    if-else statement is one of the most widely used conditional statements in most programming languages to execute a certain action in (a task of) a program. # what is if statement? In JS, you can do a conditional test...

  • Programming

    Image crawling with Python on Chrome browser

    2022-07-09

    Step by step insturction ## 1. Install selenium and beautiful soup on terminal ```python pip install bs4 pip install pip install selenium ``` ##...

  • Web Dev

    what is web and HTML/CSS/JS ?

    2023-04-19

    # What is Web? Web (World Wide Web; WWW) is a vast online information system you can write, read, watch, or consume resources such as documents, media, etc accessed by web browser on the Internet. You can think like the...

  • Web Dev

    Batch Image Loader

    2023-08-20

    Features lazy load batch (bulk) images zoom in an image on click load more images on every 100 entry skip broken or blocked image source URLs (src) Tech Stack Frameworks: Jekyll (Ruby) & Liquid template Frontend: HTML/CSS/JS & Jquery Data:...