0
<div class="prev-button"></div>
<div class="container">
    <div class="product"></div>
</div>

Is it possible to get "prev-button" element by "product" element In CSS ?

I mean something like ".prev-button! .product" for example.

Although it seems to be crazy to do this way, it is possible, isn't it?

InOrderToLive
  • 186
  • 1
  • 1
  • 9

2 Answers2

1

You are asking for a CSS selector this way: “Is there a CSS parent selector?”.

Starting your selector at .product, you cannot ascend the DOM tree to give styles to parents or siblings respectively. You can either change the structure of your markup to match your wishes, or you can use JavaScript to select the elements you want to modify.

The nearest thing to this is, for instance, div.container:hover > div.product{color:red;}, which alters the children when the parent is hovered.

So no, there is no “parent’s previous sibling” selector in CSS. You can’t achieve it with child selectors (>) or sibling selectors (+).

Community
  • 1
  • 1
dakab
  • 4,576
  • 8
  • 38
  • 56
0

if you are using jQuery then you may get this

$('.product').parent().siblings('.prev-button').addClass('prevButton');

and then you can define css for .prevButton in stylesheet

this is one possibility http://jsfiddle.net/49Jpt/4/

Pravin Waychal
  • 2,253
  • 1
  • 15
  • 24