1

I have something like

<div>
  <div class="a">content</div>
</div>
<div>
  <div class="a">content</div>
</div>
<div>
  <div class="a">content</div>
</div>

Is there a way to apply an adjacent sibling type rule to class a, given that they are contained within another element?

For example, I'd like to be able to do:

a + a { font-size: smaller; }

to apply a rule to all but the first div. However, this won't work with the traditional + operator, because it only applies to directly adjacent elements.

David
  • 123
  • 7

3 Answers3

1

General sibling combinator ~

div ~ div .a{       /* or: div ~ div > .a Or rather div.parent ~ div.parent .a */
  font-size:smaller;
}
<div>
  <div class="a">content</div>
</div>
<div>
  <div class="a">content</div>
</div>
<div>
  <div class="a">content</div>
</div>

preferabily use a parent class selector to be more specific

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

The only way you can do it in pure CSS is give the parent divs a class:

<div class="wrapper">
  <div class="a">content</div>
</div>
<div class="wrapper">
  <div class="a">content</div>
</div>
<div class="wrapper">
  <div class="a">content</div>
</div>

CSS

.wrapper + .wrapper a {
styles here
}

You could also use Javascript/jQuery to target the parents of your .a divs. You might have to combine jQuery each() and parent()

disinfor
  • 10,865
  • 2
  • 33
  • 44
-1
a:not(:first-of-type){font-size: smaller;}
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
  • Did you test this? Syntactically, I guess it should be `a:not(:first-of-type)`. But it won't work anyway, because **all** the `a` tags are `first-of-type`, since `first-of-type` refers to position under parent. –  Jul 14 '15 at 04:12