Guide to aligning navbar links at the center using Bootstrap 4

I am struggling to center the links in my navbar. I have tried using mx-auto and text-center but nothing seems to work. Here is my code:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark text-center">
  <div class="container text-center">
    <a class="navbar-brand text-center" href="#"></a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home
          </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">About</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

Answer №1

By placing the mx-auto in the ul element, you can center the links within it.

<nav class="navbar navbar-expand-lg navbar-dark bg-dark text-center">
  ...
      <ul class="navbar-nav ml-auto mx-auto">
        ...
      </ul>
    </div>
  </div>
</nav>

If you'd like to see an example, check out this CodePen link

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

The HTML webpage is optimized for desktop and tablet viewing, but has difficulty displaying correctly on mobile phones

Just starting out with HTML and created a website using HTML and CSS that is now live. It looks good on desktop and tablet (iPad) but not so great on mobile devices. Tried different solutions, including viewport settings, to fix the issue with no success. ...

Why does Chrome DevTools evaluate JavaScript before recalculating styles?

According to an article, CSS is considered render-blocking, so JavaScript will evaluate after constructing a CSSOM (also known as recalculating styles in dev tools). However, it appears in Chrome dev tools that JavaScript is evaluated before CSSOM. Why is ...

I'm working in JavaFX and I have several line charts that I need to customize individually using CSS styling

Is there a way in JavaFX to style different charts individually with CSS without having the styles override all of them? I've noticed that when I change the CSS for one chart, it affects the others as well. How can I write CSS code that only applies t ...

Can the CSS property of a class be altered in real-time with jQuery?

I am working on a project where I have a predefined CSS style in one class. I am currently attempting to load that page as an IFRAME on another website. My goal is to modify the CSS property of that specific class from the loaded site using jQuery. Unfor ...

How can I format and send an email message using HTML?

I'm currently working on a form and encountering an issue. HTML does not natively support sending emails without a specific email program installed. Is there perhaps a way to send a message that can be converted into an email? Are there any services a ...

Navigating through images via opacity changes in HTML

Is there a way to create a smooth transition effect between pictures in HTML without reloading the page? I have a series of images that I want users to navigate through by clicking a "Next" button, and I would like each image to fade into the screen. Are ...

Glitches in the fluid design of ie7

After creating a prototype layout for my latest design, I noticed that resizing the window in ie7 causes the content area to drop below the sidebar. This issue doesn't occur in other browsers, including ie6. Check out the demo here I need help ident ...

Navigating through tabs on a mobile device by scrolling sideways

Is there a way to create a dropdown menu with tabs in a bootstrap site where the icons extend beyond the width of the menu, allowing mobile users to swipe horizontally? Check out the code here: https://jsfiddle.net/6yw6rp02/1/ This is the current code s ...

Implementing WordPress link pagination for displaying only the next and previous buttons

I'm new to WP and need some guidance. I want to display a list of images in a post, with the ability for users to click next and previous to cycle through them like separate pages. To split the post into multiple pages, I added this code: <!- ...

Position the colored div on the left side of the page

Here is the CSS I am currently using... <style type="text/css"> .widediv{width:1366px;margin-left:auto;margin-right:auto} </style> This CSS code helps me create my webpage : <body><div class="widedivv"> <!-- Content go ...

Chrome experiencing conflicts with backstretch when using multiple background images

In order to dynamically apply fullscreen background images in a WordPress site, I am utilizing Backstretch.js along with a custom field. Everything is functioning properly for the body's background image. However, there seems to be an issue with anot ...

illuminate selected row in datatable using a jquery plugin

My data is displayed using the jQuery plugin DataTable, and everything appears to be working well. I am retrieving my data from PHP using AJAX. However, I encountered an issue when trying to highlight the row that was hovered over while navigating around ...

Are box shadows displaying different hues across various web browsers?

Yesterday, I came across a minor issue with my website. The Box-Shadows appear differently on various browsers. Previously, it was red on Firefox and IE, but now it looks completely different on: Opera: Orange Chrome: Violet Safari: Blue Why is that hap ...

A button that is positioned with a low z-index may be unresponsive to

I am working on aligning an image and a button next to each other so that the image slightly overlaps the button. However, I have noticed that in doing so, the button becomes unclickable. .button{z-index:-1!important;} .image{z-index:1!important;} Any s ...

The functionality to open the menu by clicking is experiencing issues

I'm attempting to replicate the Apple website layout. HTML structure: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" conte ...

What are the steps to address a bug in Bootstrap carousel?

The final element of the slider isn't displaying correctly and causing issues with the entire carousel. Strangely, if I remove the third item from the carousel, it looks good even though the last item is missing. How can this bug be resolved? Bootstra ...

Images are overlapping within nested columns

After inspecting the image provided, I am puzzled by why the images are overlapping each other. Despite my attempts to resize the images and apply the class="img-responsive", they still overlap. The column specified in the code snippet below is p ...

Ways to reduce the size of images in Bootstrap 4

Is there a way to adjust the height of the images to make them fit better on the page? Here is an example of what I'm trying to achieve: https://i.sstatic.net/BMlWl.jpg I attempted to use custom CSS to modify the height, but it ended up making the im ...

Positioning an absolute div inside a relative div within a v-for loop while using VueJS and Element UI

I am experimenting with rendering a list of <el-card> items using a transition-group. Each card has a front and back side that flip when a button is clicked. To achieve a smooth flip transition, I need to apply the style: position: absolute; top: 0; ...

Ways to stop the thead from appearing on every page in a printout

Is there a way to prevent the thead of a table in an HTML page from being printed on all pages when a user initiates printing? The issue arises because I am using page-break-after: always; on an element before my table, causing the table header to also be ...