开发者

CSS style specific div without id

开发者 https://www.devze.com 2023-02-15 06:48 出处:网络
I am making an ebay storefront and need to style an element on the page which I have no ability to edit the html of.

I am making an ebay storefront and need to style an element on the page which I have no ability to edit the html of.

The html is something like this

<td id="blah">
  <div>
    <div>....

    </div>
  </div>
  <div>....

  </div开发者_高级运维>
</td>

I need to add a margin to both the div's

..but not the children divs of those divs

the css I tried was #blah div {margin:5px;} but of course that effected the child divs.


There is the > "immediate child" selector:

#blah > div {margin:5px;}

but it's not supported by IE6, which sadly still is a consideration for many audiences.

It's preferable to add a class to the box you want to style:

<td id="blah">
  <div class="box">
    <div>....

and specify that:

#blah div.box {margin:5px;}


You can use what you have, just add another selector to override the children:

#blah div 
{
    margin:5px;
}

#blah div div
{
    margin: 0;
}

Give that a go :)


You need the child, not descendant selector:

#blah > div
0

精彩评论

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