CSS Nesting, specificity, and you

Here’s Kilian Valkhof on CSS nesting which isn’t available in browsers yet, but will be soon. There are a few differences he notes between CSS nesting and nesting in Sass or Less though. Take, for example, the following code:

div 


The post CSS Nesting, specificity, and you appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Robin Rendle

Here’s Kilian Valkhof on CSS nesting which isn’t available in browsers yet, but will be soon. There are a few differences he notes between CSS nesting and nesting in Sass or Less though. Take, for example, the following code:

div {
  background: #fff;
  & p {
    color: red;
  }
  border: 1px solid;
}

When CSS nesting lands, that last line border: 1px solid; won’t be applied to the div like it would be in, say, Sass. That’s because with CSS nesting, any styles you want applied to that div have to be written before any nesting styles are written. I think this makes a ton of sense because I tend to enforce that style in any Sass codebases I work on (it’s just much easier to read), but I can imagine people getting confused about this the first time around.

One of the smaller and, yet for some reason, super exciting things about CSS nesting is how we’ll be able to nest media queries, as Kilian notes, just like this:

body {
  background: red;
  
  @media (min-width: 40rem) {
    & {
      background: blue;
    }
  }
}

This is very exciting!

Direct Link to ArticlePermalink


The post CSS Nesting, specificity, and you appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Robin Rendle


Print Share Comment Cite Upload Translate Updates
APA

Robin Rendle | Sciencx (2021-08-10T14:51:39+00:00) CSS Nesting, specificity, and you. Retrieved from https://www.scien.cx/2021/08/10/css-nesting-specificity-and-you-2/

MLA
" » CSS Nesting, specificity, and you." Robin Rendle | Sciencx - Tuesday August 10, 2021, https://www.scien.cx/2021/08/10/css-nesting-specificity-and-you-2/
HARVARD
Robin Rendle | Sciencx Tuesday August 10, 2021 » CSS Nesting, specificity, and you., viewed ,<https://www.scien.cx/2021/08/10/css-nesting-specificity-and-you-2/>
VANCOUVER
Robin Rendle | Sciencx - » CSS Nesting, specificity, and you. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/10/css-nesting-specificity-and-you-2/
CHICAGO
" » CSS Nesting, specificity, and you." Robin Rendle | Sciencx - Accessed . https://www.scien.cx/2021/08/10/css-nesting-specificity-and-you-2/
IEEE
" » CSS Nesting, specificity, and you." Robin Rendle | Sciencx [Online]. Available: https://www.scien.cx/2021/08/10/css-nesting-specificity-and-you-2/. [Accessed: ]
rf:citation
» CSS Nesting, specificity, and you | Robin Rendle | Sciencx | https://www.scien.cx/2021/08/10/css-nesting-specificity-and-you-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.