1

I have this in line:

<div class="blue-car">
<a href="#">Car</a>
</div>

<div class="iColor">
<a href="#">Blue</a>
<div>


.blue-car:hover { color: red; }

.iColor:hover { color: read; }

I would like to make when someone hover to Car div second div which iColor change css and when hover to iColor div blue-car change css.

ie. I hover to 'Car' , 'Blue' will change color to red and when I hover to 'Blue' , 'Car' will change color to red, I want to make people aware that this two link is related.

I would love to have this in css only. No jquery. I have tried many no achievement at this moment.

Let me clear this, here is an example on this site. You could see when you hover to a country map, css link on right side will change, and you could see when you hover to a country link, country map css will change. This means this two div work each other. How they do this on this site: http://www.avito.ru

user3858546
  • 83
  • 1
  • 1
  • 7

3 Answers3

3

To start, CSS does NOT have a previous sibling operator. The only siblings that can be selected are adjacent (using +) or general (using ~).

It is possible to achieve the effect that you are seeking using only HTML and CSS. Below is one solution: http://jsfiddle.net/KGabX/. Basically, the .area is displayed as a table, which makes it wrap around the link and the image. However, the link is positioned absolutely, which prevents it from being "included" in a territory wrapped by the .area. This way, the .area is wrapped only around the image. Then, hovering over the .area we highlight the link. And, by hovering over the link we highlight the image.

Markup:

<div class = "area">
    <a href = "#">Link</a>
    <img src = "http://placehold.it/100x100" />
</div>

Styles:

.area {
    display: table;
    position: relative;
}

.area:hover > a {
    color: red;
}

.area > img {
    cursor: pointer
}

.area > a {
    position: absolute;
    right: -50px;
    top: 50%;
    font: bold 15px/2 Sans-Serif;
    color: #000;
    text-decoration: none;
    margin-top: -15px;
}

.area > a:hover {
    color: initial;
    text-decoration: underline;
}

.area > a:hover + img {
    opacity: 0.5;
}
DRD
  • 5,557
  • 14
  • 14
  • I was coming to this conclusion but stopped after I couldn't find a prior element selector. +1 – Andrew Jul 20 '14 at 20:05
  • This is what I am looking for. No one can do this even in webmaster forums. Thanks mister. – user3858546 Jul 20 '14 at 20:09
  • Oh: that's what the question was. Nice job understanding what the OP was asking and actually figuring out how to implement it. Also, placehold.it is great: I've been looking for something to use as a placeholder image in my demos the way that I use example.com for links. – AstroCB Jul 20 '14 at 20:12
  • CSS4 has some interesting selectors coming up (e.g., `:blank, nth-column`), but no previous siblings. Having previous adjacent and general operators could be a big help. @user3858546: you are quite welcome. Рад помочь. – DRD Jul 20 '14 at 20:13
  • @DRD I try change img to a link but doesn't work. Could you show me how to make both link with? (without img) thanks. – user3858546 Jul 20 '14 at 20:26
1

Although I could not interpret what you wrote very well, I immediately noticed a flaw in your css selector.

Change your code to this:

 <style>
.blue-car:hover a { color: red; }
.iColor:hover a { color: red; }
 </style>

What's different about it? iColor:hover a. Look at the a, anchor selector. It was added because your previous CSS was only selecting the div. In css the child element, in this case the anchor, will supersede it's parents. There's two ways you can approach this. The first, or make the anchor tags color in css inherit.

If this wasn't your problem I'll fix my answer.

Andrew
  • 3,393
  • 4
  • 25
  • 43
  • Lets say a country map placed in one div and second one I have list of country name in plain text. When I hover to country name the map will change color and when I hover to map the name will change color. You know what I mean? this two is same like i talk to you and you talk to me and mod can't do anything. – user3858546 Jul 20 '14 at 19:37
0

I'm not quite sure what you're asking because your question is a bit unclear.

From what I can understand, your issue stems from the fact that you're referring to the color property of the div, rather than the color property of the link.

That's a simple fix: all you need to do is drill down through the div to the link.

.blue-car:hover a{
    color: red;
}
.iColor:hover a{
    color: red;
}

Demo

Keep in mind that this isn't the best way to do this unless you absolutely need to refer to the links within the context of the div. I understand that your question fits into a broader context within your code, but for the example you gave here, all you really need is this:

a:hover{
    color: red;
}

Again, I realize that you may need to change the colors or be more specific, but there's probably a better way to do this, even if that's the case.

The issue with this particular implementation is that your div is larger than your link, and a hover on your div is what activates the color change, so you'll run into this issue:

Issue with hovering

AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • Let me clear this, here is an example on this site. You could see when you hover to map css on right side will change, and you could see when you hover to the link map css will change. http://www.avito.ru – user3858546 Jul 20 '14 at 19:42
  • @user3858546 As far as I know, you can't do that without JavaScript unless you change both elements to the same color: you're trying to effect two elements at once, which is impossible unless you're going to apply the same effect to both. – AstroCB Jul 20 '14 at 19:48
  • This kind of a map would best be made in javacript and html5. You're not going to have an easy time replicating it in css. – Andrew Jul 20 '14 at 19:48
  • @Lemony-Andrew I would like to know how to make two DIV respond each other when I do hover. – user3858546 Jul 20 '14 at 19:52
  • @AstroCB I have done this before in CSS. I am getting old right now. – user3858546 Jul 20 '14 at 19:55
  • @user3858546 What you're asking still isn't quite clear: do you want a hover on one element to trigger a style change on another? – AstroCB Jul 20 '14 at 19:56
  • @AstroCB example:When Hover to 'A' css 'B' will change, When hover to B css in A will change. This to link will change Css when one of them hovered. – user3858546 Jul 20 '14 at 20:04