The alignment of the WordPress Bootstrap5 Navbar does not match the other col divs

I integrated Bootstrap 5's Navbar into my Wordpress website. However, I am facing an issue with alignment when the screen size changes. The button and expanded menu items do not align perfectly with the navbar-brand H2 element.

I have tried various solutions but cannot seem to get the expanded menu to align correctly without causing other issues. Any assistance would be greatly appreciated.

Header.php HTML

<nav class="navbar navbar-expand-lg navbar-light"> 
    <div class="container-fluid"> 
        <div class="row d-flex align-item-center justify-content-between">
            <div class="col ">
                <div class="dropdown-container">

                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> 
                    <span class="navbar-toggler-icon"></span> 
                    </button> 
                    <div class="collapse navbar-collapse" id="navbarSupportedContent"> 
                      <div class="site-menu dropdown-primary">
                        <ul id="primary-menu" class="navbar-nav">
                            <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-14 current_page_item menu-item-16"><a href="https://example.com/" aria-current="page">Test</a></li>
                            <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-18"><a href="https://example.com/checkout/">Checkout</a></li>
                            <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20"><a href="https://example.com/sample-page/">Sample</a></li>
                            <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-21"><a href="https://example.com/shop/">Shop</a></li>
                            <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-26"><a href="https://example.com/aboot-us/">Aboot Us</a></li>
                        </ul>
                      </div> 
                    <!-- other nav ul lists --> 
                    </div> 
                </div>
            </div>
            <div class="col">
                <a class="navbar-brand" href="https://www.example.com"><h2>Online Shop</h2></a>
            </div>
        </div>
    </div> 
</nav> 

CSS

header .navbar-brand h2 {
  color: white;
  padding: 1rem 0;
}

#primary-menu li a {
  display: block;
  padding: 0.25rem 1rem;
  color: rgb(176, 252, 153);
  font-size: 1rem;
  font-weight: bold;
  white-space: nowrap;
}

.dropdown-container {
  position: relative;
}

.dropdown-primary {
  position: absolute;
  top: 100%; /* Bottom of button */
  left: 0;
  margin-right: -100px; 
}

Answer №1

Your navbar design deviated significantly from the standard Bootstrap-5 format. Including rows and columns within the navbar is unnecessary as the collapse function adjusts the layout for smaller screens. The class dropdown-container was causing navigation links to appear below the navbar, so I removed it. Additionally, there was a dropdown-container div whose purpose was unclear, so I eliminated it as well.

While Bootstrap’s navbar is a robust tool, making too many modifications at once can lead to issues.

The current page is not clearly indicated in your code.

Updates made to align small-screen menu items to the right (under the hamburger button)

To position the menu items on the right side of the screen for small devices, you can apply the text-end class to each item. This will align the items to the right within the collapsing menu without affecting their display on larger screens.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d7f7272696e696f7c6d5d28332d332d307f78697c2e">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f0fce1f6d3a1bdaabda2">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3919c9c878087819283b3c6ddc3ddc3de91968792c0">[email protected]</a>/dist/js/bootstrap.min.js"></script>

<style>
    header .navbar-brand h2 {
        color: white;
        padding: 1rem 0;
    }

    #primary-menu li a {
        display: block;
        padding: 0.25rem 1rem;
        color: rgb(176, 252, 153);
        font-size: 1rem;
        font-weight: bold;
        white-space: nowrap;
    }
    
    .navbar-brand {
        font-size: 2rem;
    }
</style>

<nav class="navbar navbar-expand-md navbar-light">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">Online Shop</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="SupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="SupportedContent">
            <ul id="primary-menu" class="navbar-nav">
                <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-14 current_page_item menu-item-16 text-end"><a href="https://example.com/" aria-current="page">Test</a></li>
                <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-18 text-end"><a href="https://example.com/checkout/">Checkout</a></li>
                <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20 text-end"><a href="https://example.com/sample-page/">Sample</a></li>
                <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-21 text-end"><a href="https://example.com/shop/">Shop</a></li>
                <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-26 text-end"><a href="https://example.com/about-us/">About Us</a></li>
            </ul>
            <!-- additional nav ul lists -->
        </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

Prevent vertical scrolling on touch devices when using the Owl Carousel plugin

Is there a way to prevent vertical scrolling on Owl Carousel when dragging it horizontally on touch devices? It currently allows for up and down movement, causing the whole page to jiggle. If anyone has a solution, I can share the JavaScript file. Appreci ...

Verify if a <select> element exists inside the main div

Is there a way for me to check if a <select> element is present within the parent div and display certain content based on its existence? Appreciate any assistance! ...

