Reputation: 407
I need to only select the first
and third
/last elements.
It was working fine until I decided to make them links. In my actual source the divs
are img
s
body {
background-color: black;
color: white;
}
.this:first-child {
color: red;
}
.this:last-child {
color: aqua;
}
<div class="main">
<a href="">
<div class="this">
FIRST
</div>
</a>
<a href="">
<div class="this">
SECOND
</div>
</a>
<a href="">
<div class="this">
THIRD
</div>
</a>
</div>
Upvotes: 0
Views: 80
Reputation: 193258
Your .this
elements are wrapped with another element (a
), and they are the only children. This means that they are both first and last child, so the last rule takes over. Move the :first-child
and :last-child
pseudo-selectors to the a
elements:
body {
background-color: black;
color: white;
}
a:first-child .this {
color: red;
}
a:last-child .this {
color: aqua;
}
<div class="main">
<a href="">
<div class="this">
FIRST
</div>
</a>
<a href="">
<div class="this">
SECOND
</div>
</a>
<a href="">
<div class="this">
THIRD
</div>
</a>
</div>
Upvotes: 2