开发者

What's wrong with selecting the first div this way

I'm trying to have some css applied for the first div with id=div1. When I do .alldivs:first, it doesn't work. What am I doing wrong?

<div class="alldi开发者_如何学JAVAvs">
   <div class="onediv" id="div1">
   </div>
   <div class="onediv" id="div2">
   </div>
   <div class="onediv" id="div3">
   </div>
</div>


If you want to select the first child: .alldivs>:first-child should do the trick.

Edit: Su edited my post to say .alldivs:first-child. This actually isn't right and I've restored it to what I originally put. The :first-child syntax selects the first child of its parent that matches the selector immediately previous to the colon. Therefore, p:first-child would match any paragraph that was the first child of its parent. Thus, .alldivs> matches any child of .alldivs and :first-child matches the first one. Please make sure you're correct before editing others posts.


What you want is:

.alldivs div:first-child


If the div has an id already, just select it by id.

#div1 {
   /* yes that's all you need */
}

There's no such thing as two elements with the same id (if you're paying attention to the rules), so it doesn't matter if it's first or thirty-first.

If you're looking for the first div no matter what the id, use .alldivs :first-child

Here's some reference for further understanding:

http://www.w3.org/TR/CSS2/selector.html#first-child


.alldivs is not a selector, if you wanted to select the first div in the dom it would be div:first-child or if you wanted to select the first div with the class onediv it would be .onediv:first-child

I hope this helps you

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