Tips for ensuring that a dropdown content smoothly flows to the right when it exceeds its container's width

I am currently working on creating a drop-down component in Reactjs, but I'm facing an issue with it popping up outside the browser width.

Initially, I tried setting right: 0px, which worked well when the dropdown was at the rightmost position of the panel. However, it caused alignment issues when the dropdown was positioned elsewhere.

If you'd like to view a sample, you can check out the codepen here.

Below is a snippet of the CSS styling:

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown:hover .dropdown-content {
  display: block;
}

And here's how the HTML structure looks like:

<li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>

You may notice that there is a scrollbar present in the content.

My goal is to achieve a right-aligned dropdown content relative to the dropdown header.

Thank you!

Answer №1

To make adjustments to the dropdown contents on the right side, you can use the following method:

 <div class="dropdown-content d2">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
 </div>

.d2 { right:0; }

If you have any questions, feel free to reach out.

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  float: right;
}

li {
  float: left;
}

li a, .dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
  background-color: red;
}

li.dropdown {
  display: inline-block;
}
  
  
div.dropbtn {
  display: inline-block;
  color: black;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
  display: block;
}

.d2 {
  right:0;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<ul>
  <li><a href="#home">Home</a></li>
  <li><a href="#news">News</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content d2">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>
</ul>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
  
    <div class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">some other dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>

</body>
</html>

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

Receiving an item in place of a true/false indicator

I wrote an asynchronous function that validates the permission status on a device. Here is the function: checkPermission = async () => { const allowed = await requestNotifications(['alert', 'sound']).then((res) => { if ...

Enable automatic importing for my React library in vscode

I am currently in the process of developing a library of components specifically for React, with the intention of publishing it on npm. To achieve this, I am using webpack and babel to compile the code to ES5. Most of the setup has been successful, howeve ...

Tips for making a hide and reveal FAQ section

Currently working on creating a FAQ page for my website, I ran into a challenge. My goal is to have a setup where clicking on a question will reveal the answer while hiding any previously open answers. So far, I have managed to achieve this functionality p ...

The ASP website displays English numbers in a Persian format

Hello, I am facing an issue with my multi-language website that supports English and Persian. I have utilized font face to showcase different fonts for the two languages. However, when I add a Persian font to the font-face, all numbers, including English ...

Tips for avoiding text wrapping on items

Here is the code snippet I'm working with: <ion-list> <ion-item> <ion-avatar item-left> <img src="img/avatar-small.jpg"> </ion-avatar> <h2>Alissa Connor said something</h2> <p>S ...

Having trouble accessing the data I'm setting in a separate component using the Context API

Currently working on my own project and I've encountered a problem while using the Context API. It's my first time using it. The issue I'm facing is that I can't seem to console.log the data I'm setting. I'm trying to create ...

Struggling to break down a URL into separate elements such as http:, www., stackoverflow, questions, and ask

While the input is working fine and producing the expected string output, I am struggling with creating arrays and not sure what to do next. Despite searching extensively, I have been unable to find a solution. <body> <form> &l ...

In a Twitter Bootstrap environment, the button cursor transforms into an I-beam when activated

After utilizing twitter bootstrap to construct a small blog site and integrating a third-party file upload application into the project, I encountered an issue. The CSS files in the file uploader style the "upload button," but when my cursor hovers over it ...

The alignment of Div elements is off in IE8

I have been struggling to align two divs side by side without any empty spaces on IE 8. Everything looks perfect on Chrome Version 35.0.1916.153 m and Firefox 30.0, but IE 8 seems to be causing some issues. All I want is a main div with two child divs ali ...

Encountering an unusual issue: Unable to access undefined properties (specifically 'get')

I'm struggling to get the order history screen to display the order history of a specific user. Every time I navigate to the path, I encounter the error mentioned in the title. I double-checked the path for accuracy and made sure there are no spelling ...

Bootstrap 5 - Dropdown menu - Links unresponsive to user interaction

Often, I want to incorporate a Bootstrap 5 dropdown menu into my project, but I encountered an unexpected issue. The menu opens correctly, but when I hover over the menu items, the hover effect is not triggered, and clicking on a link does not work. It ap ...

Mastering the Art of Stacking with FontAwesome

I am working on stacking the Soundcloud icon manually to display it within a square similar to fa-facebook-square. However, my Soundcloud icon is appearing under the square. This is the code I have so far: <div class="row"> <ul style="list-s ...

Encountering a white screen following the execution of npm run build on a React

When trying to deploy my application after running npm run build, I am encountering a white screen upon opening. The issue seems related to the file build/index.html, even though I have set the homepage as "." in package.json. I have attached ...

Managing multiple API calls in React: Tips and tricks

In my React application, I have three components that each utilize the useAxios.js hook to make separate API calls to fetch data from different endpoints. Each component returns data, error, and loading states. Now, I am looking to implement a loading scre ...

Securing your Express server, ReactJS, and Relay application with a robust authentication method using Passport

After spending several weeks working on my ReactJS application, the next step is to implement authentication. However, I find this process quite perplexing. My application consists of: An Express server running a GraphQL Server over mongoose/MongoDB on ...

"Navigate with ease using Material-UI's BottomNavigationItem and link

What is the best way to implement UI navigation using a React component? I am currently working with a <BottomNavigationItem /> component that renders as a <button>. How can I modify it to navigate to a specific URL? class FooterNavigation e ...

How can I customize an SVG using CSS when it's being accessed from another directory on my computer?

I find myself in a bit of a bind at the moment. Within my code lies an SVG depicting a map of the United States. Upon clicking on a specific state, the entire country fades away, revealing a separate SVG displaying only that state. Each of the 50 states is ...

Avoid flickering on the page due to inherited body background color when toggling themes by using JavaScript to immediately change the body background upon window load

I am currently working on implementing a theme toggle with local storage using JavaScript. The issue I am facing is that when switching from a dark theme to a light theme and then navigating to a new page, the background blinks black briefly because the b ...

The Bootstrap Input-Group with Spinner displays an unusual spinning shape that resembles the letter D

I am encountering an issue with a unique form that sends each line of input as a separate request upon submission. I am looking to add a spinner at the end of each line, but I am facing a problem where instead of a circular spinner, I am getting a spinning ...

What could be causing the button on my form not to activate when valid input data is entered? Check out the code sandbox for more details

One of the features I am currently working on involves using Material UI's Autocomplete multiple TextField, React Hook Form, and Yup for form validation. The form I have created prompts users to input their preferred days of the week. If they select ...