Creating Underlined Navbar Nav Items in Bootstrap 4

Below is the code snippet for a fixed navbar example from Bootstrap 4.1, but I'm using Bootstrap 4.5 and JQuery 3.5.1 which were installed via NuGet:

<nav class="navbar navbar-expand-lg navbar-dark fixed-top bg-dark">
  <a class="navbar-brand" href="#">Fixed navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarCollapse">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>

  </div>
</nav>

I am trying to style the navbar items differently when collapsed (vertical display) compared to when expanded horizontally. Additionally, there seems to be an issue with resizing the navbar while keeping the styling consistent.

When toggling between collapsed and expanded views of the navbar, the padding and margins set for various elements are affecting the smoothness of the transition animations.

If you have any insights or suggestions on how to improve this behavior, I would greatly appreciate your input.

Update:

Apologies for missing some key details earlier. I followed instructions from a tutorial to create a shrinking effect on scroll for my navbar, but noticed that the logo transition was not as smooth as expected. To fix this, I added the following CSS transitions:

.navbar-brand { transition: all, 0.5s ease; }
.shrink .navbar-brand { transition: all, 0.5s ease; }
.navbar-brand img { transition: all, 0.5s ease; }
.shrink .navbar-brand img { transition: all, 0.5s ease; }

I also implemented a SASS Mixin to handle smoother transitions across different browsers, similar to the approach outlined in this article.

Your suggestion regarding media queries was spot on. However, the initial implementation on the navbar-brand element wasn't ideal, so I made some adjustments based on your recommendation to achieve the desired results:

