3

I tried removing border for the last-child with class name "true". :last-child is not working.

<div>
    <div class="common true">1111</div>
    <div class="common">2222</div>
    <div class="common true">3333</div>
    <div class="common true">4444</div>
    <div class="common true">5555</div>
    <div class="common true">6666</div>
    <div class="common true">7777</div>
    <div class="common">8888</div>
</div>

css:

.common.true {
    display: block;
    border-top: 1px solid;
} 

.true:last-child {
    border-top: none;
} 

JSFIDDLE: http://jsfiddle.net/C23g6/1253/

dingo_d
  • 11,160
  • 11
  • 73
  • 132
Radha
  • 85
  • 1
  • 7
  • 2
    CSS has no selector to select the last element with a specific class. It has only `last-child` and `last-of-type`. Neither works for this case. You need to use JS. (Related threads - http://stackoverflow.com/questions/18995362/last-child-not-working-as-expected/18995451#18995451 and http://stackoverflow.com/questions/2717480/css-selector-for-first-element-with-class/8539107#8539107). – Harry Nov 16 '15 at 08:18
  • I haven't voted to close as dupes just in-case you want to edit and accept JS/jQuery answers. – Harry Nov 16 '15 at 08:24
  • if you can then add a class in your last true element
    7777
    and the css .common.true.last{ border-top: 0 none; }
    – Domain Nov 16 '15 at 10:28

2 Answers2

5

You should use js to resolve it. With css you can use last-child or last-of-type, but in this case, they can't resolve the problem, because:

The :last-child selector matches every element that is the last child of its parent.

in your case .true isn't the last child

and

The :last-of-type selector matches every element that is the last child, of a particular type, of its parent.

in your case all elements are div and you can't select the div with specific class with last-of-type

you can try:

$(".true").last().css( "border-top", "none" );

jsfiddle

Aminesrine
  • 2,082
  • 6
  • 35
  • 64
0

The :last-of-type selector allows you to target the last occurence of an element within its container.

.true:last-of-type {
    border-top: none;
} 
Adeeb basheer
  • 1,422
  • 10
  • 19