CSS 选择器是做什么的?

最近我在 CSS中遇到了 * *

网站参考资料 -网站连结

对于 CSS 样式表中的单个 *使用,Internet 和 Stack Overflow 充满了示例,但是我不确定在 CSS 中使用两个 * *符号。

我谷歌了它,但无法找到任何有关这方面的信息,作为一个单一的 *选择所有元素,但我不知道为什么该网站使用了两次。缺少的部分是什么,为什么使用这个黑客(如果它是一个黑客) ?

3278 次浏览

* * Matches everything except the top-level element, e.g., html.

Just like any other time you put two selectors one after another (for example li a), you get the descendant combinator. So * * is any element that is a descendant of any other element — in other words, any element that isn't the root element of the whole document.

That selects all elements nested inside another element in much the same way div a would select all <a> elements nested somewhere inside a <div> element.

* means apply given styles to all the elements.

* * means apply given styles to all the element's child elements. Example:

body > * {
margin: 0;
}

This applies margin styles to all the child elements of body. Same way,

* * {
margin: 0;
}

applies margin: 0 to *'s child elements. In short, it applies margin: 0 to almost every element.

Generally, one * is enough. There's no need for two * *.

Just a little big example:

Try to add this on your site:

* { outline: 2px dotted red; }
* * { outline: 2px dotted green; }
* * * { outline: 2px dotted orange; }
* * * * { outline: 2px dotted blue; }
* * * * * { outline: 1px solid red; }
* * * * * * { outline: 1px solid green; }
* * * * * * * { outline: 1px solid orange; }
* * * * * * * * { outline: 1px solid blue; }

Demo: http://jsfiddle.net/l2aelba/sFSad/


Example 2:

What does the * * CSS selector do?

Demo: http://jsfiddle.net/l2aelba/sFSad/34/