Customized navigation bar with a fixed pointer height

I designed a test page with a menu bar that features a triangular pointer when hovered over. Everything looks good except for the issue where the blue menu bar expands in height when adding the pointer at the bottom of the bar. My initial thought is to give the menu bar a fixed height and remove this style during a media query.

Is there a simpler or better solution to add the pointer without affecting the height?

(the triangle should touch the border of the menubar at the very bottom without any gap. I will adjust this after resolving the height issue)

Thank you in advance!

Screenshot: https://i.sstatic.net/sD9bM.png Code:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="57353838232423253627176379617967">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8d80809b9c9b9d8e9fafdbc1d9c1df">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<style>
    .bg-banner {
        background-image: linear-gradient(#17517f, #46769d);
    }

    .bg-headbar {
        background-color: #005db9;
    }

    .bg-banner .nav-link {
        color: white !important;
    }

    #menubar .nav-link {
        font-size: 16px;
        padding-right: 50px;
        color: white;
    }
        #menubar .nav-link:focus,
        #menubar .nav-link:hover,
        #menubar .nav-link:visited {
            color: white;
        }
    @media (min-width:768px) {
        nav.navbar .navbar-nav li.nav-item.active:after {
            content: "";
            position: relative;
            margin-left: -31px;
            left: 50%;
            bottom: 15px;
            border-left: 6px solid transparent;
            border-right: 6px solid transparent;
            border-bottom: 6px solid white;
        }
    }

    @media (max-width:767px) {
        .dropdown-menu > a {
            display: block !important;
        }
    }

    #menubar li.dropdown.show {
        position: static;
    }

        #menubar li.dropdown.show .dropdown-menu {
            display: table;
            width: 100%;
            text-align: center;
            left: 0;
            right: 0;
            margin: 0;
        }

    .dropdown-menu > a {
        display: table-cell;
    }

    .dropdown-menu > a {
        font-weight: 600;
    }
</style>

<nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
    <a class="navbar-brand" href="#">Test Site</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse navbar-toggle">
        <form class="form-inline my-2 .m-md-0 ml-auto">
            <div class="input-group">
                <input type="text" class="form-control form-control-sm" placeholder="Seach" size="40">
                <div class="input-group-append">
                    <button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
                </div>
            </div>
        </form>
    </div>

    <div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
        <nav class="navbar navbar-expand-sm navbar-light">
            <ul class="navbar-nav">
                <li class="nav-item dropdown">
                    <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                        <i class="fa fa-user"></i>
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="#">Sign Out</a>
                    </div>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link"><i class="fa fa-cog"></i></a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                        <i class="fa fa-question-circle"></i>
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="#">Help</a>

                    </div>
                </li>
            </ul>
        </nav>
    </div>

</nav>
<nav class="navbar navbar-expand-md navbar-dark text-white bg-headbar py-0 py-8-md">
    <div class="collapse navbar-collapse navbar-toggle" id="menubar">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item dropdown">
                <a class="nav-link" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Menu1
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown1">
                    <a class="dropdown-item" href="#">Home<span class="sr-only"> (current)</span></a>
                    <a class="dropdown-item" href="#">Submenu 1B</a>
                </div>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Menu2
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown2">
                    <a class="dropdown-item" href="#">Submenu 2A</a>
                    <a class="dropdown-item" href="#">Submenu 2B</a>
                </div>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link" href="#" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Menu3
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown3">
                    <a class="dropdown-item" href="#">Submenu 3A</a>
                    <a class="dropdown-item" href="#">Submenu 3B</a>
                </div>
            </li>
        </ul>
    </div>
</nav>

<div class="container">
     Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, blanditiis eaque? Vero consequuntur tempora vitae at maxime, repellendus excepturi quia quibusdam odit sed ullam praesentium natus rerum molestiae cupiditate possimus?
</div>

<script type="text/javascript">
    $("#menubar li.dropdown").hover(function () {
        $(this).addClass("active show");
    },
        function () {
            $(this).removeClass("active show");
        });
    $("#menubar ul.dropdown-menu>li>a").on("click", function (e) {
        $("#menubar li.dropdown").removeClass("active show");
    });
