2

I am displaying search results in a div:

<div id="results">
  <div class="list-item">[Text]</div>
  <div class="list-item">[Text]</div>
  <div class="list-item">[Text]</div>
  <div class="list-item">[Text]</div>
  <div class="list-item">[Text]</div>
  <div class="list-item">[Text]</div>
</div>

The number of list-items is variable. I want to have border-bottom: 1px solid #000; on all the list-item classes, except for the last result. Is there a way in CSS to do this, or do I need to use JS?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
alias51
  • 8,178
  • 22
  • 94
  • 166

3 Answers3

9

Use :not and :last-child:

.list-item:not(:last-child) {
  border-bottom:1px solid #000;
}

It works and its elegant: http://jsfiddle.net/3Znbu/

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
6

Use the :last-child selector.

.list-item {
  border-bottom:1px solid #000;
}

.list-item:last-child {
  border-bottom: 0;
}

As Mooseman pointed out, it won't work on IE8 or less, so if you need IE8 support, I would suggest either a jQuery solution, or using the :first-child selector, which has IE7+ support.

jQuery

$('.list-item:not(:last-child)').css('border-bottom','1px solid #000');

:first-child

.list-item {
  border-top:1px solid #000;
}

.list-item:first-child {
  border-top: 0;
}
Norbert
  • 2,741
  • 8
  • 56
  • 111
1
#results .list-item:last-child {
   border-bottom: none;
}
Nadh
  • 6,987
  • 2
  • 21
  • 21