Attempting to display divs at the bottom when hovering over menu options

I need help troubleshooting my HTML and CSS code. I want to be able to hover over a menu item and have the corresponding div appear at the bottom. Can you point out what's wrong with my code?

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
        <title>Trying to show a div while hovering over menu items</title>

        <style type="text/css">
        .menu_div {
        width: 300px;
        height: 50px;
        background-color:red;
        display: block;}

        .menu_div ul li {list-style: none; display:inline-block;}

        .show_div ul li {display: inline-block;}

        .show_div_one {
        width: 300px;
        height: 50px;
        background-color: orange;
        margin-top: 50px;
        display: none;
        }
        .show_div_two {
        width: 300px;
        height: 50px;
        background-color: orange;
        margin-top: 50px;
        display: none;
        }
        .menu_div ul li.menu_item_one:hover + .show_div ul li.show_div_one{display:block;}
        .menu_div ul li.menu_item_two:hover + .show_div ul li.show_div_two{display:block;}
        </style>
    </head>
    <body>
        <div class="menu_div">
        <ul>
            <li class="menu_item_one">
                <a href="">Home</a>
            </li>
            <li class="menu_item_two">
            <a href="">About</a>
            </li>
        </ul>
        </div>
        <div class="show_div">
            <ul>
                <li>
                    <div class="show_div_one">
                    </div>
                </li>
                <li>
                    <div class="show_div_two">
                    </div>
                </li>
            </ul>
        </div>


    </body>
</html>

Answer №1

Your CSS selectors may appear to use the + adjacency operator, but they are not actually doing so.

The direct adjacency selector is meant for DOM elements that immediately follow each other. To target the elements you want to display in your HTML, you would need to navigate upwards to the parent element menu_div, then sideways to its sibling show_div, and finally downwards to the correct child. This kind of traversal is not possible with CSS.

More information can be found on MDN

(+) This is known as an adjacent selector. It selects only the specified element that immediately follows the previously specified element.

To achieve the desired outcome, you will need to rearrange your code so that the element you want to display is placed directly after the element you intend to hover over. Additionally, you might want to adjust its positioning by using position:absolute.

Check out the Demo Fiddle

<div class="menu_div">
    <ul>
        <li class="menu_item_one"> <a href="">Home</a>

            <div class="show_div_one">show me!</div>
        </li>
        <li class="menu_item_two"> <a href="">about</a>

            <div class="show_div_two">show me!</div>
        </li>
    </ul>
</div>

CSS

.menu_div {
      width: 300px;
      height: 50px;
      background-color:red;
      display: block;
  }
  .menu_div ul li {
      list-style: none;
      display:inline-block;
  }
  .show_div ul li {
      display: inline-block;
  }
  .show_div_one, .show_div_two {
      width: 300px;
      height: 50px;
      background-color: orange;
      margin-top: 50px;
      display: none;
      position:absolute; /* <--- maintain the flow you expect */
  }
  .menu_div ul li.menu_item_one a:hover + .show_div_one {
      display:block;
  }
  .menu_div ul li.menu_item_two a:hover + .show_div_two {
      display:block;
  }

Answer №2

Instead of just using CSS, consider implementing JQuery for a more efficient solution.

Check out this example: Fiddle

$( document ).ready( function() {
     $('.show_div_one').hide();

    $('.menu_item_one').hover(
    function(){  
        $('.show_div_one').show();
    },
    function(){  
        $('.show_div_one').hide();
    }                 
    );
});

This code has not been tested, but it illustrates the concept of showing and hiding elements based on hover events.

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

The footer row in jqgrid is experiencing issues with functionality

I am trying to create a jqgrid table with three footer rows to display a total at the end of each grid. Following the guidance in this helpful post from Oleg on How to create two footer rows in jqgrid, I was able to modify the code provided to show three t ...

Tips for Implementing Multi-Level/Nested in Ajax Requests

I am currently developing a user-friendly web application with intuitive multi-level options that loads new content without the need to refresh the entire page. This is how my structure looks: /index.php /schools.html /colleges.html Within schools.html, ...

Chrome is experiencing a problem with anchor tags that have an href attribute set to a "blob:" URL and using a target of "_blank"

My current project involves developing a website feature that allows users to download a PDF version of a page. To achieve this, the generated HTML is rendered into a PDF on the server, which is then returned as a Base64-encoded PDF. This data is converted ...

