Universal Selector In CSS

  • The Universal Selector in CSS is represented by the asterisk (*) symbol and is used to select all elements within an HTML document. It matches every element type, including the HTML, body, and all other elements on the page. The Universal Selector has the lowest specificity among all CSS selectors, meaning it has the least priority when applying styles.
  • The Universal Selector is often used in conjunction with other selectors or to apply styles globally to all elements in a document.
Here's an example to demonstrate its usage:


    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
                background-color: aqua;
                color: red;
            }
        </style>
        <title>Document</title>
    </head>

    <body>
        <h1>
            Lorem ipsum dolor sit amet consectetur, adipisicing elit. Saepe, modi?
        </h1>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus vitae vero ducimus debitis quaerat unde
            aut inventore amet possimus eaque, quia laudantium iste, accusantium neque odio? Delectus illum minima cum.
        </p>
    </body>

    </html>

  • In this example, the Universal Selector is used to target all elements in the document. The CSS rules inside the curly braces will be applied to every element, resetting the margin and padding to 0 and setting the box-sizing property to border-box.
  • By applying these styles to all elements, you can establish a consistent starting point for your layout and avoid any unexpected default styles that different elements might have. This technique is commonly referred to as a CSS reset or CSS normalization.
  • CSS reset and CSS normalization are two techniques used to ensure that web pages render consistently across different browsers.
  • CSS reset is a set of rules that removes all default styles from HTML elements. This means that all elements will start out with the same properties, such as font size, margin, and padding. This can be useful if you want to have complete control over the styling of your web pages. However, it can also be time-consuming to re-apply all of the default styles that you removed.
Here is an example of CSS reset in CSS:


    /* Reset all HTML elements */
    * {
        margin: 0;
        padding: 0;
        border: 0;
        font-family: sans-serif;
        font-size: 16px;
    }

  • This code will reset all HTML elements to their default state. This means that they will have no margins, padding, borders, or custom fonts. This can be useful for ensuring that your website looks consistent across different browsers.
  • CSS normalization is a set of rules that corrects inconsistencies in the way that different browsers render HTML elements. This can include things like the width of columns, the spacing between elements, and the alignment of text. Normalization can help to ensure that your web pages look the same in all browsers, without having to manually style each element.
Here is an example of CSS normalization in CSS:


    /* Normalize the `<ul>` and `<ol>` elements */
    ul,
    ol {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }

    ul li,
    ol li {
        margin-left: 1em;
    }

  • This code will normalize the <ul> and <ol> elements. This means that they will have no bullets or numbers, and they will have no margins or padding. The list items will also be indented by 1em. This can be useful for ensuring that your lists look consistent across different browsers.
Which one should you use?
  • The best approach for you will depend on your specific needs. If you want to have complete control over the styling of your web pages, then CSS reset may be the best option. However, if you want to save time and ensure that your web pages look the same in all browsers, then CSS normalization may be a better choice.
  • It's important to note that using the Universal Selector can be quite powerful, but it also has performance implications. Applying styles to every element can result in a larger CSS file and potentially slower rendering. Therefore, it's generally recommended to use the Universal Selector sparingly and only when necessary.
Here's another example to illustrate a specific use case of the Universal Selector:


    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            ul * {
                color: red;
                background-color: aqua;
            }
        </style>
        <title>Document</title>
    </head>

    <body>
        <h1>
            Lorem ipsum dolor sit amet consectetur, adipisicing elit. Saepe, modi?
        </h1>
        <ul>
            <li>apple</li>
            <li>banana</li>
            <li>mango</li>
        </ul>

        <ol>
            <li>yogi</li>
            <li>modi</li>
            <li>rahul</li>
        </ol>

        <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus vitae vero ducimus debitis quaerat unde
            aut inventore amet possimus eaque, quia laudantium iste, accusantium neque odio? Delectus illum minima cum.
        </p>
    </body>

    </html>

  • In this case, the Universal Selector is combined with the descendant selector to target all elements inside an unordered list (ul) and apply a red color to their text. This rule will select all list items, nested elements, and any other descendants of the unordered list and give them a red text color.
  • In summary, the Universal Selector (*) in CSS is a powerful selector that matches all elements in an HTML document. It can be used to apply styles globally or in combination with other selectors to target specific elements or groups of elements. However, it should be used judiciously to avoid unnecessary performance overhead.

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...