1

I have a parent div, that holds three div's. They are basically columns. I need to remove the margin on the last one but can't get the right selector HTML:

<div class="productContainer">
    <div class="productBox"></div>
    <div class="productBox"></div>
    <div class="productBox"></div>
 <!--/ productContainer --></div>

Here's the CSS:

.productContainer {
    width: 980px;
    height: 400px;
    display: block;
    float: left;
}

How do you target the third child div of a parent? this should work no?
.productContainer > .productBox {
    width: 320px;
    height: 400px;
    display: block;
    float: left;
    margin: 0 10px 0 0;
}

.produtContainer > .productBox nth:child(3) {
    margin-right: 0;
}
Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
  • I've posted a similar question here: http://stackoverflow.com/questions/3786434/last-child-psuedo-class-selector-in-css-and-internet-explorer – Ant Ali Sep 24 '10 at 16:39

5 Answers5

1
.productContainter div:last-child
idrumgood
  • 4,904
  • 19
  • 29
1

While you can use the :last-child selector, it's not going to work in any version of IE before 8. Generally what I do in this situation is add a last class to the last element in the list:

<div class="productContainer">
<div class="productBox"></div>
<div class="productBox"></div>
<div class="productBox last"></div>

And then add this rule below the .productContainer .productBox rule in the stylesheet:

.produtContainer .last {
margin-right: 0;
}
mikeocool
  • 133
  • 3
0

You can do :first-child or :last child to target the first and last element.

compatibility: http://www.quirksmode.org/css/contents.html

Brad Herman
  • 9,665
  • 7
  • 28
  • 30
0

You can use :last-child selector for the rule

.productContainer div:last-child
{
    // rule
}

http://www.quirksmode.org/css/firstchild.html

Robert
  • 21,110
  • 9
  • 55
  • 65
0

You can use the last-child pseudo-selector in this case...

.productContainer > .productBox:last-child {
  margin-right: 0;
}

Note: This will not work in IE8 and older, as this is a part of CSS3. For something more portable, you might want to try this...

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

.productContainer > .productBox.last {
  margin-right: 0;
}
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227