94

I'm looking for a method for my hovering issue.

<div class="section">
  <div class="image"><img src="myImage.jpg" /></div>
  <div class="layer">Lorem Ipsum</div>
</div>

Now, both classes, image and layer, have borders. Both have different color for normal and hover. Is there way to make it, so if I hover layer class, both layer and image class hovering border color is active? And vise versa?

jasonleonhard
  • 12,047
  • 89
  • 66
Marko
  • 1,565
  • 2
  • 13
  • 20

7 Answers7

212

You don't need JavaScript for this.

Some CSS would do it. Here is an example:

<html>
  <style type="text/css">
    .section { background:#ccc; }
    .layer { background:#ddd; }
    .section:hover img { border:2px solid #333; }
    .section:hover .layer { border:2px solid #F90; }
  </style>
</head>
<body>
  <div class="section">
    <img src="myImage.jpg" />
    <div class="layer">Lorem Ipsum</div>
  </div>
</body>
</html>
Community
  • 1
  • 1
corymathews
  • 12,289
  • 14
  • 57
  • 77
  • 15
    Okey, here's winner!! :) I didn't know that if i have something:hover, I can continue that whit another element! (somthing:hover .second).. Everyday you learn something new.. Thanks x10 – Marko Sep 23 '09 at 08:28
  • 8
    What if the two divs (the one to hover over and the one to show) are not within the same immediate parent div? – Bikash Gyawali Oct 16 '11 at 19:07
  • 3
    you can go up the structure as far as you need to go, or you could add an item to be the parent of both. Sometimes though, if they have nothing in common you would have to resort to javascript. – corymathews Oct 17 '11 at 14:14
  • 4
    How would I go about changing one child element of a div when hovering another child element of the same div? – Cos Oct 17 '12 at 15:28
  • Thanks! I was using javascript and this CSS worked out better. Sometimes it's easier than we think. – yanike Nov 20 '12 at 07:23
  • And what about if I need to hover an internal element and display it in outer one? – Johnny_D Sep 28 '13 at 21:10
  • What? in css directive syntax, the space is the heritage operator. – yPhil Aug 04 '14 at 19:29
11

This worked for me in Firefox and Chrome and IE8...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
    <head>
        <style type="text/css">
        div.section:hover div.image, div.section:hover div.layer {
            border: solid 1px red;
        }
        </style>
    </head>
    <body>
        <div class="section">
            <div class="image"><img src="myImage.jpg" /></div>
            <div class="layer">Lorem Ipsum</div>
        </div>
    </body>
</html>

... you may want to test this with IE6 as well (I'm not sure if it'll work there).

Steve Wortham
  • 21,740
  • 5
  • 68
  • 90
10

I think the best option for you is to enclose both divs by another div. Then you can make it by CSS in the following way:

<html>
<head>
<style>
  div.both:hover .image { border: 1px solid blue }
  div.both:hover .layer { border: 1px solid blue }
</style>
</head>

<body>
<div class="section">

<div class="both">
  <div class="image"><img src="myImage.jpg" /></div>
  <div class="layer">Lorem Ipsum</div>
</div>

</div>
</body>
</html>
Viliam
  • 4,404
  • 3
  • 28
  • 30
7

This is not difficult to achieve, but you need to use the javascript onmouseover function. Pseudoscript:

<div class="section ">

<div class="image"><img src="myImage.jpg" onmouseover=".layer {border: 1px solid black;} .image {border: 1px solid black;}" /></div>

<div class="layer">Lorem Ipsum</div>

</div>

Use your own colors. You can also reference javascript functions in the mouseover command.

eykanal
  • 26,437
  • 19
  • 82
  • 113
2

You'd need to use JavaScript to accomplish this, I think.

jQuery:

$(function(){
   $("#innerContainer").hover(
        function(){
            $("#innerContainer").css('border-color','#FFF');
            $("#outerContainer").css('border-color','#FFF');
        },
        function(){
            $("#innerContainer").css('border-color','#000');
            $("#outerContainer").css('border-color','#000');
        }
    );
});

Adjust the values and element id's accordingly :)

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
brettkelly
  • 27,655
  • 8
  • 56
  • 72
  • Nice, really nice! What about if I have multiple section for same class names? Now I've tryed that for four whit same class names and when I hover one, those all changes?? Should I make running number front of all sections? Does jquery have wildcard support anykind? (okey, I try just google for it! ;)) Thanks.. – Marko Sep 22 '09 at 21:07
1

You can also do it like this. for example you have a link attached to another link and you want to show both at hovering at one you can do it simply by specifying each individual's functions then at once at hovering both.

.videoTitle:hover{
     color: #1270cf;
} 
.videoTitle:hover .copy{
     display: block;
}
Muddassar
  • 349
  • 2
  • 13
0

OR

.section:hover > div {
  background-color: #0CF;
}

NOTE Parent element state can only affect a child's element state so you can use:

.image:hover + .layer {
  background-color: #0CF;
}
.image:hover {
  background-color: #0CF;
}

but you can not use

.layer:hover + .image {
  background-color: #0CF;
}
Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
Jnr
  • 1,504
  • 2
  • 21
  • 36