In a nutshell, the <li> element inside the .navbar should have a

Previously, in Bootstrap 3, there was a method to achieve the following:

https://i.sstatic.net/0fkla.png

This involved adding a solid background color (orange in this case) to an <li> element within the .navbar

An example demo can be seen here: https://jsfiddle.net/p1vm4aeo/

The only custom CSS used was for the orange background, everything else followed Bootstrap 3 standards:

.orange-bg {
  background-color: orange;
}

The markup looked like this:

<nav class="navbar navbar-fixed-top navbar-default" role="navigation">
    <div id="navbar-collapse-1" class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
            <li class="dropdown orange-bg">
                <a href="#">Foo</a>
            </li>
        </ul>
    </div>
</nav>

However, with Bootstrap 4, achieving the same effect seems more challenging due to the height of the <li> inside .navbar not filling the entire navbar. The result is shown below:

https://i.sstatic.net/IM1Ae.png

The demo for Bootstrap 4 can be accessed here: https://jsfiddle.net/20ehqxsy/

The key difference in the markup lies in using the appropriate Navbar classes for Bootstrap 4 instead of 3 (.navbar-expand-lg for example):

<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light navbar-default" role="navigation">
  <div id="navbar-collapse-1" class="collapse navbar-collapse">
    <ul class="nav navbar-nav">
            <li class="dropdown orange-bg">
                <a href="#">Foo</a>
            </li>
        </ul>
  </div>
</nav>

Is there a way to replicate the same effect achieved with Bootstrap 3 while using Bootstrap 4?

Answer №1

One way to create a standard navigation bar is by using the .navbar class along with a responsive collapsing class like .navbar-expand-xl|lg|md|sm which changes the layout based on screen size.

To achieve the desired result, it is recommended to utilize Bootstrap's utility/spacing as well as incorporating the nav-link class within li elements.

This method provides a clean solution without needing to override Bootstrap's default CSS styles.

