Fixating the navbar in Bootstrap

Our website already looks pretty decent. We have a navigation bar in place, a footer placeholder, and various sections populated with some sample content. However, we are not quite there yet. The website’s overall user experience is still a bit edgy and does not yet feel very refined. Take navigation, for instance—while clicking on a navbar link does indeed take the user to the correct section, the navbar disappears once we navigate across the sections. This means that the navbar loses its purpose. It no longer facilitates easy navigation across the different sections of our website. Instead, the user will need to scroll to the top of the page every time they wish to use the navbar. To make the navbar persistent, append navbar-fixed-top to the class attribute of our nav element:

<nav class=”navbar navbar-expand-lg navbar-myphoto fixed-top”>


Now save, refresh, and scroll. Voila! Our navbar now remains fixed at the top of our page. The fixed-top works as follows: the element’s position is set to fixed. This positions the element relative to the browser window (not relative to other elements), meaning that the element will remain in the same place, regardless of the positioning of other elements on the page—that is, fixed navbars are pulled from the normal flow of the DOM and may require custom CSS.

Furthermore, the element’s top is set to 0. This means that the distance between the navbar and the top of the browser window is 0. In addition, fixed-top also changes the element’s z-index to 1,030, therefore ensuring that the element will appear above all the other elements on the page (that is, all elements that have a z-index of less than 1,030).

The fixed-top class is defined in _position.scss and looks as follows:

.fixed-top {

position: fixed;

top: 0;

right: 0;

left: 0;

z-index: $zindex-fixed;


Note: $zindex-fixed is defined in _variables.scss, and its default value is 1030.

Should you desire to fixate the navbar at the bottom of the page, you can use the fixed- bottom class. This class behaves in exactly the same way as the fixed-top class, except that instead of setting the element’s top to 0, it sets the bottom property to 0, thereby ensuring that the element resides at the bottom of the page.

More on navbar styling

If we wanted to quickly change the color of the navbar without wanting to write a whole bunch of custom rules, then we could apply the navbar-* and bg-* classes:

  • navbar-dark: This is used to indicate that the navbar’s foreground color should be adjusted to match a dark background. As such, the rule will apply a white foreground color to all navbar items.
  • navbar-light: This is the opposite of the aforementioned navbar-dark and applies a dark foreground color in order to support a light background.
  • bg-*: This will set the background color to that of the desired context class (we will cover the various context classes later on in this chapter); for example, bg- primary, bg-success, and bg-info; bg-dark mimics an inverted background, setting the background color to #343a40.

Take a look at figure 4.1:

Figure 4.1: The MyPhoto navbar with three different context styles: pg-primary, bg-warning, and bg-danger

Source: Jakobus Benjamin, Marah Jason (2018), Mastering Bootstrap 4: Master the latest version of Bootstrap 4 to build highly customized responsive web apps, Packt Publishing; 2nd Revised edition.

Leave a Reply

Your email address will not be published. Required fields are marked *