Hacker News new | ask | show | jobs
by lmm 4965 days ago
>Unless you are advocating using some kind of class system for styling within javascript, which is odd since CSS already offers that. If you are speaking of applying CSS classes through javascript, which sounds like what you are describing, then I'm confused over what the point is. You can create CSS classes and apply them to elements on the fly right now, you just seem to saying you want to create the CSS classes in javascript. Which makes no sense when you can do the exact same thing more efficiently in an external style sheet. The reason it is more efficient is because those classes will not need to be created on every single page load.

I'm advocating using whatever programming language you use to apply behaviour to your site, whether that be javascript or something else, to apply a style where it's needed, rather than via CSS selectors. Any language you're using to generate web pages will already have this functionality, because it's needed to e.g. attach event handlers (or equivalent) to text boxes. If your application uses jquery, use that (but it's probably not the best example, because jquery uses pretty much the same selector syntax). If your application uses some awful adobe templating engine and does everything via XPath, use that. If your application uses a java class hierarchy, use that. Etc.

>I would say your solution has more of a learning curve involved because you are adding completely new syntax and logic to javascript when it wasn't designed to do these things.

I think javascript is already very much optimized for functions of the form: find some subset of the elements on the page, and then do something to them.

>The solution is to update what the browsers expect from CSS. There is no reason why CSS itself cannot be extended to do what you are speaking of, probably most people (including me) would want that functionality right now.

Unfortunately the W3C is unwilling to even add variables to CSS. Even if they did, it would be years before we could rely on new functionality being present in end-user browers - and then what if we realised we wanted some additional functionality? Better for the logic to be written in server-side code, and we can use new languages as they're invented. (With javascript we don't get to use new language features but we can at least use new libraries, which is more than can be said for CSS)

>Much like my three examples you pretty much ignored, you still have to learn the CSS syntax and capabilities regardless of how you feed it to browser. Unless you advocate coming up with a whole new styling language that eventually outputs to CSS

Up to a point. Things like the box model there's no getting away from, but I see no need to use the CSS syntax or CSS selectors. Just like the way we currently handle the DOM, there can be a single styling API but you can use it in javascript with javascript syntax, or in python with python syntax, or in whichever language you like.

>Styles are not part of your markup, period. Well, maybe inline styles but, again, that's bad.

Without/before CSS, all styling was inline, i.e. part of your markup. The whole reason CSS was invented was, as I originally said, to provide a way to reuse (some small specific parts of) your markup in several places.

1 comments

I apologize, I fail to see how what you want to do is any different than what you can do today. Almost everything you just said is what I've been saying the whole time. You can create a CSS class in either a external style sheet or in a style section in the head of the document and use javascript to apply that class to any element on the page. Since you are already using CSS selector syntax in javascript to target these elements I really don't see how having something in between that makes it any easier. If I understand what you are saying, you wish to remove the selector part out of CSS and apply it directly to elements as they are used. But, again, that's already there, right now, today, for you to use with no third-party tool with its own syntax/language to get in the way.

All I can see at this point is that you wish to stop using CSS selectors but still use CSS syntax in describing properties of the elements to be styled. As stated in the beginning, you wish to remove CSS for styling and replace it with something else for, styling. That makes no sense. You are already using these very same selectors to target these elements with javascript. Plus the same syntax can be used in any programming language that outputs HTML, just assign class names to the element as they are created.

Since you propose to no longer use CSS syntax nor selectors in styling elements then I require an example of how you propose to handle this with current browsers. I really just don't understand the concept of creating CSS to style elements without using CSS, that's the circular argument you are using.

You say you want a single styling API, that already exists and it's named CSS.

>Since you are already using CSS selector syntax in javascript to target these elements I really don't see how having something in between that makes it any easier.

Stop fixating on javascript. Sure, javascript uses the CSS selector syntax. But other languages use other ways of addressing particular page elements, and I don't think the CSS selector syntax is a good way in general.

>Plus the same syntax can be used in any programming language that outputs HTML, just assign class names to the element as they are created.

The same syntax as CSS, which is different from that programming language's natural syntax. Outputting class names adds an extra level of indirection that's just unnecessary. Why not just add the relevant style inline?

>You say you want a single styling API, that already exists and it's named CSS.

A single API, not a single syntax. What I want is two things: 1. A natural way to build up a style declaration programatically in python or java (javascript already has this to a certain extent), not a string in CSS property syntax. 2. A natural way to apply this to some subset of the elements on my page, which I identify using whatever tools my programming language has (which in javascript might be CSS selectors, but in general won't be).

Now you're right that the second half is almost-possible. I can use my language to put class attributes on those elements I want to style, and then have a CSS file (or rather, a CSS-preprocessor file) that only ever uses these classes as selectors. As long as I keep the class names unique that works and it keeps the CSS comprehensible.

But I've still got two separate programs in different languages that don't need to be separate - my main program generates a mapping from page element to classname, and my CSS-preprocessor program generates a mapping from classname to set of properties[1]. The latter is a task that my primary programming language is perfectly adequate for (and vanilla CSS is not, because it's not powerful enough, hence the need for a preprocessor). So what I want is to have a bit of code that generates an object representing a set of properties (which would "really" be a string in CSS property syntax, sure, I don't care as long as it has a reasonable API for manipulating it in my programming language), and then rather than attaching a classname to the elements I want to style, I attach this style object, which applies it directly inline.

At that point the inline style would still be sent to the browser with CSS property syntax, but there'd be no separate CSS file and no cascading, so I think it's fair to say this would not be using CSS.

[1] A program which happens to be interpreted by the browser (after preprocessing), but conceptually a program nonetheless.