2

I have the following structure

<div class="mainmenu">
    <div>
      Home
    </div>
    <div>
      About us
    </div>
    <div>
      Lorem
    </div>
</div>

.mainmenu div
{
     border-right:1px #000 solid;
}

With this I can enable the right border for all divs, but I don't want border in the last div. Is it possible to control this through css, without modifying the structure above?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mokus
  • 10,174
  • 18
  • 80
  • 122

2 Answers2

7

Yes (with pseudo selectors):

.mainmenu div:last-child
{
     border-right:none;
}

Note: supported only in browsers that support css3.

easwee
  • 15,757
  • 24
  • 60
  • 83
1

CSS3 now lets us select a certain 'child' and apply styling like so:

.mainmenu div:last-child{
 border: none;   
}

However CSS3 is not a safe path to go down yet for older browsers so another option would be to give your last div the class of 'last' and styling it like so:

<div class="last">
  Lorem
</div>

.mainmenu .last{
 border: none;   
}

You can have multiple classes in a div so you can apply the 'last' option as well as whatever else the div is called e.g.

<div class="apple last">
  Lorem
</div>
DBUK
  • 1,373
  • 1
  • 23
  • 41
  • Ya, it was more of an addition to let him know that it might not work in older browsers, but, yep, I should have specified that. – DBUK Nov 15 '11 at 10:44