2

I have the following: http://jsfiddle.net/2hDB9/

How can I make it so that:

  • if you hover over Line 1, it shows the border for just Line 1
  • if you hover over Line 2, it shows the border for just Line 2, not both

HTML:

<div class="container">
    <div class="box">
        Line 1.
        <div class="box">
            Line 2.
        </div>
    </div>
</div>

CSS:

.container {
    width: 500px;
}

.box {
    padding: 12px;
    border: 1px solid grey;
}

.box:hover {
    border-color: red;
}
3
  • But you want that if you hover on Line 1 outside of 2 that Line 1 gets the color? Commented Nov 22, 2013 at 20:15
  • 1
    You can't do it only with css beacuse you're always over the parent div ... with css you can't refere to parents and set stop it if i'm on one children Commented Nov 22, 2013 at 20:35
  • A good way of doing this is with jquery. First you have to import the jquery library, and then you can use the hover function. Commented Nov 22, 2013 at 23:07

4 Answers 4

1

You need two different CSS classes for them. Since you have both Div's use the same class="box", then the .box:hover command would be triggered on both div's.

For the css, you can set it so that only the inner .box class gets the hover command:

.box .box:hover { border-color: red; }

Solution: http://jsfiddle.net/2hDB9/2/

Sign up to request clarification or add additional context in comments.

1 Comment

I think my question was unclear. I've edited the original post to make it easier to understand what I need.
1

If all classes must be the same (as your title implies), then this isn't completely possible in pure CSS.

All the current answers only address part of the answer.. but if you insist, here is a jQuery solution - all elements have the same class, though I suggest you rename them, and avoid jQuery altogether.

Nonetheless, here is a solution, which doesn't require the class names to be changed:

jsFiddle example - works for all levels.

$(".box").children().hover(function () {
    $(this).parent().css("border-color", "grey");
    $(this).css("border-color", "red");
}, function () {
    $(this).parent().css("border-color", "");
    $(this).css("border-color", "");
});

Comments

0

You should give it a different class. You have the option of using JavaScript to detect which element is being hovered, but I wouldn't suggest this.

You could try

.container > .box > .box:hover

1 Comment

I think my question was unclear. I've edited the original post to make it easier to understand what I need.
-1

Updated: http://jsfiddle.net/2hDB9/11/

Basically, there's not an easy way. You kind of have to fake it, so to speak. See the code below:

HTML:

<div class="container">
    <div class="box1">
        Line 1.
    </div>
    <div class="box2">
        Line 2.
    </div> 
</div>

CSS:

.container {
    height:50px;
    padding:50px;
}

.container:hover {
}

.box2 {
    height:50px;
    width:75%;
    border:2px solid black;
    position: relative;
    top: -110px;
}

.box2:hover {
    border:2px solid red;
}

.box1{
    position: relative;
    height:50px;
    padding: 20px 20px 40px 20px;
    top: -50px;
    left: -50px;
    border:2px solid black;

}

.box1:hover{
    border:2px solid blue;
}

This makes box2 "appear" to be a child of box1 by using relative positioning, when in reality, box1 is not the parent of box2, they are siblings.

1 Comment

I think my question was unclear. I've edited the original post to make it easier to understand what I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.