Creating a sleek and functional mobile navigation bar with Bootstrap 4

Can anyone help me with creating a fullscreen menu (navbar) for mobile devices? I want the navbar to occupy the entire viewport when the hamburger menu is clicked.

I managed to achieve this by simply adding:

#navbarText{
  height: 100vh;
}

However, the bootstrap animation is not as smooth as the default one. The animation should gradually increase or decrease the div size, like shown in this example:

https://i.sstatic.net/XFf1C.gif

Here is the code snippet:

/*---Standar Hamburger Menu (3 separate elements)---*/
.navbar-toggler {
  border: none;
  background: transparent !important;
}

.navbar-toggler:hover {
  background: transparent !important;
}

.navbar-toggler .icon-bar {
  height: 2px;
  width: 22px;
  border-radius: 1px;
  display: block;
  background-color: #B6B6B6;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
}

.navbar-toggler .icon-bar+.icon-bar {
  margin-top: 4px;
}

/*---Animation menu (create X)---*/
.navbar-toggler.x .icon-bar:nth-of-type(1) {
  -webkit-transform: translateX(3px) rotate(45deg);
  -ms-transform: translateX(3px) rotate(45deg);
  -moz-transform: translateX(3px) rotate(45deg);
  -o-transform: translateX(3px) rotate(45deg);
  transform: translateX(3px) rotate(45deg);
  -webkit-transform-origin: 10% 10%;
  -ms-transform-origin: 10% 10%;
  -moz-transform-origin: 10% 10%;
  -o-transform-origin: 10% 10%;
  transform-origin: 10% 10%;
}

.navbar-toggler.x .icon-bar:nth-of-type(2) {
  opacity: 0;
  filter: alpha(opacity=0);
}
... (more css rules)

If you have any suggestions on how to address this issue, please let me know. You can also check out and edit the code using Codeply.

Answer №1

After reviewing your code, it is evident that you have put in a significant amount of effort. Instead of going through the entire code, I simply added the following CSS:

.navbar-nav {
  height: 100vh !important;
}

This modification should work effectively on any Bootstrap 4 project. By focusing on the highlighted class in the sample code provided below, most individuals can achieve a smooth transition from Bootstrap without the need for JS or extensive modifications.

https://i.sstatic.net/4n7DJ.png

I trust that this information will be beneficial to all who come across it.

Answer №2

Upon analyzing the Bootstrap JS file, I discovered that achieving this effect simply requires making a few modifications, specifically within the 'Collapse' function around line 1100. The necessary changes are as follows:

Object.defineProperty(this._element, 'scrollHeight', {
      writable: true,
      value: $(window).height()-$(".navbar").outerHeight()
});

This code should be inserted after assigning a value to the this._element variable. By setting the maximum height of the '.navbar-collapse' div in this manner, we subtract the height of the .navbar element including the .navbar-brand and any parent padding/margin.

This approach is essential because the 'scrollHeight' property (as well as others) is not editable by default, thus necessitating explicit specification.

The final adjustment involves removing the line

_this._element.style[dimension] = '';

This deletion ensures that when the element has the 'show' attribute (signifying completion of animation), it fills the screen entirely without interference from our defined height.

These minimal alterations yield the desired outcome, although one drawback is losing the convenience of utilizing the CDN for the original file, requiring manual download and modification instead.

To clarify, here is how the file should appear following the modifications:

 ...
 var Collapse = function () {
  function Collapse(element, config) {
    ...
    this._element = element;
    //CHANGE (1)
    Object.defineProperty(this._element, 'scrollHeight', {
      writable: true,
      value: $(window).height()-$(".navbar").outerHeight()
    });
    this._config = this._getConfig(config);
    ...
  }
  ...
  _proto.show = function show() {
    var complete = function complete() {
      ...
      /* CHANGE (2) - Remove
      _this._element.style[dimension] = ''; */

      ...
    };
  };
  ...
 }
 ...

Answer №3

I have discovered an easy solution to tackle this issue.

  1. Simply include another div with id="navbar-content" that contains your navigation links like this:
    <!-- navbar links -->
        <div class="collapse navbar-collapse justify-content-sm-center" id="navbarLinks">
          <div class="navbar-nav text-center" id="navbar-content">
            <a href="#header" class="nav-link active mx-2"><h5>Home</h5></a>

            <a href="#skills" class="nav-link mx-2"><h5>Skills</h5></a>

            <a href="#about" class="nav-link mx-2"><h5>About</h5></a>

            <a href="#projects" class="nav-link mx-2"><h5>Projects</h5></a>
          </div>
        </div>
        <!-- end of navbar links -->
  1. Proceed to your stylesheet and incorporate the following:

    /* ensure the navbar expands to full screen on mobile */
    @media (max-width: 768px) {
       #navbar-content {
         height: 100vh;
      }
    }

Hence, until the medium breakpoint at 768px, the container will expand to fill the entire screen. This method has been tested on Bootstrap 5 and performs seamlessly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing a div element within a loop to showcase content in a horizontal layout

