conceal a component within a different container

Is there a way to hide or show an element within a container based on the hover state of another container?

Here is an example of what I have tried so far:

HTML5

<div class="left-menu-container">
    <div class="left-menu-inner-container">
        <div class="left-menu-item-container">
            <a href="AppsDashboard" class="left-menu-link">
                <div class="left-menu-item-first">
                    Find an Application
                </div>
            </a>
            <div class="sub-menu">
                <input type="text" value="Enter app name here" onclick="Clear(this, 'Enter app name here');" onblur="Reset(this, 'Enter app name here');" />
            </div>
        </div>
    </div>
</div>

CSS3

div.left-menu-container {
    float: left;
    width: 19%;
    padding-right: 1%;
}

div.left-menu-inner-container {
    width: 100%;
}
div.left-menu-item-container
{
    width:100%;
}

div.left-menu-item-first {
    width: 93%;
    border: 1px solid #999;
    -webkit-border-top-right-radius: 10px;
    -moz-border-radius-topright: 10px;
    border-top-right-radius: 10px;
    transition: 0.15s ease-in-out;
    color: black;
    min-height: 26px;
    padding-left: 1%;
}

    div.left-menu-item-first:hover {
        width: 97%;
        border: 1px solid #999;
        color: white;
        background-image: linear-gradient(rgb(44,119,208),rgb(27,90,159));
        padding-left: 3%;
    }

div.left-menu-item-container .sub-menu {
    width: 97%;
    border: 1px solid #999;
    color: white;
    background-image: linear-gradient(rgb(44,119,208),rgb(27,90,159));
    padding-left: 3%;
    display: none;

}

div.left-menu-item-first:hover left-menu-item-container.sub-menu {
    position:absolute;
    width: 80%;
    border: 1px solid #999;
    color: white;
    background-image: linear-gradient(rgb(44,119,208),rgb(27,90,159));
    float:left;
    display: block;
    z-index: 1000;
    float: left;
}

Unfortunately, this method does not seem to work as intended. Hovering over the left-menu-item-first div does not reveal the submenu within the left-menu-item-container parent. Is it necessary for them to be direct children for this Pure CSS approach to function properly? While I have already implemented a jQuery solution, I am interested in achieving this effect using solely CSS if possible.

Answer №1

When hovering, you have the ability to modify child and sibling elements but not an entirely different element.

If you position the submenu directly after the initial menu item, you can utilize the sibling selector + like this:

div.left-menu-container {
    float: left;
    width: 19%;
    padding-right: 1%;
}

div.left-menu-inner-container {
    width: 100%;
}
div.left-menu-item-container
{
    width:100%;
}

div.left-menu-item-first {
    width: 93%;
    border: 1px solid #999;
    -webkit-border-top-right-radius: 10px;
    -moz-border-radius-topright: 10px;
    border-top-right-radius: 10px;
    transition: 0.15s ease-in-out;
    color: black;
    min-height: 26px;
    padding-left: 1%;
}

    div.left-menu-item-first:hover {
        width: 97%;
        border: 1px solid #999;
        color: white;
        background-image: linear-gradient(rgb(44,119,208),rgb(27,90,159));
        padding-left: 3%;
    }

div.left-menu-item-container .sub-menu {
    width: 97%;
    border: 1px solid #999;
    color: white;
    background-image: linear-gradient(rgb(44,119,208),rgb(27,90,159));
    padding-left: 3%;
    display: none;

}

.left-menu-link:hover + .sub-menu {
    position:absolute;
    width: 80%;
    border: 1px solid #999;
    color: white;
    background-image: linear-gradient(rgb(44,119,208),rgb(27,90,159));
    float:left;
    display: block;
    z-index: 1000;
    float: left;
}
<div class="left-menu-container">
    <div class="left-menu-inner-container">
        <div class="left-menu-item-container">
          <span class="AppsDashboard">
            <a href="AppsDashboard1" class="left-menu-link">
                <div class="left-menu-item-first">
                    Find an Application
                </div>
            </a>
            <div class="sub-menu">
                <input type="text" value="Enter app name here" onclick="Clear(this, 'Enter app name here');" onblur="Reset(this, 'Enter app name here');" />
            </div>
            </span>
        </div>
    </div>
</div>

Answer №2

To work around the lack of a CSS parent selector, consider adjusting your HTML so that the hovered item is a sibling of left-menu-item-container.sub-menu. You can achieve this by utilizing a CSS sibling selector:

Update the following code snippet:

div.left-menu-item-first:hover left-menu-item-container.sub-menu

to

div.left-menu-item-first:hover + left-menu-item-container.sub-menu

Alternatively,

Change:

div.left-menu-item-first:hover left-menu-item-container.sub-menu

to

.left-menu-link:hover + left-menu-item-container.sub-menu

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

Having trouble with your website div not appearing correctly on Firefox?

I'm facing an issue with the display of a website (found here). The elements inside the div with the class name of .index_container are not appearing correctly in Firefox. Despite being present, they remain invisible. I've attempted to resolve th ...