div positioned on top of additional div elements

My HTML code consists of simple div tags <div class="left">Project Name: </div> <div class="right">should have a name</div> <div>Shouldn't this be on a new line?</div> These classes are defined in a stylesheet as ...

Display the list items when the page first loads

I currently have this code snippet: <nav> <ul class="sel"> <li> <a href="#LINK1" data-title="A">A</a> </li> <li> <a href ...

Styling Process Steps in CSS

Just starting out with CSS! I'm looking to replicate the Process Step design shown in the image. https://i.stack.imgur.com/Cq0jY.png Here's the code I've experimented with so far: .inline-div{ padding: 1rem; border: 0.04rem gray ...

A straightforward guide on utilizing data-attributes to achieve a linear-gradient background

considering the following input .prettyRange { -webkit-appereance: none; } .prettyRange::-webkit-slider-runnable-track { background: -webkit-linear-gradient(right, #fff 0%, green 50%, #fff 0%); } <input class="prettyRange" type="range" name="cap ...

What is a more effective way to showcase tab content than using an iframe?

I currently have a webpage set up with three tabs and an iframe that displays the content of the tab clicked. The source code for each tab's content is stored in separate HTML and CSS files. However, I've noticed that when a tab is clicked, the ...

How can I make a child of Object3d in Three.js face the camera?

One challenge I am facing involves an Object3d composed of 6 planes arranged to form a cube. After applying quaternion rotation based on mouse input and bringing the cube to a stop, my goal is to have the cube align itself directly with the camera at its c ...

Passing a JavaScript variable to a URL parameter can be achieved by

Can anyone advise me on passing JavaScript string variables and displaying them in the URL? For instance, let's say the URL is "xyc.com". Upon populating a variable (a), I wish for that variable to appear in the address bar as URL = "xyc.com/?a=". Cur ...

Link clicking does not trigger URL routing properly

I've been tasked with updating our web application, and I'm facing a challenge that I need help with. My boss wants users to be able to click on a link in an email and have the internal company web app directly navigate to a specific page indicat ...

Unable to refresh the fullcalendar section following an ajax post click

Currently developing a calendar using fullcalendar. I have created an ajax button that retrieves events from another php page. The first click on the ajax button works fine, displaying a nice month calendar with events. However, my issue arises when I cl ...

Expanding the size of select dropdown in Material UI to accommodate more options

Is there a way to increase the width of the drop down to 400px? I tried adding inline styles, but it didn't work. Upon debugging, I realized that the width is being overridden by this class. I'm not sure how to overwrite it. Could you please prov ...

Enlarge the icon so that it is bigger than the text, but ensure that both the icon and

Can someone please help me figure out how to align the icon with the text on this button? Here is the current code I have: .btn { -webkit-border-radius: 4; -moz-border-radius: 4; border-radius: 4px; font-family: 'Raleway', sans-serif; ...

Is it possible to encounter the issue of "Context Lost" and "Importing multiple instances" in Three.js?

I am in the process of developing a 3D editor that allows users to manipulate a 3D scene and then view the result by pressing a "play" button. In order to display the output, I am utilizing an iframe. Below is the snippet of my HTML code: <iframe id=&qu ...

Tips for creating a dynamic design for an Inputfield when it is in use

Imagine if the background color is red and I want the input text field to be red as well. However, when you click inside the input field to type, it should turn into a regular text field. And of course, when you are not actively typing in the input field, ...

Guide to creating a simple multi-column index linking each item to a specific page reference

Here's an idea I have in mind: Alan 12 | Byron 104 Alfred 54 | Charles 33 Ann 28 | Cleopatra 7 Basil 133 | Corey 199 ...

Issue with margins of sections when attempting to activate menu class while scrolling

My Objective: I want to create a website where the menu highlights the section that is currently being viewed as the user scrolls up and down. Progress So Far: I have successfully implemented a functioning menu that changes to "active" when the correspo ...

Transferring the link value to an AJAX function when the onclick event is triggered

I have a link containing some data. For example: <li><a href="" onclick="getcategory(this);"><?php echo $result22['category']; ?></a></li> I need this link to pass the value of $result22['category']; to ...

Analyzing neglected CSS in intricate websites

While there are tools available to identify unused CSS on static web pages, real-world scenarios often involve CSS being used dynamically after interactions such as opening modals or popups. How can we effectively manage our ever-growing render-blocking CS ...