开发者

CSS parent div with children

开发者 https://www.devze.com 2023-01-18 00:03 出处:网络
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

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: 40开发者_运维问答0px;
    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;
}


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;
}


.productContainter div:last-child


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

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


You can use :last-child selector for the rule

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

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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号