The paragraph element is refusing to align with the next paragraph element on the following line

The issue I'm facing is that the paragraph element is not displaying on a separate line from the other paragraph element. body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; backgr ...

How do I effectively replace content with a banner or alert message for users experiencing issues with the website on Internet Explorer?

mysite seems to be struggling with displaying 3D content on Internet Explorer compared to Chrome. Is there a way I can replace the content with a banner or alert informing users that they will have a better experience using Chrome or other supported browse ...

Utilize npm libraries within the main directory of Sage 8.5.3 theme

When attempting to add npm libraries in the root of Sage 8.5.3, I encountered an error in the console stating that 'require' was not found. var GoogleMapsLoader = require('google-maps'); GoogleMapsLoader.load(function(goo ...

The implementation of jQuery within an included HTML file may not function properly

I'm looking to implement the use of includes for generic HTML elements in my project. However, I've run into an issue where including an HTML file causes jQuery to stop working. Here is a snippet of my code that illustrates the problem. Specifica ...

Update the CSS of a different element when hovering

Hello to all the jQuery developers out there! I have a question that's fairly simple. I'm trying to change a CSS attribute for an element when I hover over a different element on the page. I've created a fiddle for you to review, and I&apos ...

Effortlessly connecting Angular with forms

Check out this jsfiddle demo: https://jsfiddle.net/zpufky7u/1/ All the forms on my site were functioning correctly until suddenly angular started binding them all with class="ng-pristine ng-valid" Could this be due to a setting or something else causing ...

Guide to removing selected value from a combobox in Angular

I am working on a simple HTML file that consists of one combobox and one clear button. I want the clear button to remove the selected value from the combobox when clicked. Below is my code: mat-card-content fxLayout="row wrap" fxLayoutAlign="left" fxLayou ...

What is the secret to creating double-width characters that are precisely double the size of single-width characters?

When using terminal, many fonts that support double-width characters display them as exactly twice the width of single-width characters. For instance, these two lines should appear with the same width: あああ aaaaaa However, this consistency is not ma ...

failure in data transmission causing upload issues

Is there a way for me to prevent the character > from causing issues? I have a combo box with an option listed below. I attempted to replace > with &gt;, but it still isn't uploading correctly. <select name="Category3" id="Category3"> ...

The lack of responsiveness in Bulma CSS is causing issues with the Bulma Carousel and image displays

I recently built a website using the Bulma CSS framework for styling, incorporating the Bulma Carousel for a text carousel. Utilizing Bulma's column feature, I positioned the carousel next to an image/video on the left side of the screen. Everything l ...

Display or conceal the location where the click event occurs within a single div

progress $(".button-toggle").click(function(e){ $(this).parents.find('#second').toggle(); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="first"> <a href="#" class= ...

Tips for presenting random images from an assortment of pictures on a webpage

I'm looking to enhance my website by adding a unique feature - a dynamic banner that showcases various images from a specific picture pool. However, I'm unsure of how to find the right resources or documentation for this. Can you provide any guid ...

Maintain a consistent header height with CSS even when using the calc() function

--dis: calc(var(--max-height) - var(--min-height)); /* @media large{--min-height:13rem}; @media small{--min-height:6.5rem}; --max-height:75vh; */ --percent: calc(var(--scroll-ani) / var(--dis)); /* in js: document.body.style = "--scroll-ani: ...

The div element is not adjusting its size according to the content it

Essentially, I want the #main-content div to expand so that its content fits inside without overlapping, as shown in the codepen example. I've been unsuccessful in implementing the clearfix or overflow:hidden solutions so far. It's puzzling why ...

Ensuring one div remains in a fixed position relative to another div

I'm struggling to keep my two white divs aligned in relation to the black div in the center. They need to maintain the same distance from the black div even when the page is resized. #halvfjerds { width: 100%; height: 1050px; background-color ...

What is the best way to incorporate variables into a text file using ajax?

I am having an issue with my code. I have written a script that allows users to draw an image on canvas and then extract pixel values from the clicked location, writing them into a text file using ajax. However, I am facing problems with my onclick and a ...

Why am I only able to send form data using the GET method and not the POST method?

I'm currently delving into the realm of html and php, but I seem to have hit a snag. When utilizing the GET method in my html form, parameters are successfully sent to my php file. However, when attempting the same with the POST method, no parameters ...

Printing Strings in JavaScript IF Conditional Block

Hello there! I am looking for assistance with a JavaScript concept. Recently, I created some code where in the HTML file, there is a div element with an id of "GetID" where the string outputs are meant to be displayed. The JavaScript code looks something ...