Tips for creating a Bootstrap 5 navbar dropdown menu that opens on hover for desktop users and on click for mobile users

Just starting out in the world of Web Development, so any help would be greatly appreciated!

I've been working on a Wordpress website for a company and encountered an issue with my CSS while implementing the Bootstrap 5 navbar. On desktop, the dropdown menus open on hover, and the items inside are clickable as intended. However, I'd like them to open only on hover, and currently, they remain open if clicked. Additionally, my CSS completely disrupted the mobile functionality - the entire menu refuses to open on mobile, the button toggle doesn't work, and I'm unsure how to proceed.

If I remove the custom CSS, the navbar works as expected out of the box - no hover on desktop, but clickable on mobile. So, I know the issue lies with the styling I've added.

Any suggestions on how to solve this, including using scripts or other methods, would be much appreciated. Thank you!

CSS (style.css):

.navbar-wrapper {
    background-color: var(--orange);
}
.nav-container {
    width: 100%;
    max-width: 90rem;
    margin: 0 auto;
    padding: 0 2.5rem;
    height: 5.625rem;
}
.navbar-toggler {
    border-style: none;
}
.navbar-brand {
    padding-top: .85rem;
}
.collapse.navbar-collapse {
    justify-content: flex-end;
    display: flex;
}
.navbar-nav .nav-link {
    color: #fff;
    text-transform: uppercase;
    font-weight: 600;
    padding: .625rem 0.75rem;
    border-radius: 50px;
    transition: background-color 0.3s;
}
.nav-item {
    padding-left: .625rem;
}
.nav-item.active .nav-link{
    background-color: rgba(255,255,255,0.8);
    color: var(--orange) !important;
}
.navbar-nav .nav-link:hover {
    background-color: rgba(255,255,255,0.2);
    color: #fff;
}
.nav-item.dropdown:hover .dropdown-menu {
    display: block;
}
.dropdown-menu {
    background-color: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.dropdown-menu .dropdown-item {
    text-transform: uppercase;
    font-weight: 600;
    color: var(--light-grey);
    transition: color 0.3s;
}
.dropdown-menu .dropdown-item:hover {
    color: var(--orange);
}

PHP (header.php):

<header class="navbar-wrapper">
        <nav class="navbar navbar-expand-lg">
            <div class="container-lg padding-global nav-container">
                <a class="navbar-brand" href="<?php echo esc_url( home_url( '/') ); ?>">
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/svg/logo.svg" width="130px" height="auto" alt="Logo Bigdooh">
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#bs-example-navbar-collapse-1" aria-controls="bs-example-navbar-collapse-1" aria-expanded="false" aria-label="<?php esc_attr_e( 'Menu de Navegação', 'your-theme-slug' ); ?>">
                    <i class="fa-solid fa-bars"></i>
                </button>
                <?php
                wp_nav_menu( array( 
                    'theme_location' => 'header-menu',
                    'depth' => 2,
                    'container' => 'div',
                    'container_class' => 'collapse navbar-collapse',
                    'container_id' => 'bs-example-navbar-collapse-1',
                    'menu_class' => 'navbar-nav mr-auto',
                    'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
                    'walker' => new WP_Bootstrap_Navwalker(),
                ) ); 
                ?>
            </div>
        </nav>
    </header>

Answer №1

If you want the additional CSS to only affect desktop and not mobile, you can easily achieve this by adding a CSS media query to your stylesheet.

@media only screen and (min-width: 720px) {
    .navbar-wrapper {
        background-color: var(--orange);
    }
    .nav-container {
        width: 100%;
        max-width: 90rem;
        margin: 0 auto;
        padding: 0 2.5rem;
        height: 5.625rem;
    }
    .navbar-toggler {
        border-style: none;
    }
    .navbar-brand {
        padding-top: .85rem;
    }
    .collapse.navbar-collapse {
        justify-content: flex-end;
        display: flex;
    }
    .navbar-nav .nav-link {
        color: #fff;
        text-transform: uppercase;
        font-weight: 600;
        padding: .625rem 0.75rem;
        border-radius: 50px;
        transition: background-color 0.3s;
    }
    .nav-item {
        padding-left: .625rem;
    }
    .nav-item.active .nav-link{
        background-color: rgba(255,255,255,0.8);
        color: var(--orange) !important;
    }
    .navbar-nav .nav-link:hover {
        background-color: rgba(255,255,255,0.2);
        color: #fff;
    }
    .nav-item.dropdown:hover .dropdown-menu {
        display: block;
    }
    .dropdown-menu {
        background-color: #fff;
        border-radius: 2px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }
    .dropdown-menu .dropdown-item {
        text-transform: uppercase;
        font-weight: 600;
        color: var(--light-grey);
        transition: color 0.3s;
    }
    .dropdown-menu .dropdown-item:hover {
        color: var(--orange);
    }
}

You can simply copy and paste the CSS code above to address the issue you're facing on your website.

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 create a timer using JavaScript?

I'm looking for a reverse timer to track session timeout. I found a code on codepen that works as a clockwise timer, but my attempts to make it counterclockwise have failed. Can someone please suggest a solution? I want the timeout to be either 1 hour ...

Adjustable Bootstrap Progress Bar - Modify element width on the fly

I have encountered an issue with my progress bars on the webpage. The static HTML version works perfectly fine, but the dynamically created one using jQuery seems to be instantly at 100% without any delay in progression. To illustrate the problem better, ...

Delete an entry from the list

I have a fully functional Ajax call and function that fills up a datalist. This datalist is utilized to choose from a limited list and add it to a UL element on the page. Once an option is added from the datalist to the actual list, I aim to eliminate that ...

Utilizing jQuery UI datepicker as a fallback in absence of HTML5 native date input type

Although <input type="date"> is not commonly used for desktop, it is the preferred method for mobile devices. I am attempting to incorporate the fallback code located at the end of this section: <form> <input type="date"> </form&g ...

Tips for maximizing the potential of the HTML5 <nav> element

While creating a theme with multiple menus in the header, I'm pondering whether to use separate nav tags for each menu or wrap them all inside one nav tag? You can see an example on codepen. header-a wraps everything inside a single nav tag. header ...

How can you configure fancybox3 to open exclusively on a double-click?

Looking for a solution to open a fancybox gallery with a double click on a grid of sortable images. The goal is to be able to focus on an image with a single click, allowing for sorting using keyboard commands rather than drag and drop. I attempted to use ...

I'm having trouble with my code - I suspect the issue might be related to the header() function

When I execute the following code, it resets the form and keeps me on the same page. I am aiming for it to redirect to home.php after a successful login. I believe there is an issue with my header() I have experimented with different approaches and have ...

Utilize jQuery to create a login form within a div when triggered by clicking on

I have implemented a jQuery script for login functionality that displays a new div in the center of the page while fading out the rest of the content. It is functioning correctly when I use an anchor tag with the class='login-window' and select i ...

The iconbar feature in the mobile menu is experiencing functionality issues

I am currently experimenting with the iconbar property of mmenu (here: ) Unfortunately, I am encountering an issue. The menu opens as expected when I run the code. However, upon closing the menu, it first closes completely and then the container slides sl ...

My uniquely designed post type isn't displaying correctly based on categories in WordPress

After creating a custom post type called 'phones', I specified 4 brand names as categories. My goal is to assign each phone to a specific category on the category page. This is my function code: function create_phone_post_type() { register_post ...

When utilizing json_encode to transmit HTML content, it results in receiving a "null" string appended at the

My approach involves loading PHP functions and passing them to JavaScript within a plugin, as demonstrated below: function me_nav_query_submit() { $urlcall = nav_me_paises(); /* retrieves a large HTML string */ $response = json_encode($urlcall); ...

Python script to extract data from a dynamic JavaScript webpage

I've been involved in a research project where we are aiming to gather a list of reference articles from the Brazil Hemeroteca (The specific page reference we are seeking: , needs to be located by extracting data from two hidden elements on this page: ...

Verification of Email Address Using HTML5

There are claims that with HTML5, there is no longer a need for JavaScript or server-side code to validate user input as a valid email or URL address. I am wondering how I can validate an email after a user enters it and without using JS, how can I displa ...

Issues with Nav.Link functionality within React Bootstrap navbar using React-router-dom

I'm currently working on a project using React and trying to incorporate a bootstrap navbar along with react-router-dom. Although I have everything displaying correctly, none of the links seem to be working. If you'd like to see the issue I&apos ...

Angular 2.0 in conjunction with D3.js does not style SVG elements using CSS

Currently, I am utilizing Angular 2.0 TypeScript along with the d3.js library to create graphics. My main focus is on applying CSS to the axis (especially shape-rendering: crispEdges;). Do you have any suggestions? Check out the code below: var vis = d3 ...

Bootstrap Navigation Bar Animation causing Input Fields to be cut off

I am working with a collapsible bootstrap NavBar that contains two elements within the collapsible <div>, <form class="form-inline> and <select class="custom-select" id="menu"> (Refer to my code below) The problem I'm encountering ...

Is it possible to replicate, modify, or recycle a purely HTML and CSS component in Angular?

On my Angular website, I am looking to reuse a component across more than 30 pages. While the majority of the pages have similar CSS, the content inside varies. I know I can simply insert <app-example></app-example> on all the pages, using < ...

Update the 'checked' attribute of a radio button when the form is submitted

How do I dynamically change the content of an HTML table based on which radio button a user selects using jQuery? For example, when the page loads, I want the United Kingdom radio button to be checked and the table content to display as 'blue'. I ...

Hide and reveal images on hover by using multiple images or links on a single webpage

Despite my efforts, I haven't been able to find exactly what I'm looking for. My current setup involves an image/content slider powered by Awkward Showcase. Within each slide, I want to incorporate a mouse-in/mouse-out effect that switches and hi ...

Having issues with the horizontal list layout

I am having trouble implementing Bootstrap horizontal lists on my website. How can I resolve this issue? Desired outcome: https://i.sstatic.net/zzBFi.png Current situation: https://i.sstatic.net/cfoZN.png Below is the snippet of CSS code in question ...