Updates to Bootstrap 4 Navbar

I have some simple CSS questions that are giving me a bit of trouble;

How can I make the green background take up the full height of the navbar on hover for a nav-item?

Also, how can I center the nav-item text and add a border-bottom when the navbar is collapsed on small/mobile screens?

Here is the HTML code:

<nav class="js-navbar-scroll navbar navbar-expand-lg navbar-light fixed-top navbar-custom">
      <div class="container-fluid">
        <a class="navbar-brand" href="/">
         <img src="img/logo-design-new.png" alt="JDC Support" style="width: 40px;" />
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggler" aria-controls="navbarToggler" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarToggler">
          <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="about">About</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="photos">Photos</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="collections">Collections</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="careers">Careers</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="contact">Contact</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

Here is the CSS code:

.navbar-custom {
      background-color: #fff !important;
      border-bottom: 2px solid #f1f1f1 !important;
    }

    .navbar-custom .container-fluid .nav-item.active .nav-link,
    .navbar-custom .container-fluid .nav-item:hover .nav-link {
        color: #ffffff;
        background-color: green; 
    }

For reference, here's a link to a Plunker showcasing it: Bootstrap Navbar

Answer №1

Make sure to include jquery before adding bootstrap js. Additionally, apply the following custom css for specific styling:

/* Your desired styles should be entered here */
.navbar-custom {
      background-color: #fff !important;
      border-bottom: 2px solid #f1f1f1 !important;
    }
   
    /* Modify the color of active or hovered links */
    .navbar-custom .container-fluid .nav-item.active .nav-link,
    .navbar-custom .container-fluid .nav-item:hover .nav-link {
        color: #ffffff;
        background-color: green; /* Set a background color for active links */
    }
<link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a6ababb0b7b0b6a5b484f0eaf5eaf7">[email protected]</a>" data-semver="4.1.3" rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
  <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a9b2b6a6b1ba83f0edf1edf2">[email protected]</a>" data-semver="3.2.1" src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9a3b8bcacbbb089fae7fbe7f8">[email protected]</a>/dist/jquery.min.js"></script>  
  <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bf9f4f4efe8efe9faebdbafb5aab5a8">[email protected]</a>" data-semver="4.1.3" src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<style type="text/css">
    .navbar-custom{
      padding-top:0rem;
      padding-bottom:0rem;
    }
.nav-link {
    display: block;
    padding: 1rem 1rem;
}
@media (max-width: 991px){
     .navbar-custom{
      padding-top:0.5rem;
      padding-bottom:0.5rem;
    }
.nav-link {
    display: block;
    padding: 0.5rem 1rem;
} 
.navbar-light .navbar-nav .nav-link {
   text-align:center;
}
.navbar-light .navbar-nav .nav-item {
   border-bottom:5px solid #000000;
}
}
</style>
  <nav class="js-navbar-scroll navbar navbar-expand-lg navbar-light fixed-top navbar-custom">
    <div class="container-fluid">
      <a class="navbar-brand" href="/">
        <img src="img/logo-design-new.png" alt="JDC Support" style="width: 40px;" />
      </a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggler" aria-controls="navbarToggler" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarToggler">
        <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
          <li class="nav-item mr-4 mb-2 mb-lg-0">
            <a class="nav-link" href="about">About</a>
          </li>
          <li class="nav-item mr-4 mb-2 mb-lg-0">
            <a class="nav-link" href="photos">Photos</a>
          </li>
          <li class="nav-item mr-4 mb-2 mb-lg-0">
            <a class="nav-link" href="collections">Collections</a>
          </li>
          <li class="nav-item mr-4 mb-2 mb-lg-0">
            <a class="nav-link" href="careers">Careers</a>
          </li>
          <li class="nav-item mr-4 mb-2 mb-lg-0">
            <a class="nav-link" href="contact">Contact</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>

Answer №2

If you want to add hover effect and center text alignment with a bottom border for small/mobile screens on your nav-item, you can use the code snippet below.

.nav-item:hover {
  background-color: green;
}
.collapse.show {
  text-align: center;
}
.collapse.show .nav-item {
  border-bottom: 1px solid black;
}

You can view an example of this code in action by visiting the following link: https://jsfiddle.net/DharaSumitPatel/8h1cwezq/

Answer №3

Make a few adjustments to your CSS:

<style>
.navbar-custom {
    background-color: #fff !important;
    border-bottom: 2px solid #f1f1f1 !important;
    padding:0px;
    margin:0px;
  }

.navbar-custom .container-fluid .nav-item.active .nav-link,
.navbar-custom .container-fluid .nav-item:hover .nav-link {
      color: #ffffff;
      background-color: green; 
      border:2px solid green;

  }
#navbarToggler.show{
  text-align:center;
}
#navbarToggler.show 
.nav-item{
  border-bottom:1px solid gray;
}
</style>
 <nav class="js-navbar-scroll navbar navbar-expand-lg navbar-light fixed-top navbar-custom">
      <div class="container-fluid">
        <a class="navbar-brand" href="/">
         <img src="img/logo-design-new.png" alt="JDC Support" style="width: 40px;" />
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggler" aria-controls="navbarToggler" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarToggler">
          <ul class="navbar-nav ml-auto mt-2 mt-lg-0">
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="about">About</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="photos">Photos</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="collections">Collections</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="careers">Careers</a>
            </li>
            <li class="nav-item mr-4 mb-2 mb-lg-0">
              <a class="nav-link" href="contact">Contact</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

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

