CSS nesting: use with caution
html - Is nesting possible with CSS (like it is with SASS)? - Stack Overflow
css selectors - Nesting CSS classes - Stack Overflow
CSS nesting improves with CSSNestedDeclarations
Videos
I’ve been keeping an eye on native css nesting for a while now and I was wondering if you would consider it a safe feature to use yet.
Here is the support on can I use
https://caniuse.com/css-nesting
I’m talking about the clean version that doesn’t use the & prefix.
Clean css nesting without sass please tell me the future is here.
No, that's not possible now, that's why SASS lists nesting as a feature.
Well, as @destroy already answered, you cannot have nested selectors using CSS, that's why developers choose LESS and SASS preprocessors. The best thing you can do to minimize the CSS is my grouping common properties like
div, p, i {
color: #f00;
/* All the three elements will have the same color */
}
You can also declare the base properties right in the parent selector so that they can be easily inherited and you don't have to call them again and again on each..
body {
font-size: 14px;
font-family: Arial;
color: #515151;
}
The above properties will be easily inherited by elements such as p, so you won't have to declare the font-family or font-size each time unless and until you want to have a different font-family for a particular element which can be over ridden by using a more specific selector like
.class_name p {
font-family: Open Sans, Arial;
}
You do have universal selectors which will also ease up over lengthy selectors, like say you want to color red all the elements nested inside a specific element having a class called .class_name so instead of doing
.class_name p, .class_name div, .class_name fieldset {
/* This is really bad */
}
Instead you can write the above as
.class_name * {
/* Much better */
}
Conclusion : Learn CSS Selectors, that's the only way you can figure out and you can optimize your CSS yourself, while selectors totally depend on the DOM, so there are no pre defined techniques but you should keep the selectors simple, not over complicated, else you will end up writing more and more specific rules which will lead to more 100 line of crappy CSS...
Also, here's an handy tool by Google you can always use to optimize the performance.
There is now a CSS Nesting Module in the CSS specification. The module is currently a Working Draft and CSS nesting is supported in all major browsers.
The syntax looks like this:
table.colortable {
& td {
text-align:center;
&.c { text-transform:uppercase }
&:first-child, &:first-child + td { border:1px solid black }
}
& th {
text-align:center;
background:black;
color:white;
}
}
.foo {
color: red;
@nest & > .bar {
color: blue;
}
}
.foo {
color: red;
@nest .parent & {
color: blue;
}
}
Not possible with vanilla CSS. However you can use something like:
- Sass
Sass makes CSS fun again. Sass is an extension of CSS3, adding nested rules, variables, mixins, selector inheritance, and more. It’s translated to well-formatted, standard CSS using the command line tool or a web-framework plugin.
Or
- Less
Rather than constructing long selector names to specify inheritance, in Less you can simply nest selectors inside other selectors. This makes inheritance clear and style sheets shorter.
Example:
#header {
color: red;
a {
font-weight: bold;
text-decoration: none;
}
}