Creating separation between a dropdown menu and its corresponding button

I'm dealing with a situation where I have a button that reveals a hidden droplist upon hover. The problem is that there is no space between the button and the droplist, causing interference with the functionality.

My attempt to create some distance by adding margin to the droplist resulted in triggering the hover function prematurely when hovering over that newly added space before reaching the droplist itself.

Below is the code snippet showcasing this issue...

/* STYLING FOR THE DROPLIST BUTTON */

.droplist {
  position: relative;
  display: inline-block;
  user-select: none;
  float: right;
}

.droplist button {
  width: 150px;
  border: none;
  background-color: #585858;
  transition-duration: 0.3s;
  padding: 20px;
  color: white;
  font-family: "Arial";
}

.dropcontent {
  display: none;
  position: absolute;
  background-color: #393939;
}

.dropcontent a {
  display: block;
  text-decoration: none;
  color: white;
  padding: 15px;
  font-family: "Arial";
  transition-duration: 0.1s;
  min-width: 200px;
}

.droplist:hover button {
  background-color: #333333;
  transition-duration: 0.5s;
}

.droplist:hover .dropcontent {
  display: block;
}

.dropcontent a:hover {
  color: #C0C0C0;
  transition-duration: 0.1s;
}


/* STYLING FOR THE DROPLIST BUTTON */
<div class="droplist" style="margin: 10px; outline: none">
  <button>GAMES</button>
  <div class="dropcontent">
    <a href="#">STICK FIGHT: THE GAME</a>
    <a href="#">CLUSTERTRUCK</a>
    <a href="#">SQUARE BRAWL</a>
    <a href="#">NUCLEAR BUSINESS</a>
  </div>
</div>

Answer №1

Instead of keeping the dropdown close to the bottom edge of its parent, it is advisable to add some margin to the button.

Reasoning:

The dropdown has an absolute position set, which means it does not impact the height of its parent container (.droplist) and appears outside of it. By adding margin to the button, you create a gap outside of the .droplist, making the dropdown more visible and avoiding hover issues (you can add a border to the container to observe this).

/* THE DROPLIST BUTTON */

.droplist {
  position: relative;
  display: inline-block;
  user-select: none;
  float: right;
}

.droplist button {
  width: 150px;
  border: none;
  background-color: #585858;
  transition-duration: 0.3s;
  padding: 20px;
  color: white;
  font-family: "Arial";
  margin-bottom:10px; /* Adding margin here */
}

.dropcontent {
  display: none;
  position: absolute;
  background-color: #393939;
}

.dropcontent a {
  display: block;
  text-decoration: none;
  color: white;
  padding: 15px;
  font-family: "Arial";
  transition-duration: 0.1s;
  min-width: 200px;
}

.droplist:hover button {
  background-color: #333333;
  transition-duration: 0.5s;
}

.droplist:hover .dropcontent {
  display: block;
}

.dropcontent a:hover {
  color: #C0C0C0;
  transition-duration: 0.1s;
}


/* THE DROPLIST BUTTON */
<div class="droplist" style="margin: 10px; outline: none">
  <button>GAMES</button>
  <div class="dropcontent">
    <a href="#">STICK FIGHT: THE GAME</a>
    <a href="#">CLUSTERTRUCK</a>
    <a href="#">SQUARE BRAWL</a>
    <a href="#">NUCLEAR BUSINESS</a>
  </div>
</div>

Answer №2

To enhance the user experience, incorporate a droplist-hover feature that adds padding-bottom:10px to this div when hovered over. This slight expansion of the droplist div by 10 pixels at the bottom ensures that the dropcontent section remains visible even when the mouse is moved outside from the bottom. Additionally, it prevents the dropcontent from opening prematurely before the mouse enters the button area. To distinguish between elements, apply margin-top:10px to .droplist:hover .dropcontent. Refer to the provided snippet for implementation details.

.droplist {
  position: relative;
  display: inline-block;
  user-select: none;
  float: right;
}

.droplist button {
  width: 150px;
  border: none;
  background-color: #585858;
  transition-duration: 0.3s;
  padding: 20px;
  color: white;
  font-family: "Arial";
}

.dropcontent {
  display: none;
  position: absolute;
  background-color: #393939;
}

.dropcontent a {
  display: block;
  text-decoration: none;
  color: white;
  padding: 15px;
  font-family: "Arial";
  transition-duration: 0.1s;
  min-width: 200px;
}

/*Implement padding on hover*/
.droplist:hover{
  padding-bottom:10px;
}

.droplist:hover button {
  background-color: #333333;
  transition-duration: 0.5s;
}

/*Set margin-top to 10px here*/
.droplist:hover .dropcontent {
  margin-top:10px;
  display: block;
}

.dropcontent a:hover {
  color: #C0C0C0;
  transition-duration: 0.1s;
}
<div class="droplist" style="margin: 10px; outline: none">
  <button>GAMES</button>
  <div class="dropcontent">
    <a href="#">STICK FIGHT: THE GAME</a>
    <a href="#">CLUSTERTRUCK</a>
    <a href="#">SQUARE BRAWL</a>
    <a href="#">NUCLEAR BUSINESS</a>
  </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

