85

Can I do something like the following?

.class1{some stuff}

.class2{class1;some more stuff}
TylerH
  • 20,799
  • 66
  • 75
  • 101
kralco626
  • 8,456
  • 38
  • 112
  • 169

9 Answers9

86

Update 1: There is a CSS3 spec for CSS level 3 nesting. It's currently a draft. https://tabatkins.github.io/specs/css-nesting/

Update 2 (2019): We now have a CSSWG editors draft https://drafts.csswg.org/css-nesting-1/

Update 3 (2022): We now have a W3C First Public Working Draft https://www.w3.org/TR/css-nesting-1/

Update 4 (2023): This is now available in Chrome caniuse.com/css-nesting

If approved, the syntax would look 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;
  }
}
etoxin
  • 4,908
  • 3
  • 38
  • 50
  • it was so helpful. Thanks @import '../../styles/variables.css'; .download-page { & .download-page-hero { background-color: #fff; background-image: url('../../img/map-background.jpg'); background-position: center center; background-size: cover; padding: 7.5rem 0; } } These code works on my end now. – Kenyi Despean Apr 30 '18 at 16:19
  • "The spec proposal was not approved by the Working Group." How to determine if it is approved or not? – Weihang Jian Feb 18 '19 at 03:03
  • 1
    You can see here in this issue (https://github.com/w3c/csswg-drafts/issues/2701) it gets rejected near the end of the ticket. – etoxin Feb 18 '19 at 23:03
  • 5
    It didn't get rejected; rather, it was moved to a formal pull request here: https://github.com/w3c/csswg-drafts/pull/2878 – Douglas Manley Mar 01 '19 at 15:08
  • 1
    And has been sitting in "explored" status ever since. SASS and LESS exist exactly because the working group moves so slowly. At their rate of speed, this functionality shouldn't be expected in native CSS until at least 2040. – Ian Kemp Mar 04 '21 at 15:27
  • The proposed syntax above looks overcomplicated and awful to me. – Mike Jul 18 '21 at 12:16
  • For anyone interested in browser implementation of the draft, the site [Can I use](https://caniuse.com/) states [which browsers have implemented nested styles](https://caniuse.com/css-nesting). – Dave F May 01 '22 at 03:15
79

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;
  }
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • alright. Thanks. I'm using JQuery anyways, so i'll use that. I was just hoping to use css because then if I dynamically add another pice of html with class2, for example, that class would be attached to the new html, where as with Jquery I have to add it manually after adding the html – kralco626 Dec 30 '10 at 17:52
  • 28
    What does jQuery have to do with it? Are you suggesting you roll your own css pre-processor in jQuery? – moopet Aug 23 '16 at 08:17
62

Not with pure CSS. The closest equivalent is this:

.class1, .class2 {
    some stuff
}

.class2 {
    some more stuff
}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 3
    Thanks for this. It’s bizarre how so many love taking a simple solution like nested CSS and turning it into a nightmare with preprocessing. If we had this simple feature in plain CSS it would be another solid reason to stop all this extra preprocessing kids love today. – Stokely Mar 10 '21 at 09:33
4

Not directly. But you can use extensions such as LESS to help you achieve the same.

RabidFire
  • 6,280
  • 1
  • 28
  • 24
  • alright. Thanks. I'm using JQuery anyways, so i'll use that. I was just hoping to use css because then if I dynamically add another pice of html with class2, for example, that class would be attached to the new html, where as with Jquery I have to add it manually after adding the html. – kralco626 Dec 30 '10 at 17:52
3

No.

You can use grouping selectors and/or multiple classes on a single element, or you can use a template language and process it with software to write your CSS.

See also my article on CSS inheritance.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

If you cannot wait until native CSS nesting goes official, you can use Container Queries to do it. As of now, it is supported (partially) by Chrome & Edge 105+, as well as Safari 16+.

It will looks like this:

.class1 {
   container-type: inline-size;
   container-name: my-container;
   // other style rules
}

@container my-container (min-width: 0px) {
  .class2 {
    // some style rules
  }
}

More details can be found at here.

toyssamurai
  • 561
  • 4
  • 13
0

I do not believe this is possible. You could add class1 to all elements which also have class2. If this is not practical to do manually, you could do it automatically with JavaScript (fairly easy to do with jQuery).

Kyle
  • 2,822
  • 2
  • 19
  • 24
0

Now you can, it's called nesting: https://developer.chrome.com/articles/css-nesting/

  • top answer is better – starball Jul 21 '23 at 08:43
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '23 at 13:36
-2

Try this...

Give the element an ID, and also a class Name. Then you can nest the #IDName.className in your CSS.

Here's a better explanation https://css-tricks.com/multiple-class-id-selectors/

  • 2
    How is that helpful? It doesn't allow you to extend a CSS rule, which is what the question is asking, and there's no benefit to using an ID with a class as opposed to just using multiple classes anyway. – moopet Aug 23 '16 at 08:20