</script>

jsfiddle

Answer №1

I really appreciate the solution provided by @joshmoto, which neatly aligns the submenus directly under the menu item. However, I believe that achieving full-width submenus is possible by simply applying the position: static to Menu1 and other list items.

When the submenus are expanded to full width, it becomes necessary to position them towards the right in order to maintain alignment (with adjustments based on your specific menu text).

$("#menubar li.dropdown").hover(function() {
        $(this).addClass("active show");
    },
    function() {
        $(this).removeClass("active show");
    });
$("#menubar ul.dropdown-menu>li>a").on("click", function(e) {
    $("#menubar li.dropdown").removeClass("active show");
});
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="27454848535453554657671309110917">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8cac7c7dcdbdcdac9d8e89c869e8698">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<style>
    .bg-banner {
        background-image: linear-gradient(#17517f, #46769d);
    }

    .bg-headbar {
        background-color: #005db9;
    }

    .bg-banner .nav-link {
        color: white !important;
    }

    #menubar .nav-link {
        font-size: 16px;
        padding-right: 50px;
        color: white;
    }

    #menubar .nav-link:focus,
    #menubar .nav-link:hover,
    #menubar .nav-link:visited {
        color: white;
    }

    @media (min-width:768px) {
        .navbar {
            height: 2.5rem;
        }

        nav.navbar .navbar-nav li.nav-item.active:after {
            content: "";
            position: relative;
            display: inline-block;
            margin-left: -1.6875rem;
            left: 50%;
            bottom: 1.1875rem;
            border-left: 0.375rem solid transparent;
            border-right: 0.375rem solid transparent;
            border-bottom: 0.375rem solid white;
        }

        .navbar-nav li:nth-child(2) .dropdown-menu {
            padding-left: 6.75rem;
        }

        .navbar-nav li:nth-child(3) .dropdown-menu {
            padding-left: 13.4375rem;
        }


        .dropdown-menu>a {
            font-weight: 600;
            float: left;
            width: auto;
            clear: none;
        }
    }

    @media (max-width:767px) {
        .dropdown-menu>a {
            display: block !important;
            font-weight: 600;
        }
    }

    #menubar li.dropdown.show {
        position: static;
    }

    #menubar li.dropdown.show .dropdown-menu {
        display: table;
        width: 100%;
        text-align: center;
        left: 0;
        right: 0;
        margin: 0;
    }
</style>

<nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
    <a class="navbar-brand" href="#">Test Site</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse navbar-toggle">
        <form class="form-inline my-2 .m-md-0 ml-auto">
            <div class="input-group">
                <input type="text" class="form-control form-control-sm" placeholder="Seach" size="40">
                <div class="input-group-append">
                    <button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
                </div>
            </div>
        </form>
    </div>

    <div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
        <nav class="navbar navbar-expand navbar-light">
            <ul class="navbar-nav">
                <li class="nav-item dropdown">
                    <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                        <i class="fa fa-user"></i>
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="#">Sign Out</a>
                    </div>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link"><i class="fa fa-cog"></i></a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link" data-toggle="dropdown" role="button" href="#">
                        <i class="fa fa-question-circle"></i>
                    </a>
                    <div class="dropdown-menu">
                        <a class="dropdown-item" href="#">Help</a>

                    </div>
                </li>
            </ul>
        </nav>
    </div>

</nav>
<nav class="navbar navbar-expand-md navbar-dark text-white bg-headbar py-0 py-8-md">
    <div class="collapse navbar-collapse navbar-toggle align-self-start" id="menubar">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item dropdown">
                <a class="nav-link" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Menu1
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown1">
                    <a class="dropdown-item" href="#">Home<span class="sr-only"> (current)</span></a>
                    <a class="dropdown-item" href="#">Submenu 1B</a>
                </div>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link" href="#" id="navbarDropdown2" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Menu2
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown2">
                    <a class="dropdown-item" href="#">Submenu 2A</a>
                    <a class="dropdown-item" href="#">Submenu 2B</a>
                </div>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link" href="#" id="navbarDropdown3" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Menu3
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown3">
                    <a class="dropdown-item" href="#">Submenu 3A</a>
                    <a class="dropdown-item" href="#">Submenu 3B</a>
                </div>
            </li>
        </ul>
    </div>
