0

I have multiply CSS div's within a parent like this:

<div id="homepage">
     <div class="a"></div>
     <div class="b"></div>
     <div class="c"></div>
</div>

I am currently putting this in my CSS like this:

#homepage .a {
     background-color: #ff0000;
}

#homepage .b {
     background-color: #0000ff;
}

#homepage .c {
     background-color: #00ff00;
}

My question is there a better way to reference this in my CSS than what I am currently doing. I have tried this but it fails:

#homepage {
     .a {
          background-color: #ff0000;
     }
     .b {
          background-color: #0000ff;
     }
     .c {
          background-color: #00ff00;
     }
}
AMACB
  • 1,290
  • 2
  • 18
  • 26
Cameron
  • 572
  • 1
  • 3
  • 12

1 Answers1

3

There is no way to do such things in plain CSS. You have to use a CSS preprocessor, for example Sass or Less.

Example of nesting selectors in Sass:

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  li { display: inline-block; }

  a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
  }
}

It generates following CSS:

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  display: inline-block;
}

nav a {
  display: block;
  padding: 6px 12px;
  text-decoration: none;
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177