I'm currently working on a project where I need to showcase YouTube links on a webpage, accompanied by brief descriptions. To achieve this, I am utilizing a repeater. Below is the item template I have implemented: <Item Template> <asp:Pane ...

Ensure that when a user clicks back on an image popup, the page stays in its current position

Is there a way to ensure that after closing a popup image, the browser returns to the same position where it was before? Currently, when I close the popup image, the page scrolls back to the top (as indicated by the red square in the example). However, I ...

designing a personalized two-row navbar layout in Bootstrap 4

I have been attempting to design a unique custom navigation bar that I haven't come across anywhere else online. Despite my diligent search for examples or solutions, I have not been able to find what I am looking for. Therefore, I am reaching out her ...

The tab content appears to be hidden or not displaying properly

I'm currently working on a project involving tabs that display content for two different data-targets (which is not an issue). I've implemented a script that automatically switches tabs every 5 seconds. The tab changes successfully, but the tab-c ...

`Manipulating the image source attribute upon clicking`

I need help changing the attr: { src: ...} binding of an img element when a different image is clicked. Here is an example: $(document).ready(function () { var viewModel = { list: ko.observableArray(), showRenderTimes: ...

The date retrieved from the MySQL database is failing to show up properly in an HTML table even after applying the date_format

I am struggling with a piece of HTML code that retrieves data from a database and displays it in an HTML table. The problem is, everything shows up except for the date field. Oddly enough, when I execute the query in the MySQL command line, the date displa ...

Extract the content of a textbox within an iframe located in the parent window

Is it possible to retrieve the value of a text box in an iframe from the parent page upon clicking a button? Below is an example code snippet showcasing the situation: <div> <iframe src="test.html" > <input type=text id="parent_text"> & ...

Retrieve and substitute attributes using preg_replace

I'm looking to add tabs to all the properties listed here, but I am having trouble isolating the properties as $1 is not working in this case. A small tweak would solve this issue... Thank you! $css = <<<EOF body { padding: 0px; ...

Having trouble removing a DOM element with jQuery?

Hey there, I need your help with a jQuery issue I'm facing. I am currently working on cloning a div element that contains a span with a click event attached to it. The purpose of the click event is to remove the newly cloned section from the DOM. Whil ...

Guide to making Bootstrap collapsible panels with full-width content that do not affect neighboring columns

I'm currently working on a responsive grid that displays items with expandable details when clicked. My issue is that when one item expands, the elements next to it collapse down, which is not what I want. I want all items to stay at the top and have ...

The right floating behavior with <li> elements seems to be malfunctioning

I am currently dealing with an issue in my code where it works perfectly in Firefox and IE 8, but in IE 7 the second list item is displaying with some extra space at the top instead of being on the same line. <LI style="PADDING-LEFT: 20px"> ...

How do you implement radio button logic within an *ngFor loop?

When I populate options in a form with radio buttons, I am not seeing isChecked set to true when I select an option. Am I missing something in the following Angular code? app.component.html <div class="form-group"> <label>answerOption</la ...

Styling a modal popup using session storage in CSS

I recently created a modal popup using CSS by following a helpful tutorial that can be found at this link: Now, I am curious about how to store the value entered in a textbox as session data. I came across some code in another tutorial that demonstrated h ...

Continuous horizontal columns

Is there a way to create horizontal columns with inline-blocks, like the method described here, without having vertical gaps between items on the second line due to different heights? I want to eliminate the vertical gaps between the tiles using only CSS. ...

Issue with excessive content causing scrolling difficulty

I've created CSS for a modal dialog, but I'm facing an issue. When the content exceeds the vertical space of the wrapper, the scrolling functionality doesn't work correctly. Although I can scroll, it's limited and I can't reach th ...

Stop the replication of HTML/CSS styles during the extraction of content from a div

Is there a way to prevent the copying of CSS properties, such as font styles and sizes, when content is copied from a div? I want only the plain text to be copied to the clipboard, without any formatting applied. ...

css the vertical column is unable to reach 100% vertical

Check out my website at I need help getting the black column with my thumbnails to extend all the way down on the site. Here is the CSS code for the thumbnails: #thumbnails { position:absolute; top: 110px; width: 190px; height: 100%; ...

How can I export the styling from vite library mode in a React application?

My Vite React TypeScript application features JSX components, module.scss, and global CSS files. Although when I build it in Library Mode, I end up with separate .js, .d.ts, and .css files. However, once I install it in another application, the styling d ...

The HTML code for the base64 image conversion failed to load

I am facing an issue with handling HTML strings containing base64 image tags generated by Summernote. I have a process in place where I load the image elements, convert them to image blobs, upload them to get the image-url, and then update the src attribut ...

Slow mouse hover element highlighting in jQuery is causing issues

I'm currently working on creating a simple chrome extension that allows me to highlight an element when hovering over it with the mouse. I've been trying to implement a feature where the element is highlighted by adding a border shadow, but this ...