Home >Web Front-end >CSS Tutorial >What is the universal selector (*) in CSS?
The universal selector, denoted by the asterisk symbol (*), is a selector in CSS that matches any element within the document. It is a wildcard that applies styles to all elements on a webpage, regardless of their type, class, or ID. When used, it can be combined with other selectors to apply styles globally or to reset certain properties across the entire document.
For example, the following CSS rule will set the text color to blue for all elements on the page:
<code class="css">* { color: blue; }</code>
The universal selector can also be used in more specific selectors. For instance, to target all elements inside a <div> with the class "container", you would write:<pre class="brush:php;toolbar:false"><code class="css">.container * {
margin: 0;
padding: 0;
}</code></pre>
<p>This broad applicability makes the universal selector a powerful tool in CSS, but it should be used judiciously due to its impact on performance and specificity.</p>
<h3>How does the universal selector (*) affect performance in CSS?</h3>
<p>The universal selector (*) can have a significant impact on performance, primarily due to the way browsers process and apply CSS rules. Here are the key points to consider:</p>
<ol>
<li>
<strong>Increased Computational Overhead</strong>: The browser has to check every single element on the page when the universal selector is used. This means the browser must apply the specified styles to potentially thousands of elements, which can lead to slower page rendering times.</li>
<li>
<strong>Selector Matching</strong>: The process of matching the universal selector is inherently less efficient than using more specific selectors. When a browser encounters a universal selector, it has to traverse the entire DOM tree, which increases the time required for rendering.</li>
<li>
<strong>Impact on Specificity and Cascading</strong>: Because the universal selector has very low specificity, it can be overridden by more specific selectors. However, when combined with other selectors, it can still contribute to a complex specificity chain, complicating the cascade and potentially slowing down the rendering engine.</li>
<li>
<strong>Reflow and Repaint</strong>: Applying styles to all elements can trigger multiple reflows and repaints, especially if the styles affect layout properties like margins or padding. This can further degrade performance, particularly on mobile devices or older hardware.</li>
</ol>
<p>In summary, while the universal selector is useful, its broad application can lead to performance issues, making it important to use it sparingly and with careful consideration of its impact.</p>
<h3>Can the universal selector (*) be used to reset styles in CSS, and if so, how?</h3>
<p>Yes, the universal selector (*) can be effectively used to reset styles in CSS. Resetting styles is a common practice to ensure consistent rendering across different browsers, which often have their own default styles for HTML elements. Here's how you can use the universal selector to reset styles:</p>
<ol>
<li>
<p><strong>Global Reset</strong>: You can use the universal selector to reset certain properties for all elements. A common example is resetting margins and padding to zero:</p>
<pre class="brush:php;toolbar:false"><code class="css">* {
margin: 0;
padding: 0;
}</code></pre>
</li>
<li>
<p><strong>Box Model Reset</strong>: You can also use the universal selector to normalize the box model across elements:</p>
<pre class="brush:php;toolbar:false"><code class="css">* {
box-sizing: border-box;
}</code></pre>
</li>
<li>
<p><strong>Combined Reset</strong>: You might combine the universal selector with other selectors to create a comprehensive reset. For instance, the following resets styles for all elements, including pseudo-elements:</p>
<pre class="brush:php;toolbar:false"><code class="css">*, *:before, *:after {
margin: 0;
padding: 0;
box-sizing: border-box;
}</code></pre>
</li>
<li>
<strong>Inheritance and Specificity</strong>: When using the universal selector for reset purposes, be mindful of CSS inheritance and specificity. Some properties (like <code>font-family
or color
) might be better reset on specific elements or through other means to avoid unnecessary overrides.
By using the universal selector in this manner, you can create a clean slate for your styles, making it easier to implement your design consistently across various browsers and devices.
While the universal selector (*) has its uses, there are certain scenarios where it should be avoided due to potential drawbacks:
.container * .item
is less clear and more computationally expensive than .container .item
.In summary, while the universal selector is a powerful tool in CSS, its use should be carefully considered, especially in performance-sensitive or complex environments. More specific selectors are often a better choice for targeted styling and better performance.
The above is the detailed content of What is the universal selector (*) in CSS?. For more information, please follow other related articles on the PHP Chinese website!