everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

2024 Guide to CSS Nesting: No More Preprocessors & Modern CSS Only!

CSS Nesting - Readability & Maintainability (No More CSS Preprocessors Needed!)  - CSS Native only! Creating a website has usually involved a lot of nesting within the CSS code. For example, when we want to target a specific container within a specific parent container, and we have been writing the following for decades - uipencil

Creating a website has usually involved a lot of nesting within the CSS code. For example, when we want to target a specific container within a specific parent container, and we have been writing the following for decades:

.myitem{
   font-color: black;
}

/*To overwrite the body color from black to red in the specific .myitem container*/
.parent_container .child_container .grandchild_container .myitem{
    font-color: red;
}

For decades, developers could only rely on CSS preprocessors like SASS, SCSS, or LESS for more structured and understandable code. However, since December 2023, most of the modern browsers are now support built-in CSS nesting capabilities.

Syntax

1. Nesting

HTML:

<body>
    <div class="myitem">Original</div>
    <div class="parent_container">
        <div class="child_container">
            <div class="grandchild_container">
                <div class="myitem">I want to override this text color</div>
            </div>
        </div>
    </div>
</body>

Example 1A: CSS (Old):

.myitem{
   font-color: black;
}
.parent_container .child_container .grandchild_container .myitem{
    font-color: red;
}
.parent_container .child_container .grandchild_container .myitem:hover{
    background: yellow;
}

Example 1B: New CSS Nesting:

.myitem{
   font-color: black;
}

.parent_container{
    .child_container{
     .grandchild_container{

       .myitem{
         font-color: red;

         &:hover{
             background: yellow;
         }

      }
    }
   }
}

Result (During Hover State):

CSS Nesting - Readability & Maintainability (No More CSS Preprocessors Needed!)  - CSS Native only! Creating a website has usually involved a lot of nesting within the CSS code. For example, when we want to target a specific container within a specific parent container, and we have been writing the following for decades - uipencil

Both will achieve the same results as shown above, but the code structure in 1B is more readable and structured compared to the one in 1A.

2. Reverse Nesting

In addition to nesting, another feature is for reverse nesting. For instance, The “&” nesting selector can also be appended to reverse the context of the rules.

HTML:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="myitem">Original</div>
        <div class="color1">
            <div class="myitem">
                I want to override this text color to red with yellow hover bg
            </div>
        </div>
        <div class="color2">
            <div class="myitem">
                I want to override this text color to green
            </div>
        </div>
    </body>
</html>

Example 2A: CSS (Old):

.myitem{
    color: black;
}
.color1 .myitem{
    color: red;
}
.color1 .myitem:hover{
    background: yellow;
}
.color1 .myitem{
    color: red;
}

Example 2B: New CSS Nesting:

.myitem{
    color: black;

    .color1 &{
        color: red;
        &:hover{
            background: yellow;
        }
    }
    .color2 &{
        color: green;
    }
} 

Result (During Hover State):

CSS Nesting - Readability & Maintainability (No More CSS Preprocessors Needed!)  - CSS Native only! Creating a website has usually involved a lot of nesting within the CSS code. For example, when we want to target a specific container within a specific parent container, and we have been writing the following for decades - uipencil

This key difference between native and CSS preprocessors is that:

  1. the native nesting CSS is directly interpreted and understood by browsers, without any additional process.
  2. the CSS generated by a preprocessor needs to be processed and compiled into standard CSS before browsers can interpret it.

Browser compatibility

Important Note: As of today, this feature is almost fully supported on all modern and latest browsers. However, it may not work on older devices or browsers, and if your end users have not updated their browsers to the following versions, they may encounter issues with the CSS display.

ChromeEdge FirefoxSafari Opera Chrome Android WebView AndroidiOS Safari Firefox AndroidOpera Android Samsung Internet
12012011717.210612012017.21178023 (Partial Support)

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/Nesting_selector#browser_compatibility

Happy Coding!


Leave a comment

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

S$0.99
S$2.00
S$5.00

Or enter a custom amount

S$

Thank you for your contribution!

Donate

Topics:

Clean CSS (5) CSS (35) css pseudo (4) debug (2) design (7) fonts (2) html (8) image (9) JavaScript (1) layout (5) MiniGame (3) optimization (15) performance (10) responsive (9)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements