Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

purpose


example: without nesting

let’s consider this very small code

Loading...

same with nesting

with nesting, we can simply keep the rules in the same logical structure as the HTML tree
that is to say, we could rewrite the above like so

Loading...

how does it work ?

the 2 examples are written so as to be exactly equivalent

as you can see:

so for example on line 9 we have & p.one {
since this is nested inside a rule (line 6) with selector div.first
the corresponding properties (color: black) will apply to selector
div.first p.one

and as you can see on line 12, this can be nested as deeply as you need..


what with the & ?

the & character is a placeholder for the full selector of the parent rule
so it’s helpful if you need to distinguish between ambiguous cases

consider for example the following snippet:

.card {
  &.active { ... } /* means .card.active */
  & .highlight { ... } /* means .card .highlight */
  .title { ... }  /* means .card .title */
}

so as you can see here, the & is sometimes optional, however it’s often clearer to use it explicitly so as to avoid confusion


why is it cool ?

as mentioned in the intro, this feature is very useful as it helps keep some sense in the flow of css, which otherwise quickly becomes a very challenging task, as the css code grows in size and complexity (that is to say, very quickly..)