Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show div when hover another div using only css

Tags:

html

css

I have searched the web for this one but didn't find anything similar.

Say we have div A and div B. When hover on div A, div b should be visible ON (should look like overwriting) div A and not placed under.

It should appear like only the content of div A has changed to content of div B.

How can this be done in pure CSS and HTML?

#container > div {
    display: none
}
#container > div:first-child {
    display: block
}
#container > div:hover + div {
    display: block

<div id="container">
    <div>A</div>
    <div>B</div>
</div>
like image 788
M. El-Set Avatar asked Jan 17 '13 15:01

M. El-Set


People also ask

How do you make a hover effect a div?

You'll need a container div and at least one foreground div to cover the background (could be just an image). Then you'll want to target the parent on hover and change the foreground child. I used transform instead of animating a position property because it's more performant.

How do I show hover image in CSS?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

Can you use hover on a div?

You can apply :hover styles to any renderable element on a page. IE6 only supports that pseudo-class on links though.

How will you check if an element is hovered or not using CSS?

You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery. The jQuery code in the following example will display a hint message on the web page when you place or remove the mouse pointer over the DIV element's box.


2 Answers

This will work, but only if the two divs are adjacent and b follows a.

#a:hover + #b {
    background: #f00
}

<div id="a">Div A</div>
<div id="b">Div B</div>

If you have divs in between use ~

#a:hover ~ #b {
    background: #f00
}

<div id="a">Div A</div>
<div id="c">Div C</div>
<div id="b">Div B</div>

To go the other way around, unfortunately you will need Javascript

// Pure Javascript
document.getElementById('b').onmouseover = function(){
    document.getElementById('a').style.backgroundColor = '#f00';
}
document.getElementById('b').onmouseout = function(){
    document.getElementById('a').style.backgroundColor = '';
}

// jQuery
$('#b').hover(
  function(){$('#a').css('background', '#F00')}, 
  function(){$('#a').css('background', '')}
);

Full fiddle http://jsfiddle.net/p7hLL/5/

like image 137
PassKit Avatar answered Oct 19 '22 03:10

PassKit


If you don't want to use the selector + or ~ which aren't compatible with some browsers, it is just possible if the <div /> to show (e.g. div#b) is a child of the <div /> to hover (e.g. div#a).

<div id="a">
    <div id="b"></div>
</div>

<style>
    div#b {
        display: none;
    }

    div#a:hover div#b {
        display: block;
    }
</style>
like image 15
algorhythm Avatar answered Oct 19 '22 01:10

algorhythm