.orange-bg {
  background-color: orange;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar p-0 fixed-top navbar-expand-sm navbar-light bg-light navbar-default" role="navigation">
  <div id="navbar-collapse-1" class="collapse navbar-collapse">
    <ul class="nav navbar-nav">
      <li class="nav-item dropdown orange-bg">
        <a class="nav-link" href="#">Foo</a>
      </li>
    </ul>
  </div>
</nav>

Answer №2

The .navigation section in your code includes:

.navigation {
    ...
    padding: .5rem 1rem;
}

To adjust this, simply change the padding to 0 and apply the same setting to a related element within your navigation; for demonstration purposes, I used .dropdown as an example but you can substitute it with .menu-items > li, etc. :

.dropdown {
    ...
    padding: .5rem 1rem;
}

.navigation {
    padding: 0;
}

This modification will achieve the desired outcome.

You can refer to this Fiddle for reference: https://jsfiddle.net/ab3c4def/

Answer №3

Simply add padding: 0 to the nav element since it currently has a padding of 16px. Here is the tested solution:

If you want to make your navbar responsive and collapsible, you can utilize .navbar-expand-xl|lg|md|sm. Check out the documentation on Navbar

nav {
  padding: 0px!important;
}

.orange-bg {
  background-color: orange!important;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>

<body>
  <nav class="navbar fixed-top navbar-expand navbar-light bg-light navbar-default" role="navigation">
    <div id="navbar-collapse-1" class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="dropdown orange-bg">
          <a href="#">Foo</a>
        </li>
      </ul>
    </div>
  </nav>
</body>

</html>

https://i.sstatic.net/jEFsI.png

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

What is the significance of keyframes in CSS animations at different percentages

I'm facing a challenge with the code below that is intended to make objects float from left to right in the background. However, once they go off-screen, a vertical scroll bar appears despite the overflow-y: hidden attribute added to the class. I atte ...

By utilizing Angular's @output decorator, data can be passed from child components to their parent counterparts

I have a situation where I am passing data from a child component to a parent component, and I need to automatically check if the data is empty or has a value. This code snippet is from my login.component.ts - child TypeScript file: @Output() update = ne ...

How can I create the effect of text changing color automatically after a specified time period has elapsed

I am currently dealing with a timer that is functioning properly, but I have a specific CSS inquiry. Essentially, what I would like to achieve is when the timer hits 30 seconds, I want the numbers to change to red color. $(function () { var $startTimer = ...

Combining fixed pixel width with the max-width property in CSS

My container has a nested <p/> tag serving as the message text. The container is set to have a width of 200px, with a max-width of 600px to allow for expansion if the message length exceeds the initial 200px. However, despite these settings, the div ...

Angular ng-repeat can sometimes lead to annoying flickering on the webpage

I have a code snippet that displays a list of thumbnails: <div class ="channel" ng-repeat ="channel in UIModel.channels" ng-class ="{evenchannel: ($index % 2) == 0, oddchannel: ($index % 2) == 1}"> <img class="channel-img" ng-src ="da ...

What is the best way to add a darker tint to the background behind my overlay panel in jQuery

After grabbing the panel code from the jQuery Mobile website and integrating it into my file, I am interested in changing the appearance. I want to either darken or blur the background content when the panel is displayed. I attempted to use filter:blur(5px ...

Tips for Aligning Form Elements in the Middle using Bootstrap

What could be the issue? I can clearly see the container border, indicating the space available for my work. However, when I insert the row (div) and offset (div), the content gets left-justified. <div id="page" class="container" style="border:soli ...

Tips for adjusting this CSS to be compatible with Internet Explorer

This CSS code I have was created specifically for Firefox compatibility. /* Green header */ .container { background: linear-gradient(#5FA309, #3B8018); position: relative; display: inline-block; padding: 0 20px 0 10px; width: 270px; ...

unitary incline division assemblage

I noticed a particular element in the design that is currently displayed with an image (png). However, I am facing limitations due to needing to make it adaptive. Can a gradient be used as a sub-element instead? background-image: linear-gradient(to left, ...

When attempting to assign a variable in my ngModel, what issue could be causing a problem?

I am facing an issue while trying to implement ngModel in my code. The code is not executing properly and an error is being displayed. Below is the code snippet: contact-form.component.html: <div class="container"> <form> <div class ...

What is the best way to make a DIV box span the entire webpage without overflowing to the left?

I am currently working on a client's website and running into some issues with the layout of the booking page. The address is supposed to be on the left side, the contact form on the right, and both should be contained within a white box that fills th ...

Customize your Bootstrap table with unique ions on specific rows

I've successfully managed to display a bootstrap table using JSON output. However, I'm facing an issue trying to figure out how to add a button in the last column of each row. <table class = "table table-hovergrey" data-search="true" data-to ...

Nextjs Image Optimization: Accelerating Web Performance with Images

Encountering an issue when trying to display multiple images in nextjs. I noticed that without setting position: relative for the parent element and position: absolute for the child element, along with specific height and width values for the Image itself, ...

What is the best way to resize an image in HTML based on a percentage of its height?

Within my HTML code, I have a PNG image that has been adjusted in size using CSS: .adjust-image { border:1px solid #021a40; display: block; width: auto; max-width: 100%; max-height: 1000px; } .img-container { position: relative; } ...

display the chosen choices from the jquery tool

I have implemented a Jquery movie seat selection plugin on my jsp website successfully. It allows users to select seats with ease. However, the issue I am facing is that due to my limited knowledge of Jquery, I am unable to display the selected seats by t ...

What is the best way to take control of DOM alterations?

In the project I'm currently working on, the client has a modal box with its display property set to none. Upon clicking the CTA button, it triggers the application of the fadein and fadeout classes. This results in a change from display:none to displ ...

Tips for incorporating a tooltip into a disabled selectpicker option

A selectpicker I have is filled with various languages, listed as `li` tags within a `ul` tag. You can see an example in the image provided below. https://i.stack.imgur.com/dsg66.png It's noticeable that the `+ Add Language` option in the selectpick ...

Exploring the utilization of server-side Web Sockets functionality

Recently, I've been diving into the world of Web Sockets and I'm eager to understand how server-side implementation works. While I feel comfortable with writing client-side code using onmessage() in HTML5, JavaScript, etc., I find myself unsure a ...

A div element that automatically adjusts its width to fill the available horizontal space, and is then re

div</code> elements next to each other horizontally. The right div should adjust its size based on its content up to a set maximum width, while the left one should occupy the remaining space. I have successfully achieved this layout by floating the ...

Issue encountered while trying to play sound on PhoneGap

Greetings! I am attempting to integrate sound into various buttons and components of a game. I found this code on the PhoneGap website which works perfectly fine during testing: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "h ...