Elements are unresponsive to scrolling inputs

My Ionic 2 input elements are not scrolling to the top when the keyboard is shown. I've tried everything I could find on Google, making sure the keyboard disable scroll is set to false. However, I still can't figure out what's causing the sc ...

Is there a way to delete content in HTML after a particular text using Python and BeautifulSoup4?

I am currently in the process of scraping information from Wikipedia. My goal is to extract only the necessary data and filter out any irrelevant content such as See also, References, etc. <h2> <span class="mw-headline" id="See ...

Using Jquery to toggle columns based on their index and custom attribute

My table has two rows in the header: the first row with colspan and the second row containing headers. You can see an image below for reference. With a jQuery function, I extract all the table cell values from the second header row and add them to a div w ...

The Chocolat.js Lightbox plugin is experiencing issues with jQuery as it is unable to detect the

I am in the process of integrating chocolat.js into a basic website. An issue I encountered was that the thumbnail was directly processing the anchor link, which resulted in the image opening in a new window instead of launching it in a modal on the screen ...

Value of an object passed as a parameter in a function

I am trying to use jQuery to change the color of a link, but I keep getting an error when trying to reference the object. Here is my HTML : <a onmouseover="loclink(this);return false;" href="locations.html" title="Locations" class="nav-link align_nav" ...

Experiencing difficulty moving information from React form to DATA.txt file through Express

I've tried various things, but I keep encountering the same error. Changing the file path didn't seem to make a difference. The current structure of my project is as follows: {nodemodules,public,scr (containing all files including App.jsx),DATA. ...

Click Event for Basic CSS Dropdown Menu

My goal is to develop a straightforward feature where a dropdown menu appears below a specific text field once it is clicked. $(".val").children("div").on("click", function() { alert("CLICK"); }); .container { display: inline-block; } .val { d ...

In instances where the text exceeds the width of the container, a horizontal scrollbar will appear

When looking at the custom content section on my website, you will find paragraphs and lists. One of the list items contains the following text: Track your package online: The link provided is quite lengthy and lacks white spaces, causing it to extend ...

What is the proper way to add process.env.PORT to the script declarations string within my package.json file?

I am facing an issue while trying to upload my file on Heroku that requires including the PORT specification. Unlike my previous projects, this project consists of only html, js, and css files instead of an index.js file. To run it, I am using "live-server ...

Using the .slider class on concealed div elements

Explaining this may be a bit tricky, but here we go... I have multiple hidden divs that switch with each other when a link is clicked using $(document).ready(function(){ $('a').click(function () { var divname= this.name; $("#"+divname ...

Include chosen select option in Jquery form submission

Facing some challenges with a section of my code. Essentially, new elements are dynamically added to the page using .html() and ajax response. You can see an example of the added elements in the code. Since the elements were inserted into the page using . ...

Insert multiple hyperlinks into a text field on a separate webpage and store them individually in a MySQL database

Within my PHP file, I have an HTML form that includes a textbox where users can enter multiple links. For example: www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm www.4shared.com/video/Ury ...

Having trouble getting the custom font to display correctly in xhtml2pdf for a Django project

I'm having trouble incorporating a custom font into my PDF generated from HTML. Although I successfully displayed the font in an HTML file, indicating that the font path is correct and it's being used properly, the .ttf font type doesn't re ...

Trigger a dialogue box when a button is clicked to show dynamically inserted list elements

My goal is to have a collection of list items, each with its own button that triggers a unique dialog box displaying specific text. Below is the HTML code: <ul id="filter-list" class="scrollable-menu text-center"> @foreach (var filter in ...

Utilize JavaScript or JQuery to create a dynamic pop-up window that appears seamlessly on the

Looking to create a unique feature on an HTML page? Want a clickable link that opens a "pop-up" displaying a specific image, right within the page rather than in a new tab or window? Ideally, this pop-up should be movable around the page like a separate ...

Is there a way to prevent the letters from moving when I hover over them?

What occurs when the numbers are hovered over: https://gyazo.com/20b6426d435551c5ee238241d3f96b4d Whenever I hover over the pagination numbers, they shift to the right, and I am unsure of what mistake I made in my code that's causing this. Below, I ...

What steps are involved in generating a scene dynamically with A-Frame?

Looking to transition from declarative coding in js and html to a programmatic approach with Aframe? You might be wondering if it's possible to modify your scene dynamically, here is an example of what you're trying to achieve: <!DOCTYPE html ...

Choose an input that is not grouped with the rest

I am facing an issue where I need to group these three inputs together as one continuous form. However, there seems to be a problem with the select input not aligning correctly with the rest of the form. Is there something that I might be doing wrong? & ...

What is the best way to insert horizontal padding into each line of a single sentence that is wrapped on multiple lines

Below is the code snippet I am working with: <div><p><span>... highlighted text ...</span></p><p>Chapter info</p></div> Here is a screenshot of how it currently appears: I am looking for a way to add paddi ...

I am experiencing an issue with the initial for-loop in my code as it is not

Upon clicking the start button, the score button, as well as the try again or correct button, are not being updated. Can someone help me figure out what's wrong? I believe the issue lies within this section of the code: for (i=0; i<5; i++) { do ...