Overflowing dropdown menus on a dynamic navigation bar

After creating a navigation bar with dropdown links, I encountered an issue where hovering over the link to open the drop-down caused it to overflow to the right. Since the button is positioned at the right end of the navbar, it resulted in the scrollbar appearing. How can I adjust the position so that the link in the drop-down starts from the right side?

    .navbar {
    list-style-type: none;
    overflow: hidden;
    background: #3498db;
    border-bottom: 1px solid #2c3e50;
    }
    
    .navbar-item {
    float: left;
    }
    
    .navbar-item.right {
    float: right;
    }
    
    .navbar-link {
    display: block;
    color: #ecf0f1;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    }
    
    .navbar-link:hover, .navbar-link.active, .dropdown-content a:hover, .dropdown-content a.active {
    background: #2980b9;
    }
    
    .dropdown-content {
    display: none;
    position: absolute;
    background-color: #3498db;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    min-width: 120px;
    z-index: 1;
    }
    
    .dropdown-content a {
    color: #ecf0f1;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
    }
    
    .dropdown:hover .dropdown-content {
    display: block;
    }
    
    @media screen and (max-width: 600px) {
    .navbar-item.right, .navbar-item {
    float: none;
    }
    }
<ul class="navbar">
    <li class="navbar-item"><a href="#" class="navbar-link active">Home</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item dropdown">
    <a href="javascript:void(0);" class="navbar-link">{PH}</a>
    <div class="dropdown-content">
    <a href="#">{PH}</a>
    <a href="#">{PH}</a>
    <a href="#">{PH}</a>
    </div>
    </li>
    <li class="navbar-item dropdown right" id="membersbtn">
    <a href="javascript:void(0);" class="navbar-link">Members</a>
    <div class="dropdown-content">
    <a href="#">Register</a>
    <a href="#">Log In</a>
    </div>
    </li>
    </ul>

Answer №1

To achieve the desired effect, include the following code:

.dropdown:hover .dropdown-content { right:0px;}

    .navbar {
    list-style-type: none;
    overflow: hidden;
    background: #3498db;
    border-bottom: 1px solid #2c3e50;
    }
    
    .navbar-item {
    float: left;
    }
    
    .navbar-item.right {
    float: right;
    }
    
    .navbar-link {
    display: block;
    color: #ecf0f1;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    }
    
    .navbar-link:hover, .navbar-link.active, .dropdown-content a:hover, .dropdown-content a.active {
    background: #2980b9;
    }
    
    .dropdown-content {
    display: none;
    position: absolute;
    background-color: #3498db;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    min-width: 120px;
    z-index: 1;
    }
    
    .dropdown-content a {
    color: #ecf0f1;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
    }
    
    .dropdown:hover .dropdown-content {
    display: block;
        right:0px; /*Include this line*/
    }
    
    @media screen and (max-width: 600px) {
    .navbar-item.right, .navbar-item {
    float: none;
    }
    }
<ul class="navbar">
    <li class="navbar-item"><a href="#" class="navbar-link active">Home</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item dropdown">
    <a href="javascript:void(0);" class="navbar-link">{PH}</a>
    <div class="dropdown-content">
    <a href="#">{PH}</a>
    <a href="#">{PH}</a>
    <a href="#">{PH}</a>
    </div>
    </li>
    <li class="navbar-item dropdown right" id="membersbtn">
    <a href="javascript:void(0);" class="navbar-link">Members</a>
    <div class="dropdown-content">
    <a href="#">Register</a>
    <a href="#">Log In</a>
    </div>
    </li>
    </ul>

Answer №2

To make the necessary changes, include the following code:

.navbar-item.right .dropdown-content {
    right: 0;
}

Refer to the example below:

    /* Existing CSS styles */
    
    .navbar {
    list-style-type: none;
    overflow: hidden;
    background: #3498db;
    border-bottom: 1px solid #2c3e50;
    }
    
    /* Additional CSS rules */
    
.navbar-item.right .dropdown-content {
right: 0;
}
    
    @media screen and (max-width: 600px) {
    .navbar-item.right, .navbar-item {
    float: none;
    }
    }
<ul class="navbar">
    <li class="navbar-item"><a href="#" class="navbar-link active">Home</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item"><a href="#" class="navbar-link">{PH}</a></li>
    <li class="navbar-item dropdown">
    <a href="javascript:void(0);" class="navbar-link">{PH}</a>
    <div class="dropdown-content">
    <a href="#">{PH}</a>
    <a href="#">{PH}</a>
    <a href="#">{PH}</a>
    </div>
    </li>
    <li class="navbar-item dropdown right" id="membersbtn">
    <a href="javascript:void(0);" class="navbar-link">Members</a>
    <div class="dropdown-content">
    <a href="#">Register</a>
    <a href="#">Log In</a>
    </div>
    </li>
    </ul>

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

Enhancing Xcode security login through personalized security question prompts

I have been working on implementing the following code snippet: NSURL *url = [NSURL URLWithString:@"https://login.XXX.com/security/xxxforms// logonchalnp.fcc?TYPE=33554433&REALMOID=06-95b0f0c8-198a-1039-b583 83b02659fd47&GUID=&SMAUTHREASON=0&a ...

Retrieve the HTML value of an element in Vue.js by clicking on its adjacent element

Hey there, I'm currently working on a simple notes app and I've hit a roadblock with one particular feature. In my project, I have a card element with a delete button as a child. What I need to achieve is to check if the value of the .card-title ...

Changing the Background Color of the Toolbar in Ionic

I am having trouble making the background color of my footer dynamic. I have tried binding the element to a class or applying dynamic styling, but it doesn't seem to work. Even when I have this in my HTML: <ion-footer align="center" style="height: ...

What is the best way to add the ".html" suffix to pages in nuxt.js?

Is it possible to add the ".html" extension to a page address? For instance, changing "test_page" to "test_page.html" ...

What could be causing the page to automatically scroll to the bottom upon loading?

My experience with this bootstrap/jquery page in Firefox (v39, latest updates installed) has been unusual as it always seems to jump to the bottom of the page upon clicking. Upon inspecting the code, I couldn't find any JavaScript responsible for thi ...

Vertical text area expansion functioning in Chrome, but not in Firefox

Is there a way to create a textarea that dynamically expands to fill the available space within a div, while maintaining fixed height offsets from the top and bottom? The CSS code below achieves this effect in Chrome, but Firefox displays the textarea with ...

Tips for limiting users to inputting only alphanumeric characters and excluding special characters in an input field using Angular 8

How can I prevent users from inputting special characters in an input field and only allow alphanumeric values? The code that I have implemented so far does not seem to be working as intended. When a user enters a special character, it still shows up in th ...

Breaking HTML attribute values into separate lines will make the code more organized and easier

Is it possible to format HTML attribute values so they can span across multiple lines? Essentially, I'm wondering if there is a way to include line breaks within a regular typed HTML attribute value. For instance, in the functional code snippet below ...

Using Vue JS to display information conditionally within a single component

I currently have a component that dynamically displays the data from an array of objects. However, I want the component to display only one object based on a specific condition. Is it possible to show either one object or another depending on the value o ...

Aligning Images Inside Div Containers

I am currently facing an issue with centering 4 images on my website and my brain seems to have hit a roadblock in solving this problem. Here is the Fiddle link for reference -> http://jsfiddle.net/theminijohn/bcMX5/ Simply using the <center> ta ...

Angular form grouping radio buttons in a unique way

Encountering some unusual behavior with radio buttons and forms currently. In my form, I have 4 radio buttons set up. The issue I'm facing is that when I click on a radio button, it remains enabled permanently. This means I can have all 4 options sel ...

Identification numbers remain constant

I've been working on a custom CMS blog, specifically designing a page for admin access. My goal is to incorporate pagination into the layout. Currently, the page displays the most recent six posts with IDs ranging from 1 to 6 on the initial view. How ...

Is there a way to completely remove all styling from a specific child element?

Struggling with a drop-down menu issue - the parent element has a border, but I don't want that style to be inherited by the child sub-menu. Despite my attempts to remove the border using border: 0px, it's not working as expected. Is there any wa ...

Deactivating the submit button after a single click without compromising the form's functionality

In the past, I have posed this question without receiving a satisfactory solution. On my quiz website, I have four radio buttons for answer options. Upon clicking the submit button, a PHP script determines if the answer is accurate. My goal is to disable t ...

Looking for a different method to remove a list item within an unordered list using CSS instead of HTML

I am currently working on designing a mock-up website using CSS codes. If you want to see the mock-up website, you can visit: Here is the specific list I am referring to: This is the expected outcome: In order to achieve the desired result, I used HTML ...

Utilizing Cache Features in the XDK Application Framework API

I recently posted a question in the Intel Developer Zone forum for XDK, but unfortunately have not received any answers yet. So I decided to seek help here: My project involves creating a simple HTML5-only periodical website that will be packaged as a sta ...

I am currently having issues with a PHP script I wrote for sending email, as it is

After creating a form and implementing a PHP script to send an email upon form submission, I encountered an issue where the "Thank you" message was displayed on a different HTML page, but the email wasn't sent to my account. I uploaded the files to t ...

Potential issue: Firefox causes distortion in animated stroke-linecap="round" properties

While working on a project on Stack Overflow, I noticed a potential bug in Firefox when animating a line with stroke-linecap="round" and vector-effect="non-scaling-stroke" svg{border:1px solid} path{ animation: draw 1s ease-in; ...

Obtain the ID of a YouTube video from an iFrame link using jQuery

Check out this YouTube video: iframe width="560" height="315" src="//www.youtube.com/embed/XbGs_qK2PQA" frameborder="0" allowfullscreen></iframe>` (Hell Yeah! Eminem :P) I only want to extract "XbGs_qK2PQA" from the link provided. Using $(&apo ...

Learn how to toggle the visibility of three div elements arranged horizontally

$(document).ready(function () { $("#toggle").click(function () { if ($(this).data('name') == 'show') { $("#sidebar").animate({ width: '10%' }).hide() $("#map").an ...