Display DIV when hovering another DIV's child CSS

samsko b

Is it possible to display a DIV that's located outside the DIV's child?

<div id='container'>
    <div>
        <div>
            <div id="one">Item 1</div>
            <div id="two">Item 2</div>
            <div id="three">Item 3</div>
        </div>
    </div>
    <div class="extra" id="one-extra">Show item 1 extra info!</div>
    <div class="extra" id="two-extra">Show item 2 extra info!</div>
    <div class="extra" id="three-extra">Show item 3 extra info!</div>
</div>

Shouldn't class:hover class do the job? Or is selecting DIV's outside its own DIV not possible without Javascript?

.extra {
 display:none;
}

#one:hover #one-extra {
 display: block;
}
SEoF

The issue is to do with your selector.

A CSS selector cannot traverse upwards. Specifically, the "#one-extra" has been defined as a "descendant". It's also possible to define "latter siblings" with the "~", "next siblings" with the "+", and "child" with a ">". See https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors for more information about CSS selectors.

Solution 1 - Changing the HTML

A change to your HTML and CSS could see styling apply with no need for JS:

<div id='container'>
    <div id="one">Item 1</div>
    <div id="two">Item 2</div>
    <div id="three">Item 3</div>
    <div class="contents">
        <div class="extra" id="one-extra">Show item 1 extra info!</div>
        <div class="extra" id="two-extra">Show item 2 extra info!</div>
        <div class="extra" id="three-extra">Show item 3 extra info!</div>
    </div>
</div>

and the CSS:

.extra {
    display:none;
}

#one:hover ~ .contents #one-extra {
    display: block;
}

Solution 2 - Add JS

The alternative is to use Javascript.

A JS library might be suitable, though I strongly suggest avoiding JQuery and other old libraries these days as they are bloated and provide nothing more than can be done with ease in raw Javascript, but add significant overhead, and add an unnecessary reliance.

If you're doing a lot of these things, specifically DOM manipulation, in JS in your project, a JS framework like Vue.js or React would provide a cleaner way to do this, though it would be a big change from no JS at all adding a large amount of bloat into your project.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Hovering one item to display the child in another div

CSS hovering with in a child div

Affect another div when hovering CSS

How to display a div when hovering over another div

change css of one div by hovering on another div

Is there a way to change a target div's text when hovering over other div's using css only?

CSS objects in div move when hovering

Change the Brightness of a div when hovering over another div

How to display a div when hovering over an image using CSS/HTML + Transitions?

CSS, display: table and overflow not working properly on child div's

CSS div that appears when hovering on anchor; keeping it visible while hovering over div

Changing the style of one div when hovering over another

How to scroll up a div when hovering over another element

How to display div with hovering on a nested div?

How to trigger div visibility when hovering over list item in CSS?

How to set height of child div while hovering over a parent div in css?

Changing CSS of one element when it's hovering another

HTML display child div in new line when it exceeds parent div

How do add a class to another div, when hovering over one div?

Absolute position div don't overlays another's div child

Display another DIV when submit button is clicked

display div over another when click a link

Hovering on Div

A part of div on another div when the second div's overflow is hidden

Hovering over image and displaying text in another div

Hovering over 1 div to change content in another

CSS Expand parent div to fit child div width when zoom in

fadeIn each child div of each div while hovering

Display three links when hovering on another link