Menu dropdown positioned beyond visible window

I'm facing an issue with my CSS and HTML involving a Press Me button that triggers a dropdown menu to open when clicked. The problem I'm encountering is that when the dropdown opens, it is partially outside of the visible window, requiring users to scroll right to see it entirely.

I attempted to address this by using right: px with various numbers, but this solution ended up making the design non-responsive.

// NO PROBLEM HERE IN JAVASCRIPT! 
function myFunction() {
    document.getElementsByClassName('dropdown-content')[0].classList.toggle('show');
}
window.onclick = function(event) {
    if (!event.target.matches('#verify-name')) {
        var dropdowns = document.getElementsByClassName('dropdown-content');
        var i;
        for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
            }
        }
    }
}
.buttons {
    display: flex;
    justify-content: flex-end;
    color: red;
}
.dropdown-content {
    display: none;
    position: absolute;
    width: 120px;
        padding: 14px;
        background-color: yellow;
}
.show {
    display: flex;
}
<div class="buttons">
    <div>
        <a id="verify-name" onclick="myFunction()">Press Me</a>

        <div class="dropdown-content">
            <span>Logout</span>
        </div>
    </div>
</div>

Answer №1

I made a simple adjustment by removing position: absolute from the .dropdown-content class and adding text-align: right; to the .buttons class.

// I have no issues in my JavaScript code!
function myFunction() {
  document.getElementsByClassName('dropdown-content')[0].classList.toggle('show');
}
window.onclick = function(event) {
  if (!event.target.matches('#verify-name')) {
    var dropdowns = document.getElementsByClassName('dropdown-content');
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.buttons {
  display: flex;
  justify-content: flex-end;
  color: red;
  text-align: right;
}

.dropdown-content {
  display: none;
  direction: rtl;
  width: 120px;
  padding: 14px;
  background-color: yellow;
}

.show {
  display: flex;
}
<div class="buttons">
  <div>
    <a id="verify-name" onclick="myFunction()">Press Me</a>

    <div class="dropdown-content">
      <span>Logout</span>
    </div>
  </div>
</div>

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

Having trouble identifying the source of the margin-right styling being used

I'm facing a perplexing issue with the CSS for my sidebar (#primary) on my website. Even though I haven't specified any margin for the sidebar, it is somehow getting a margin-right of 605px. In trying to solve this, I experimented with relative ...

Duplicate label issue with 'No files chosen' message on Bootstrap form control

After migrating to webpack for managing our assets (js, scss, ...), everything seemed to be working smoothly. However, we encountered a minor issue where the input label behind the file-selector-button is overflowing with its neighbor, as shown in the exam ...

Obtain document via Angular 2

Is it possible to use TypeScript to download an image that is already loaded? <div *ngIf="DisplayAttachmentImage" class="fixed-window-wrapper_dark"> <button class="btn btn-close-window" (wslClick)="HideAttachmentImage()"> & ...

Simplified jQuery function for multiple div mouseover operations

Uncertain about the accuracy of my title. Due to certain reasons, I need to assign different IDs for the class, as it only detects ID and not class when hovered over. Therefore, I have created a CSS version where the opacity of a specific div will change t ...

Having an issue with jQuery where trying to append a remove button to a list results in the

Looking to create a dynamic list where users can easily add new items by clicking a button. As each item is added, a corresponding remove button should also be displayed. The issue I'm facing is that every time a new item is added, an extra close but ...

What is the method for initiating a radial gradient from the middle of the pie chart?

In my pie chart with grid lines, I have successfully implemented a radial gradient. However, the issue is that there is a separate gradient for each pie slice. What I really want is a single gradient originating from the center of the entire pie chart. I ...

Safari browser causing Google Plus icon to disrupt alignment

My share buttons at the bottom of my page look fine in Chrome and Firefox: https://i.sstatic.net/4mkbv.png In Safari, not so much: https://i.sstatic.net/X5WbO.png And in Opera, they break too: https://i.sstatic.net/66iXp.png Here is my HTML code: &l ...

Definitions that are displayed dynamically when hovering over a particular element

I am seeking a way to implement popup definitions that appear when a div is hovered over. My website showcases detailed camera specifications, and I want users to see a brief definition when they hover over the megapixel class called '.mp'. One o ...

How can a node be added between two nodes based on an attribute condition?

Is there a jQuery function, or perhaps another library function, that allows for the insertion of a node (div) between two other nodes (divs) based on its attribute? For instance: Assume I have the following HTML code: <div value=111/> <div val ...

I am having trouble understanding why dropdown menus with the same content have varying widths

My issue is illustrated in the screenshots below: first-dropdown: second-dropdown: The second dropdown appears slightly wider on the right side. Despite Google Chrome's claim that the widths are the same. HTML code: <div class="row menu"> ...

What are the steps to include a scrollable tooltip in HTML?

Is there a way to implement a scrollable tooltip without using an iframe? Any existing jquery or css scripts that can help achieve this? If so, how can it be done? ...

Is it possible for Angular's ngStyle to accept multiple functions? If not, what is the alternative solution?

Can multiple conditions be added to different attributes with ngStyle? Is it possible to change text color and background color based on specific JSON values using ngStyle? About the Project: I am currently working on creating a tab bar that resembles a ...

Can someone help me uncover the previous URL for login using just JavaScript? I've tried using document.referrer but it's not giving me the

Currently, I am utilizing express with pug templates and pure JavaScript. In order to enhance the user experience of my log in system, I would like to save the URL that someone came to the login page with, so that I can redirect them back to it once they h ...

Choose the nth item from a group of elements that do not have the same parent node in common

Is it possible to select a specific li element within a dynamic HTML structure without knowing the exact number of ul elements or li elements in each? From my understanding, neither :nth-child nor :nth-of-type can achieve this due to the elements needing ...

How can I use CSS to clip the United States, specifically Tennessee and Nashville, from the left side?

For my ASP.NET generated hyperlink that says "select location", when the user clicks on it, they are redirected to a new page where they can choose a different location at /change-location/default.aspx. This could be any country, state, or city in the worl ...

Get rid of any spaces that are present between or after td elements by applying BeautifulSoup

Does anyone know how to remove spaces between td elements using beautiful soup? For instance <table> <tr class="soup-target"> <td></td> <td></td> </tr> </table> Before suggesting to simply use ...

Tips for handling Ajax urlencode in PHP

I'm facing an issue with a problem and need some assistance to resolve it. Currently, I am attempting to utilize Ajax urlencode in PHP, but the POST content is not being displayed by PHP as expected when HTML is sent directly to PHP. The following c ...

Create a dynamic animation using Angular to smoothly move a div element across the

I currently have a div with the following content: <div ng-style="{'left': PageMap.ColumnWrap.OverviewPanelLeft + 'px'}"></div> Whenever I press the right key, an event is triggered to change the PageMap.ColumnWrap.Overvie ...

Display exclusively the chosen option from the dropdown menu

I am facing an issue with the select input on my webpage. Whenever I try to print the page, it prints all the options of the select instead of just the one that is selected. Can someone please guide me on how to modify it so that only the selected option ...

Display div content depending on the clicked ID in AngularJS

Unique Header Links HTML <ul ng-controller="pageRouteCtrl"> <li ui-sref-active="active"> <a ui-sref="home" class="" ng-click="getPageId('live-view')">LIVE</a> </li> <li> <a ng-clic ...