</nav>

<div class="container">
    Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, blanditiis eaque? Vero consequuntur tempora vitae at maxime, repellendus excepturi quia quibusdam odit sed ullam praesentium natus rerum molestiae cupiditate possimus?
</div>

While the absence of the dropdown-toggle class on Menu1, etc. items may limit accessibility for users navigating with the tab key or using touch devices, it should suffice if you do not require such support.

Answer №2

It appears that you are experimenting with adding a triangle pointer to each hoverable navigation item. While I haven't provided an ideal solution, I have made some adjustments to your CSS by positioning the pseudo 'after' element relative to its parent rather than static.

My modifications may impact the full-width active navigation dropdown menu, but they could help guide you in the right direction 👍

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05676a6a71767177647545312b332b35">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="086a6767e96777d163664c34ad0559">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<style>
  .bg-banner {
    background-image: linear-gradient(#17517f, #46769d);
  }
  
  ... (CSS code continues)
          

Answer №3

Here is an example of a navbar setup using the standard Bootstrap configuration for comparison. Although it lacks the hover effect, it is designed to be accessible via the tab key and touch devices.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35636469716b636160776571207d6b64682c66">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395454534b51593e53554157365241404175343a51">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<style>
    .bg-banner {
        background-image: linear-gradient(#17517f, #46769d);
    }

    .bg-headbar {
        background-color: #005db9;
    }
</style>

<nav class="navbar navbar-expand-md navbar-dark text-white bg-banner">
    <a class="navbar-brand mr-auto" href="#">Test Site</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-toggle">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse flex-grow-0 navbar-toggle">
        <form class="form-inline my-2 my-md-0 mr-md-3">
            <div class="input-group">
                <input type="text" class="form-control form-control-sm" placeholder="Search" size="40">
                <div class="input-group-append">
                    <button class="btn btn-outline-light btn-sm" type="button"><i class="fa fa-search"></i></button>
                </div>
            </div>
        </form>
    </div>

    <!--<div class="banner-navbar collapse navbar-collapse flex-grow-0 navbar-toggle">
        <nav class="navbar navbar-expand navbar-light">-->
        <div class="collapse navbar-collapse flex-grow-0 navbar-toggle">
            <ul class="navbar-nav">
... (additional code remains the same as in original)
    </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

What is the best way to use CSS/jquery to make a default toggle show as open

I need help with modifying a code that collapses/expands an HTML area when clicked. The default setting is currently closed. How can I change it to either open or closed by default? http://jsfiddle.net/8ZCXU/5/ ...

Preserving Scroll Position in jQuery Mobile 1.3 List ViewEfficiently retaining scroll position in

I've been doing a lot of research on this issue but unfortunately haven't found a solution yet. I'm currently working with jquery mobile 1.3. The problem I'm facing is related to a dynamic nested list view that gets populated from a da ...

Getting a URL to redirect after a successful login via an AJAX request in PHP

I've been trying to figure out how to redirect the URL after a successful login using an Ajax call in PHP. Can someone please review my code and point out any mistakes? Here is the content of the registration.php file located at http://localhost:8080 ...

How can one retrieve the value of AngularJS {{curly-brace-value}} using jQuery?

Although I may not be well-versed in angular/jquery, it is clear that the div generated using angular has caught my attention: <div id='my-div' style={{ang.style}}'> This is content inside the div </div> The style of the div is ...

Encountering a problem when trying to dynamically change the value in a dropdown menu with Angular.js

Having an issue where two drop down lists have the same value. When I set a value to the first drop down list, the second one also takes that value. Below is my code for reference: <div class="col-md-6"> <div class="input-group bmargindiv1 col-md ...

What is the best way to export my mongo schema to a file and then utilize it for inserting data?

I've been encountering difficulty when attempting to insert data into my collection. I'm not entirely sure if I'm doing it correctly, so I apologize for the vague request. Hopefully, by providing you with my code, you can help me understand ...

There was an issue parsing the JSON data due to a syntax error that was encountered

I came across this code snippet: function pushJsonData(productName) { $.ajax({ url: "/knockout/SaveProduct", type: "POST", contentType: "application/json", dataType: "json", ...

Is it possible to replace a server-side templating engine with Angular 2's server-side pre-rendering?

As a novice in web development, I have recently been exploring angular 1.x, react.js, and angular 2 (eventually deciding on angular 2). Lately, I've been intrigued by the concept of server-side pre-rendering. In my understanding, this process is ...

Order of custom code and JQuery in ASP master page

Using an ASP master page to include all the Javascript and jQuery files has presented a challenge for me. Specifically, the jQuery function in OrderManagement.js $(".menu-item").click(function () { window.alert("some text"); }); fails to execute whe ...

Is there a way to implement this toolbar in Ionic Angular?

https://i.sstatic.net/sGd1o.png I am looking to replicate the toolbar shown in the image above using Ionic. As a beginner in Ionic development, I am finding it challenging to translate the design into code. I attempted to use a grid layout, but the varyin ...

I need some help with adjusting the number of rows shown per page in MaterialReactTable

I've been utilizing MaterialReactTable and my goal is to display only 5 items on each pagination page. Despite setting muiTablePaginationProps, I still see 10 items per page. How can I resolve this issue? <MaterialReactTable columns={columns} ...

What determines the padding of a display:table-cell?

I am currently working on creating a table-like layout where medicine names will be displayed on the left and data will occupy the rest of the table. In simpler terms: +-------------+------------+ medicine 1 | some data | some data | ...

Adjusting the layout of a Wordpress website created with visual composer to be responsive

Continuing on from this post: Wordpress Visual Composer Strech Row and Direction RTL This fantastic jQuery solution works perfectly on window load: jQuery(window).load(function () { if (jQuery('html').attr('dir') == 'rtl& ...

Enhanced jQuery Pop-Up Panel

A few issues to address: 1) My goal is to optimize the efficiency of this script. 2) When a user clicks on either pop-out button, it opens a window and hides the element. (Currently using .detach() to remove the embedded video player because in Firefox . ...

Error: The parameter "q" provided in the Google Maps Embed API is

Currently, I am developing a Web Application using NodeJs, Express, and Pug/Jade. The pug code below is functional: iframe#map(width="100%", height="600", frameborder="0", style="border:0" src='https://www.google.com/maps/embed/v1/place?' + ...

Angular: Transferring dynamic functions between parent and grandchild components

For my angular application, I am developing a versatile table component that can accept inputs for rows, columns, as well as handle various action button functions to render the table. The end result should resemble something like this: https://i.sstatic.n ...

Changing the height of a pseudo element according to the width of the viewport in a dynamic way

Currently, I am utilizing the :before pseudo-element on a 100% width div to create a curved top, which is functioning correctly. However, I am facing the issue of adjusting the height of the :before element through media queries whenever the browser width ...

Dragging a child column in ag-grid can be made more efficient by also allowing the user to drag

My grid layout resembles the one shown in this image: https://i.sstatic.net/S7F0n.png The ag-grid feature allows you to drag individual child columns (such as 'Game Name' or 'Bought') out from the parent column, dividing the parent in ...

Extract the color from the most prominent element in the viewport

I'm working on a button that will take the user to the bottom of the page, and I want the background color of the button to match the color of the first element that touches the viewport. The button is functioning properly, but I'm having trouble ...

Retrieve the information from a fetch call and save it for later use

Greetings to the wonderful StackOverFlow community. As a beginner in web development, I have countless questions swirling in my mind. The focal point of my query is regarding a fetch request in JavaScript. I am struggling to extract the data (userId) fr ...