tags:

views:

125

answers:

4

How complicated will it be to implement from scratch?

Mainly for layout issues.

The reason I want to make it declarative is that I hope to encapsulate it into a javascript class,so that I don't need to cope with css deficits once and again,instead,once for all.

+4  A: 

It's not really a big deal at all. You just do things like

document.getElementById('whatever').style.width = 100;

or, in jQuery,

$('#whatever').css('width', 100);

Gotta say, though, it sounds like you want to completely replace stylesheets with programmatic style manipulation, and that's probably a terrible idea.

chaos
Yes,what I'm asking about is the terrible version:)
Shore
Well the question was tagged "JavaScript", so I hope he knows the implications of doing such a thing, but I agree, completely replacing CSS with JavaScript is a terrible idea.
Dan Herbert
Yeah, your Web site will look like crap for anyone running noscript.
Chuck
Not to mention the initial render probably being a mass of craziness until the programmatic styles are done being applied.
chaos
Your comments are all reasonable,but layout is my biggest headache,so I want to find a way to fix it once and never need to cope with it again.
Shore
If your site requires Javascript to operate completely, then I don't see any reason to avoid CSSJSON, or some other method of styles-by-script.
Jonathan Sampson
+1  A: 

You should clarify your question. If I understand it is as "Why is it that CSS is a declarative language rather then imperative" then it's because it's conceptually closer to how humans want webpages to be styled (and how browsers feel comfortable reading it).

ilya n.
Or, to make a nice layout, use one of the standard ones around the web.
ilya n.
I want to reuse the layout.
Shore
Well, you can use jQuery as in the example to modify your page to your liking, and encapsulate it into your object... anything else?
ilya n.
+4  A: 

Honestly, the only correct answer here is don't do it - learn CSS properly. You seem to want to get around some deficiencies in CSS, but you don't say what they are, apart from "layout" which is about as vague as you can get. CSS does layout fine (using floats, not positioning - the latter is rarely used for layout).

Setting CSS styles using Javascript isn't really solving any problem as far as I can see. Maybe you need to explain more clearly (with examples) what your problem is.

DisgruntledGoat
Floats certainly can't produce all types of layout that a person might want.
ilya n.
@ilya n - No, but most layouts that people might want can be acheived through floats.
Jonathan Sampson
@ilya: what layouts can't be achieved through CSS alone? (I'm sure you're correct but I can't think of anything.) And how is JS going to solve that problem?
DisgruntledGoat
A: 

Check out JSON CSS. It's a rather interesting method of using the power of Javascript (performing logic, etc) in a nice and orderly fashion to set styles. Arc90.Labs has a nice write-up on the topic with examples.

http://lab.arc90.com/2009/01/json_css.php (also on Google Code)

Demo JSON CSS Stylesheet:

{
    "@variables": {
        "demoBgColor": "#FBFAF4"
    },

    "#demoContainer": {
        "background-color": "@{demoBgColor}",
        "border": "1px dotted #CCC",
        "padding": "0.5em",
        "width": "40em",
        "@browser[msie-6]": {
            "width": "400px"
        },

        "h3.header": {
            "text-decoration": "underline"
        },

        "div:first": {
            "font-weight": "bold"
        }
    }
}
Jonathan Sampson