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

Creating a button in ReactJS with text displayed under an icon

Presently, I am working with a component that looks like this: https://i.stack.imgur.com/qGCwj.png This is the code for the component: import React from "react"; import {withStyles} from "material-ui/styles"; import Settings from "material-ui-icons/Setti ...

Trouble with CSS positioning

Styling with CSS: .one { width: 13%; } .two { width: 30%; } .three { width: 30%; } Formatting in HTML: <table> <tr> <th class= "one">Quantity</th> <th class= "two">Info</th> ...

How can "this" be properly utilized in jQuery?

I am attempting to retrieve the title attribute of an element from various elements with the same class, each having different attributes. This is my current approach: HTML <div title="title1" class="pager" onclick="location.href='link.aspx& ...

How to Use Vue.js to Find the Nearest Div Element with a Specific

Below is the HTML code I am working with: <div id="app"> <div class="image"> <div class="overlay"> <p>Some overlay text</p> </div> <img src="https://placeimg.com/640/480/any" class="img-fluid"> ...

IE9 Z-index Issue

Currently, I am facing an issue with two images not displaying correctly on a website that I am working on. The problem seems to be specific to IE 9, as other browsers are working fine. The trouble arises when I try to create a slideshow with a shadow back ...

IE8 is not properly handling nested HTML elements that have the display:none property set

I am currently using jQuery to create a smooth fade-in effect for an <article> element that contains a <section> element. The outer element has CSS properties of display:none, position:fixed, and z-index:5. Meanwhile, the inner element is styl ...

When removing the class "img-responsive" from an image, the bootstrap columns begin to overlap

Just starting out with Bootstrap while working on an Angular2 project and I have a question. Currently, I have a map-component taking up 3 columns on the left-hand side, but every time I resize the browser, the image also resizes. I want the image to rema ...

Attempting to enhance the modularity and readability of my code

Looking for assistance to enhance the modularity and readability of this lengthy code. Any tips on how to simplify it and improve clarity would be greatly appreciated! I'm currently working on a street fighter game, and here's what I have so far ...

Embedding images using a blob or base64 format does not function properly on iOS devices

I'm facing an issue with setting the src of an img tag to display an image. The code snippet below works fine on android, mac, and windows, but it is not functioning correctly on iOS: let base64Image = pageModel.image; this.$currentPageImage.src = `da ...

Enhancing navigation functionality using CSS

I have two separate pages with navigation included in both. This way, it's easier to edit the navigation menu once instead of doing so on each page individually. However, I am now facing uncertainty on how to implement the 'active' class usi ...

Automatically adjust the image size in CSS to fit the screen when the iPhone is rotated from portrait to landscape

I am trying to create a responsive design where an image will fit the screen without being cropped in both portrait and landscape mode. I have attempted the following code: <!doctype html> <html> <head> <style> img { max-height ...

Can MUI 5 replicate the Emotions 'css' function through analog?

From what I've learned, MUI 5 utilizes Emotion for its styling framework. Emotion offers a convenient helper function called css, which generates a string - a class name that can be used as a value for the className property or any other relevant prop ...

Tips for positioning an element at the bottom of a flexible container with variable size

How can I ensure that the Author element in this jsfiddle is aligned properly between the card elements? I am struggling to adjust the size of the details container to match the variable sizes of other elements in the same row. The aim is to have the Auth ...

Retrieve the input value closest to the selected row

I need to extract the price and currency value of a checked row containing a checkbox. The price is specified in a text box while the currency is selected from a dropdown menu. $(document).ready(function() { $('#test').click(function(){ ...

Deleting an item from a Vue.js list

I'm currently working on a project to develop a basic webpage that allows users to add or remove textboxes using vue.js. new Vue({ el: "#app", data() { return { list: [] }; }, methods: { deleteFromList(index) { this ...

What causes the entire page to disappear when the screen is adjusted to mobile width?

I'm facing an issue with my ReactJS screen. Everything works perfectly until I resize the screen to mobile width (sm of Material UI) and then everything turns into a WhiteScreen. I've been trying to debug this but can't seem to find the root ...

Retrieve a specific child element by clicking on a custom control on the map

I have a container element with three child spans. Using JQuery, I can determine which child was clicked when the container is clicked by utilizing the following function: $('.unitContainer').on('click','span',function(){ ...

The $.post method is malfunctioning

I am experiencing an issue where the onchange function is working properly, but the $.post function is not functioning as expected. Below is the HTML code snippet: <input type="checkbox" id="chk" value="3" onchange="checkradio(this.value)"/> Here ...

Looking to troubleshoot the selenium error in a loop, specifically the InvalidSelectorException that states: "invalid selector: Unable to find element with the specified xpath"?

I have been attempting to create a for loop in selenium that checks if certain buttons on a website are clickable, clicks on them, and refreshes the page until they are. However, I am encountering an error with my Xpaths as shown below (InvalidSelectorExce ...

Looking to transform an HTML table into a stylish CSS layout complete with a form submission button?

Recently, I've been delving into the world of CSS and PHP as I work on converting old code entirely into HTML and PHP with a touch of CSS. The visual aspect seems fine, but I've hit a roadblock with the submit form due to an IF statement in PHP. ...