@media screen and (max-width: 991.88px) {
.navbar-nav .nav-link {
    padding-left: 15px
}

.navbar-nav .nav-item {
    border-bottom: 1px solid #555;
}

.shrink .navbar-nav {
    margin-top: 10px;
}

.shrink .navbar-nav .nav-link {
    margin: 8px 0;
    padding-bottom: 11px;
}

Thank you for your assistance and patience. Once allowed by the platform, I will mark your response as the solution. Your help has been invaluable!

Answer №1

Here are a few pointers to consider:

  • If you only require the styles for vertical orientation (like a burger menu), include a media query.
  • Avoid using margin-top on the first item and instead, apply margin-bottom to the brand element.
  • In my code example, there is no noticeable choppy behavior.

Check out the snippet below:

@media screen and (max-width:991px) {
  .navbar-nav .nav-link {
    border-bottom: 1px solid #555;
    padding-left: 15px
  }
  .navbar-brand {
    margin-bottom: 30px
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>

<body>


  <div class="container">

    <nav class="navbar navbar-expand-lg navbar-dark fixed-top bg-dark">
      <a class="navbar-brand" href="#">Fixed navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
      <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link disabled" href="#">Disabled</a>
          </li>
        </ul>

      </div>
    </nav>

  </div>

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

Images within links appear with blue lines underneath them

Currently, I am in the process of adding links to a footer by using <img> tags with SVG images. However, I am encountering an issue where there are small blue lines under the icons. After searching online, it seems that you need to add this CSS code: ...

Can anyone provide a solution for the issue with bootstrap utilities?

Currently, I am using Vue3 with Vite and attempting to integrate Bootstrap into my project. While I have managed to implement most of it successfully, I encountered a significant error when trying to override Bootstrap-related variables. Can anyone offer a ...

The excessive scrolling behavior of Chrome becomes apparent when a fixed division is located at the top of the viewport

I'm back. After noticing a fixed division issue at the top of the viewport, I discovered that Chrome was scrolling too much. To test this behavior, I've created a page where you can see how various browsers handle scrolling, particularly Chrome ...

Paragraph opacity adjustments can alter both the text and background opacities

Is there a way to change the opacity of only the background of a paragraph without affecting the text? When I try to adjust the opacity, it changes both the text and the background. How can I resolve this issue? HTML <div id="opener"> <p id="in ...

nyroModal automatically adapts its dimensions according to the size of the webpage, not the size of

A situation has arisen with a link that opens an image using nyroModal. While everything is functioning correctly, the Modal window appears in the center of the page instead of aligning with the middle of the browser window. As a result, only the upper por ...

I'm looking for a way to implement an interactive auto slideshow in JavaScript that allows users to manually

Having spent a lot of time studying auto slideshows, I'm facing an issue where clicking on the bullet to show the next image results in the image disappearing suddenly. Initially, I thought the problem was with using addEventListener events, so I swi ...

What are the best practices for utilizing intro.js effectively on mobile devices?

Description When using introjs on mobile devices with a screen width of less than 600px, the tooltip ends up covering the element. When I hold the device horizontally, the tooltip adjusts its position accordingly. I attempted to apply custom CSS to the too ...

The CSS classes from Bootstrap are failing to function properly despite my best efforts in

I have been experiencing issues with the row and col classes in Bootstrap. <!DOCTYPE html> <html lang="en> <head> <meta charset="utf-8> <meta http-equiv="X-UA-Compatible" content="IE-edge> <meta name="viewport" content=" ...

The positioning of the button was off-kilter in Firefox

app.component.html: <div class="input-group col-12 d-flex align-items-baseline pb-3"> <p class="mb-0 pb-3 col-12">Verify....</p> <input type="url" class="form-control" id="validationC ...

Closing the modal by simply clicking outside of it's boundaries

Is there a way to close the modal by clicking outside of it using pure JS or AngularJS? I'm struggling to figure out how to implement this functionality. Any assistance would be greatly appreciated. View Fiddle .modalDialog { position: fixed; ...

Maintain the transformed :after element within the designated target in CSS

Having trouble with menu styling. This is how the menu looks: https://i.sstatic.net/pqzBm.png Styling for each element is as follows: .gallery-navi-element { position: relative; float: left; text-align: center; font-size: 1.7em; pad ...

Is there a way I can convert all relative image paths to absolute paths directly within a CSS file?

I have been attempting to convert all relative background-image paths into absolute paths, but I am facing difficulties. The current code I am using only replaces the last image path instead of all. Within my code, I am utilizing a path resolver function ( ...

The text alignment in Internet Explorer is off

After testing this website on both Firefox and Chrome, the alignment in the "Releases" section appears to be correct: However, when viewed in Internet Explorer, some of the text seems to be aligned in the center for unknown reasons. I have been struggling ...

Showing text and icons side by side in the mobile view

How can I align individual icons (created with WordPress) and text on the same line? It's displaying fine on desktop screens but not on mobile devices. Can someone please check the image attachment and suggest a CSS solution? Thank you! Best regards ...

Design a customized menu using a JavaScript JSON array

Looking to generate a menu using JSON Array data Here is the JSON Array: [{ "page_id":"102608802958096849","title":"Submenu 1 1","page_order":1,"parent_id":"305280635460611248","layout":"header","page_url":"submenu-1-1.html" },{ "page_id":"207782 ...

Measuring the test coverage of Jest for evaluating the useEffect that modifies the styling of an HTML element

As a newcomer to Jest and React, I am currently working on developing a scroll button component in React using the useEffect function. Here's a snippet of my code: useEffect(() => { square && (square.style.transform = `translate ...

PHP submitting form saves zeros

I have encountered an issue with submitting a form entry provided by the user. Instead of storing the actual values entered by the user, the database is recording 0 as the input. I require assistance to rectify this problem. Below is my code snippet: & ...

What is the best way to prevent the body from scrolling when scrolling on a fixed div without making the body's scroll bar disappear?

Is there a way to prevent the body from scrolling while I scroll on a fixed div? I attempted using overflow:hidden for the body, which stops scrolling but causes the page to shake when the scroll bar disappears. Is there a solution that allows me to keep ...

The enigmatic appearance of CSS border-image using a transparent image creates a visible edge when viewed

My website, located at , features a unique design element created using the CSS border-image property and a .png image with a transparent edge, giving the appearance of torn paper on the divs. However, I have encountered an issue specifically on my Android ...

Are websites built with HTML and JavaScript considered secure in today's digital age?

In the scenario where I create a website using HTML5, Javascript, and CSS3 with no forms or input fields other than mouse clicks on links, as well as no logins, messaging, or comments - will this site still be vulnerable to attacks? Recently, my hosting s ...