tags:

views:

64

answers:

4

Maybe not and I need to do this in HTML but is there a way to write

if($('.Name1 li').length>2) $('.main').css('k', 'v');

I did this in JS and if I am loading the page for the first time I see a noticeable pop happen. Its not 'bad' but I do 'notice' it.

+3  A: 

CSS is not a programming language, it is not designed to interact with HTML in this way.

You
+1  A: 

Something similar is or will soon be possible using JESS, a framework combining CSS with JavaScript for, umm, "dynamic styling." I haven't looked at it in dept myself yet, but it may be worth a look for people interested in this sorta thing.

Carl Smotricz
Isn't that what jQuery does? (among other things)
kingjeffrey
Well, jQuery has a different goal set - it's the kitchen sink of JS, AJAX and who knows what else. JESS is just "layout with calculation". While jQuery can do anything, JESS is specialized, perhaps a little easier to learn, and more lightweight. All this is an educated guess, my knowledge of either product is marginal. But I've often yearned for just such a thing: Say, specifying a measurement of `20% + 50px` for some dimension. Nesting two `DIV`s to achieve that effect in CSS is repugnantly inelegant.
Carl Smotricz
+5  A: 

It would be better if you check this condition on server side (php, ruby, C#, python, whatever) and assign some class then.

klew
+1  A: 

Unfortunately you're always going to get this flash of unstyled content whenever you do this kind of class setting with javascript since the JS is loaded after everything else on the page.

Without knowing what specifically you need to achieve, some suggestions would be to use some kind of placeholder CSS on the offending elements (setting a width / height / colour / whatever it is you need) to try to mitigate the effect. You can also try to mitigate it by making sure your page is optimised as much as you possibly can, so give it a run through with yslow and see if there are any speed improvements you can make.

The only other (and best) way would be to use klew's suggestion and insert the class server-side instead of waiting until the DOM has loaded for JS to do its stuff.

hollsk