CSS menu with disappearing sub-menu on hover

I'm currently tackling a CSS-only menu project, but I'm struggling with keeping the sub-menu visible when moving away from the li element.

The height should span 100% of the page, and the sub-menu should mirror the main menu, appearing to the right.

Any tips or advice on this would be greatly appreciated!

Check out the demo (http://jsfiddle.net/91t43ruo/1/)

<div class="wrapper">
  <div class="main-menu">
    <ul>
      <li><a href="#">Lorem ipsum</a></li>
      <li><a href="#">Sed id cursus</a></li>
      <li><a href="#">Co-Investor</a></li>
      <li class="has-sub-menu"><a href="#">Donec interdum</a>
        <div class="sub-menu">
          <ul>
            <li><a href="#">Ut quis bibendum</a></li>
            <li><a href="#">Praesent vestibulum</a></li>
            <li><a href="#">Sed ultrices</a></li>
          </ul>
        </div>
      </li>
      <li><a href="#">Morbi a mi blandit</a></li>
      <li><a href="#">Pellentesque</a></li>
    </ul>
  </div>
</div>

body, html{
    height:100%;
    margin:0;
    font-family:Arial;
}
a {
    cursor: pointer;
    text-decoration: none;
}
/*Wrapper*/
.wrapper {
    width: 100%;
    height: 100%;
    background-color: #CCC;
}
/* Menu */
.main-menu, .sub-menu {
    background-color: #2F759B;
    width: 200px;
    height: calc(100% - 40px);
    padding: 20px;
}
.main-menu ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
.main-menu li {
    color: white;
    background-color: #3585B0;
    line-height: 26px;
    margin-bottom: 5px;
}
.main-menu li:hover, .main-menu li.has-sub-menu{
    background-color: #43A8DD;
    cursor: pointer;
}
.main-menu a {
    color: inherit;
    margin-left: 6px;
}
/*Sub Menu*/
.sub-menu {
    display:none;
    position: absolute;
    top: 0;
    left: 240px;    
}
.sub-menu ul {
}
/*Hover*/
.main-menu li:hover > div.sub-menu{
    display:block;
}

Answer №1

There seems to be a gap between the hover and submenu because the li is too far away. To fix this, change the left: 240px on .sub-menu to left: 220px;

.sub-menu {
  display: none;
  position: absolute;
  top: 0;
  left: 220px;  
}

FIDDLE

UPDATE

To address the spacing issue while keeping the hover effect, my suggestion is to wrap the sub-menu in another div:

NEW FIDDLE

Answer №2

Give this a shot

.sub-menu {
    display:none;
    position: absolute;
    top: 0;
    left: 220px; /*Adjusting from 240px to 220px;*/ 
    padding-left: 40px; /**Include this**/
}

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 are the steps to remove scrollbars in Webmethods 8?

After transitioning from Webmethods 7 to Webmethods 8, I noticed that my portlets now have unwanted horizontal scroll bars at the bottom. Is there a way to remove them? Would CSS be able to solve this issue? Any advice would be greatly appreciated! ...

What strategies can I use to control the DOM within the onScroll event in ReactJS?

I am currently working on implementing an arrow-up button that should be hidden when I am at the top of my page and displayed once I scroll further down. However, I am facing issues with manipulating the DOM inside my handleScroll function to achieve this. ...

Altering the background color of :after while :before is being hovered over

Is it possible to modify the :after section when hovering over the :before part? Take a look at this image for reference: I want to alter the small triangle shape when the large square is being hovered over. The square uses the :before pseudo-element and ...

Navigating through content with scrollable views while utilizing the popular Angular framework

Being relatively new to famous and somewhat familiar with angular. Question: Given that everything in famous is fixed positioning, how should I go about creating a scrollable section using famous angular? HTML: This code snippet creates a grid of square ...

Set iframe or form to be in a fixed position

Having issues with my site where an iframe is contained within a div. When only one character is entered in the telephone box, validation fails and red warning text is displayed. Furthermore, after clicking in the email box and pressing tab twice, the fo ...

Selenium struggles to update dropdown menu choice

Here is the HTML code snippet in isolation: <span style="position: relative; width: 100%; display: inline-block;"> <select id="0ac88542d16d6200fb983d094f655c76_select" class="form-control"> <option value="display_value">Numbe ...

What is the best method for adding a table header that spans across the first two columns?

Here is a link that I would like you to look at: jsfiddle.net/hTRQJ/ <table class="table table-bordered table-striped"><thead> <tr> <th colpan=""> Checkin </th> ...

Tips on avoiding quotation marks in a Less variable containing a color identifier

I'm currently working on an HTML/CSS project where I aim to establish classes for labels and texts based on their color. For instance: .text-red{ color: red; } .label-white{ color: white; } In order to achieve this, my approach involves cr ...

Interactive Thumbnail Selection for HTML5 Video

Having trouble with creating thumbnails? I managed to solve the cross-domain issue using an html2canvas PHP proxy. No error messages in the Console, but the thumbnails are unfortunately not showing up - they appear transparent or white. Here is a snippet ...

Adjusting the navigation image as it passes through various div elements during scrolling

Is it possible to dynamically change an image in the navigation bar based on the user's scroll position? For example, I want pic1 to be displayed when the page content is at the top, then switch to pic2 once the user reaches the footer, and then back ...

What is the best way to utilize justify-content or align-self to perfectly center this?

Despite my efforts to center it, I'm struggling to do so. I've experimented with justify-content and align-items for each row and col class, but unfortunately, nothing seems to be effective. In the breakpoints (md, lg), I really need the 4 lorem ...

Positioning Tumblr blockquotes beneath each other, rather than within

Just diving into the world of HTML/CSS and I've been working hard on creating my own Tumblr theme (almost finished!). One issue I'm having is styling the replies, which are in blockquote form. I want them to display one after another in a line li ...

Basic Timer with Visual Background

Struggling to find the right CSS/script for a straightforward countdown timer, Here are the requirements: Countdown in days only, no need for hours, minutes, and seconds Ability to use a custom image as background I've scoured online searches but n ...

Header and footer remain unaligned

Bookstore.html <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style> ul { list-style-type: none; padding: 20px; overflow: hidden; background-color: black; width:1230px; p ...

Using the -webkit-box-reflect property along with the CSS position:absolute styling

Running on the Chrome Canary build, I have come across this HTML code snippet: <div id="debug" class="debug" >TEST</div> Additionally, here is the CSS code snippet: -webkit-box-reflect: below 0px -webkit-gradient(linear, ...

Modifying the input field value in React

I've been attempting to modify the value of an input field after selecting an item from the SelectField in the MaterialUI library. Despite my efforts, I have not yet succeeded. However, based on my research, everything I have written appears to be cor ...

What is the reason behind getComputedStyle having visibility set to visible?

What is the reason behind getComputedStyle always returning element visibility as visible even when no explicit visibility has been set? For instance: getComputedStyle($('#block1')[0],null).visibility; --- "visible" Meanwhile: $('#block1&a ...

I can view this email signature correctly in VS Code and my browser, but all the other applications I've tried so far are unable to display it correctly

Simply put, I am facing an issue with my email signature. I use eM Client, while my client uses Outlook and I've also checked it in iCloud webmail. Surprisingly, none of these platforms display my email signature correctly, but VS Code and all my brow ...

Track and store your current progress with a countdown deadline using a progress bar in Bootstrap 4

I am working on a unique way to showcase a dynamic countdown date progression using Bootstrap 4 progress bar. The challenge lies in displaying the progress percentage based on a specific date input format from an admin. After retrieving the deadline date ...

Is there a way to center the text vertically within an anchor tag?

How do I align text vertically within an anchor tag? Here's a visual representation of the issue: The text "Publisher Search," "Add a New Publisher," and "Edit a Publisher" should be aligned vertically in the "bar." This is the coding structure: & ...