Issues with Converting JSON to HTML Table using JavaScript

Issues with Converting JSON to HTML Table Using JavaScript I've been trying to create a function that will display the contents of a JSON file in an HTML table. However, I keep getting an undefined error. Despite being able to see the data displayed ...

Error encountered while attempting to load JSON data into HTML audio element

I need to incorporate data from a JSON file into an HTML audio tag. While I've been able to extract the desired information from the JSON, I'm facing difficulty loading it into HTML. Here's what I've tried so far: <?php $json = file ...

What are the best methods for creating a responsive website?

Strategies for implementing responsive design on a website ...

How to utilize methods from different pages in Ionic 2

Looking to display the total number of items in an array on a card located on the home screen, but facing issues referencing methods outside of the typescript file they're written in. Trying to extract the array size method and utilize it in a differe ...

Issue with Bootstrap Grid functionality

I've been working solo on a project and it's been smooth sailing so far. However, after making some changes to my code, the grids are not displaying as desired. Instead of three columns, only one is showing up and I can't seem to pinpoint th ...

Tips for creating an inline style with "border: 1px solid #72777d"

For the boxes I applied borders using "border: 1px solid #f2f2f2" as depicted in the image. The issue is that with 1px solid borders, they overlap each other. Is there a way to use inline borders with CSS instead? Thank you in advance ^_^. border: 1px s ...

Tips for assigning a string value to an <li> element with Jquery

I have a database table structured as follows: ID name aa1 Tom bb2 Jack cc3 Mike To display this data in an HTML format, I am using Ajax to pass the values. $.ajax({ ... success: function (data) { $.each(data, f ...

Enhance Bootstrap: adjust button width based on fluid text width

Incorporating Bootstrap 5 framework, I have designed a card that showcases brand information along with a navigation button on the right side. https://i.sstatic.net/25QVr.png When the description is brief, the button aligns perfectly in a single line. Ho ...

Is it possible to insert a button into a specific column of an ngx-datatable within an Angular 8 application?

I am having trouble with this particular HTML code. <ngx-datatable-column name="Option" prop="option" [draggable]="false" [resizeable]="false [width]="250"> <span> <button class="optionButton" type="button" data-to ...

Issue with php's mysql_fetch_assoc() function

Seeking to fetch data from a function that holds my query. The retrieved data is sent back to the main page for printing using a while loop but encountering an error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given ...

Scrolling horizontally to display dynamic columns based on a search query in AngularJS

I am facing a challenge with implementing search functionality in a table that has multiple dynamic columns with a horizontal scrollbar. The goal is for users to be able to search for a specific column name or data, and if a match is found, the scrollbar s ...

Ways to apply distinct styles to various ids and common classes

When dealing with multiple IDs such as id1, id2, and id3 that share common classes like .selectize-input and .select-dropdown, it can become cumbersome to set styles individually for each ID. Instead of writing: #id1 .selectize-input, #id2 .selectize-inp ...

Guide to update the source of several images simultaneously based on the chosen list item

My website includes a variety of images and thumbnails showcasing a car in different colors. There are four color options available, along with a selection list to choose a color. I would like the ability to switch between colors in the list and have 4 out ...

The functionality of aria-expanded is not functioning properly when set to false

Having trouble with the bootstrap accordion feature. When the page loads, the accordions are expanded by default instead of being collapsed. I want them to start collapsed and then expand when clicked. Here is the HTML code: <!DOCTYPE html> &l ...

Implementing an alert box upon redirect with PHP

This question is pretty straightforward. I have a form on my index.html page and PHP code to submit that form to an external URL, then redirect back to the index: <?php Echo "<style>body{background:#3e4744}</style>"; Echo "<style>h2{ ...

Is there a way to transform an HTMLCollection into an array without depleting it of its elements?

I've been attempting to transform a collection of 4 divs in HTML into an array, but all my efforts seem to lead to the array becoming void. <div class="container"> <div class="shape" id="one"></div> <div class="sh ...

The issue of the horizontal navigation bar not functioning properly persists when a specific width

I am currently working on building a horizontal navigation bar using CSS. What puzzles me is that when I specify a width for my ol tag, the horizontal float function does not seem to cooperate. However, if I remove the width property, everything works jus ...

When dynamically adding input fields in Bootstrap, there is a smaller gap between inline inputs

When adding a new list item dynamically in a modal using jQuery append, the spacing in the first li element seems to have a larger gap between the input fields compared to the rest that are added later. Even after checking the developer tools and confirmin ...

Implementing Enter key functionality to add items to a Todo list using pure DOM manipulation

var listLis=document.getElementById('list'); const addbutton=document.querySelector('.fa-plus') const inputBar=document.querySelector('.show') function showInput(e){ inputBar.classList.toggle('show') } addbutt ...

Display the AJAX reply within a designated div based on the selected value

I currently have two dropdown menus where the options in the second menu depend on what is selected in the first one. The behavior I want is for the second menu to update as soon as the user makes a selection in the first one, without needing to submit the ...