Nesting CSS classes

kralco626 picture kralco626 · Dec 30, 2010 · Viewed 202.2k times · Source

Can I do something like the following?

.class1{some stuff}

.class2{class1;some more stuff}

Answer

Sarfraz picture Sarfraz · Dec 30, 2010

Not possible with vanilla CSS. However you can use something like:

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

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;
  }
}