March 7, 2012

CSS – context used styling?

Question by Jurik

I thought that it was possible, but everyone tells me it’s not.

I want context styling in my css file like:

div#foo {
 h2 {
   color: #F42
 }
 p.bar {
   font-size: 12px
 }
}

So that only h2 and p.bar in the div with id foo will be styled. Or is this only possible with LESS and other similar libs?

Thanks & kind regards,
Jurik

Answer by VAShhh

This is not possible with pure CSS, that’s why you should use SCSS or LESS (i suggest to use SASS/SCSS), which are CSS supersets

LESS/SASS-SCSS allows you to write dynamic CSS with ease, take a look at this comparision

check out COMPASS which is the main reason why I suggest you SASS/SCSS

Answer by Starx

Its not possible using default CSS techniques.

But, by using sass and less however, it is possible.

The code in your question, works in both of the libraries above.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!