Incompatibility Issue with Ajax Request on iPad 1 (iOS 5.1.1)

Having an issue with reloading content on my iPad web app. The get request works fine in the browser, but I'm experiencing trouble in Safari on the iPad 1 (iOS 5.1.1). Any suggestions on how to fix this? <script type="text/javascript"> ...

Partially filling circles for a personalized coin-slider experience

Looking to create a unique coin-slider that changes the filled factor of a coin as it moves through a specific range of images. For example, when scrolling through 10 images, the filled part of the coin would transition from 0 to 1 depending on which image ...

Initiate the printing process by executing the window.print() function. As the document is being

Here's the code snippet : <body> <div class="headerCont noprint"> <div class="headerHold"> <div class="logoCont"><img src="images/logo.jpg" width="104" height="74" alt="Logo"> ...

When attempting to load a CSS file, Java automatically redirects to the login page

EDIT** After successfully loading CSS, I encountered an issue where the page kept redirecting back to login.jsp instead of displaying the CSS. Any insights on what might be causing this?https://i.sstatic.net/Ij7Oh.png I attempted to incorporate a custom ...

When the div is loaded, automatically navigate to the crucial li element within it, such as the one with the class "import

Welcome to my first webpage: <html> <head> <script type="text/javascript" src="js/jquery.min.js"></script> </head> <body> <div id="comment"></div> <script type="text/ja ...

Invoke a JavaScript function once the div has finished loading

After clicking on a radio button, I am dynamically loading a div element. I want to hide a specific part of the div once it is loaded. $(function($) { $('.div_element').on('load', function() { $('.textbox').hide(); } ...

Having a Jquery resizing problem? No worries! When the width is less than 768, simply enable the click option. And when the width is

HTML <div class="profile"> <a href="#" class="hoverdropdown" onclick="return false;">Profile</a> <ul class="dropdown" style="display: none;"> <li><a href="#">Dashboard&l ...

What is the best way to implement CSS in this JavaScript Fetch code in order to manipulate the text's position and font style

Hello, I am just starting out with JS. Is there a way for me to customize the position and font of text in this JS Fetch function using CSS? Any help will be greatly appreciated. let file = 'art.txt'; const handleFetch = () => { fe ...

Troubleshooting Problem with Web Forms and Bootstrap Form Design

Currently, I am utilizing a simplified version of the Admin LTE Bootstrap Theme in an ASP.NET Web Forms Project. In this project type, only one form tag can be used per page. The form tag must encapsulate both the Top (Nav) and Bottom (Side Bar Left & Mai ...

Tips for positioning an advertisement at the center of a full-screen HTML5 game

I am struggling to find a way to perfectly center my advertisement in a fullscreen HTML5 game. The game automatically expands to fullscreen when played, and I want the advertisement to always be positioned in the center, regardless of whether the game is b ...

Combining numerous base64 decoded PDF files into a single PDF document with the help of ReactJS

I have a scenario where I receive multiple responses in the form of base64 encoded PDFs, which I need to decode and merge into one PDF file. Below is the code snippet for reference: var pdf_base1 = "data:application/pdf;base64,JVBERi0xLjQKJfbk/N8KMSAwIG9 ...

Discovering the screen width and increasing its value using CSS

Is there a way to determine the screen size and then add 253px to it? For example, in CSS: .element { width: calc(100% + 253px); } What would be the best approach to achieve this? ...

Why are my subpages not appearing when using nodejs?

Currently, I'm in the process of creating a basic node app for my website. So far, I have set up my app.js, controllers, and routers. However, I am encountering an issue where two out of three subpages return a 404 error, while the index page works ...

When trying to load AJAX, it does not function properly unless the page is refreshed

I'm currently working on a web page and encountering some difficulties. Within my HTML, I have two divs that are refreshed using AJAX. The issue is that the content loading seems to be inconsistent unless I manually refresh the page or implement a tim ...

Unable to retrieve input value from dynamically-generated field with JQuery

There seems to be an issue with receiving a value from a static field when using the keyup method based on the input field class (.inputclass). Once a field is added dynamically, it does not get the value. Below is a sample code. Any help would be appreci ...

Switch up your text display using JQuery and toggle between different texts

I have implemented a jQuery script to toggle the display of certain paragraphs when the "More" link is clicked. However, I am facing an issue where the link always displays "More" even after the content has been expanded. I want it to change to "Less" once ...

Transferring an image between two <td> cells within the same table

For a project, I'm working on creating a Checkers game. I have the checker board and pieces ready, but I'm struggling with the movement logic. My goal is to enable the image to move or transfer from one td element to another when clicked. A frie ...

Display XML information when a row in the table is selected

I am working with an XML data sheet and utilizing ajax to extract information from it in order to generate specific tabs and construct a table of data. However, I am encountering a challenge when attempting to display details in adjacent divs once a row of ...

Alter appearance of images depending on browser window size

I am working on an angular JavaScript code snippet that uses the ng-repeat command to display a row of small images. I want to ensure that these images do not spill over into a second line when the browser window is resized. Instead, I prefer them to eith ...