What I want from CSS3 – nested declaration blocks

Back in October, “Andy Budd wanted to have min- and max-padding in CSS3“:http://www.andybudd.com/archives/2005/10/what_i_want_from_css3_part_1/index.php, so it seems only fair that for my 200th post here I can also give my $.02 and ask (and maybe stomp my little feet) for something in CSS3 (after all we can celebrate together in well, roughly 2010 when all browsers support it – that is if we still use browsers then):

I want nested declaration blocks!

I basically want them as I hate repeating myself, and I hate repeating code when it is not needed.

Currently there is a lot of repetition going on, especially when you use links:


  #content ul{
    list-style:square;
    margin:0 2em;
  }
  #content li{
    list-style:square;
    padding:2px 5px;
    line-height:1.3em;
  } 
  #content a:link{
    [...]
  }
  #content a:visited{
    [...]
  }
  #content a:active{
    [...]
  }
  #content a:hover{
    [...]
  }
  #nav ul{
    list-style:none;
    margin:0;
  }
  #nav li{
    list-style:none;
    padding:2px 5px;
    line-height:1.3em;
  }
  #nav a:link{
    [...]
  }
  #nav a:visited{
    [...]
  }
  #nav a:active{
    [...]
  }
  #content a:hover{
    [...]
  }

Wouldn’t it be the cat’s pijamas to be able to do something like this?


  #content {
   ul{
     list-style:square;
     margin:0 2em;
   }
    li{
      list-style:square;
      padding:2px 5px;
      line-height:1.3em;
    } 
    a{
     :link{[...]}
     :visited{[...]}
     :active{[...]}
     :hover{[...]}
    }
  }
  #nav {
   ul{
     list-style:none;
     margin:0;
   }
    li{
      list-style:none;
      padding:2px 5px;
      line-height:1.3em;
    } 
    a{
     :link{[...]}
     :visited{[...]}
     :active{[...]}
     :hover{[...]}
    }
  }

Ok, I don’t necessarily need the pseudo selector nesting bit, but the main idea would be such a great shortcut.

Imagine how easy it would be to give instructions to CSS designers how to differentiate between JavaScript enhanced and normal styles. Imagine a script that adds a class called ‘dyn’ to a UL with the id ‘nav’ when JS and DOM is available. The CSS file could have the following instructions.


  /* Styles for the static version */
  ul#nav{
       /* go nuts! */
  }
  /* Styles for the dynamic version */
  ul#nav.dyn{
       /* go even nuttier! */
  }

Progressive enhancement would also be a snap:


  /* For CSS3 browsers, like Opera 16 
    and Firefox 1.831425243 release candidate 2 */
  html {
     body{
       [...]
    }
  }

I checked the “W3c CSS3 syntax reference”:http://www.w3.org/TR/2003/WD-css3-syntax-20030813/ but so far there is nothing of the sort in there. There is a rather cryptic example, which basically shows that you can use curly braces in a selector when you embed them in quotationmarks:


  p[example="public class foo
  {
      private int x;
  
      foo(int x) {
          this.x = x;
      }
  
  }"] { color: red }

Which somehow confuses the heck out of me. What is this selector supposed to be?

Ok enough dreaming. Let’s see that we can get more people out there to upgrade first…

13 Responses to “What I want from CSS3 – nested declaration blocks”

  1. Shannon Says:

    … and the bee’s knees — all rolled into one! Great idea.

  2. Dan Kubb Says:

    What about writing something that pre-processes a file with the nested blocks like you have and outputs a normal CSS file?

    Sure, its an extra step but at least that way you could write it your way now, and in 5 years you can drop the pre-processing phase.

    Plus if you didn’t want to bother with the extra step it could even be done on-the-fly for each request — it should be relatively fast in most programming languages.

    If performance ever was an issue you could set up something so that the translation is done for the first request, the CSS file written out, and all future requests would use the static file. There’d still be a small amount of overhead to check and see if the master is newer than the CSS file — regenerating the CSS if that’s the case — but it still should be lightening quick.

  3. Chris Heilmann Says:

    Dan,

    i’ve “done something similar to simulate CSS constants”:index.php?p=24 and sure thought about writing this one, too. Deep nesting might be tricky to do though.

    Furthermore, right now I got a book to finish and haven’t started writing all of the TOC yet. :-)

  4. Robin Says:

    The last example I think would select:

    <p example="public class foo
    {
    private int x;

    foo(int x) {
    this.x = x;
    }

    }">Example para</p>

    It’s a bit weird to embed code into an attribute but valid as far as I can see.

  5. Henrique Costa Pereira Says:

    Very, very funny! But I more optimist and I think that Mozilla in 2010 will be in 1.9 version!!!!!!!!!!! and no, Internet Explorer will not exist anymore. I hope…

  6. Matt Says:

    I’ve written a PHP pre-processor to do exacly this before. Let me know if you want more info.

  7. Wait till I come! » Blog Archive » CSS preprocessor for variables, nested selectors and other goodies Says:

    [...] Skulic just emailed me about CSSPP, a CSS preprocessor inspired by my CSS wishlist and CSS constants [...]

  8. CSS Wishlist: New Ideas, Debates and Solutions | CSS | Smashing Magazine Says:

    [...] issue is our inability to nest entire declaration blocks. The article "What I want from CSS3: nested declaration blocks" offers a smart outlook on the benefit of nested declaration [...]

  9. CSS Wishlist: New Ideas, Debates and Solutions « Tech7.Net Says:

    [...] issue is our inability to nest entire declaration blocks. The article “What I want from CSS3: nested declaration blocks” offers a smart outlook on the benefit of nested declaration [...]

  10. AMB Album » CSS Wishlist: New Ideas, Debates and Solutions Says:

    [...] issue is our inability to nest entire declaration blocks. The article "What I want from CSS3: nested declaration blocks" offers a smart outlook on the benefit of nested declaration [...]

  11. CSS Wishlist: New Ideas, Debates and Solutions · Variedades e Bookmarks Says:

    [...] issue is our inability to nest entire declaration blocks. The article "What I want from CSS3: nested declaration blocks" offers a smart outlook on the benefit of nested declaration [...]

  12. CSS Wishlist: New Ideas, Debates and Solutions | WEBDESIGN FAN Says:

    [...] issue is our inability to nest entire declaration blocks. The article "What I want from CSS3: nested declaration blocks" offers a smart outlook on the benefit of nested declaration [...]

  13. macca87 Says:

    Check out .LESS for CSS

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).