How reliable is “>”?

2019-08-06 01:39发布

The > character can be used with CSS to select an element that has a certain parent. The benefit I see here is that I can apply styles only to a certain level of a list for example. Like menus - first level is orizontal and has different rules than 2nd level+. so i don't need to worry about resetting properties for lvl 2+

Anyway, can I depend on >? Is it supported by all browsers and without buggy behaviors?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-06 02:04

It's part of the CSS2 standard: http://www.w3.org/TR/CSS2/selector.html#child-selectors so modern browsers should support it.

According to this quirksmode.org, only IE6 and earlier do not amongst major browsers. I only see IE6 used in very situational cases (like dedicated machines that don't receive software patches).

查看更多
3楼-- · 2019-08-06 02:16

The child selector > is fully supported by IE7 and later, and not at all in IE6 and earlier. Of course, all versions of all other major browsers in use today support it fully as well.

All CSS2.1 selectors are well-supported by IE8 and later so you can use them today, unless you're writing legacy code that needs to cater to IE6, in which case avoid them where possible.

The SitePoint Reference does mention an obscure parsing bug related to comments that affects IE7, but it only breaks the selector if a comment is there. You don't usually put comments in the middle of selectors unless you're doing so as a hack, so you don't need to worry about this bug.

Related: Are CSS child selectors a W3C standard? (Of course they are!)

查看更多
登录 后发表回答