<p>Many frameworks seek to abstract away from HTML (custom tags, JSFs component system) in an effort to make dealing with that particular kettle of fish easier.</p>

<p>Is there anything you folks have used that has a similar concept applied to CSS? Something that does a bunch of cross-browser magic for you, supports like variables (why do I have to type #3c5c8d every time I want that colour), supports calculated fields (which are 'compiled' into CSS and JS), etc.</p>

<p>Alternatively, am I even thinking about this correctly? Am I trying to push a very square block through a very round hole? </p>

[{'answer_id': 25462, 'author': 'Dan Herbert', 'author_id': 392, 'author_profile': 'https://Stackoverflow.com/users/392', 'pm_score': 5, 'selected': False, 'text': "<p>What I found works best is to really learn CSS. I mean <strong>really</strong> learn CSS.</p>\n\n<p>It can be a confusing language to learn, but if you read enough about it and practice, eventually you'll learn the best way to do things.</p>\n\n<p>The key is to do it enough that it comes natural. CSS can be very elegant if you know what you want to do before you start and you have enough experience to do it.</p>\n\n<p>Granted, it is also a major PITA to do sometimes, but even cross-browser issues aren't so bad if you <strong>really</strong> practice at it and learn what works and what doesn't, and how to get around problems.</p>\n\n<p>All it takes is practice and in time you can become good at it.</p>\n"}, {'answer_id': 25467, 'author': 'grom', 'author_id': 486, 'author_profile': 'https://Stackoverflow.com/users/486', 'pm_score': 4, 'selected': True, 'text': '<blockquote>\n  <p>You can always use a <code>template engine</code> to add <code>variables</code> and\n  <code>calculated fields</code> to your CSS files.</p>\n</blockquote>\n'}, {'answer_id': 25471, 'author': 'AnnanFay', 'author_id': 2118, 'author_profile': 'https://Stackoverflow.com/users/2118', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>Then comes the multiple browser issue</p>\n</blockquote>\n\n<p>There is <a href="http://code.google.com/p/ie7-js/" rel="nofollow noreferrer">this</a> that helps remove some inconsistencies from IE. You can also use jQuery to add some selectors via javascript.</p>\n\n<p>I agree with <a href="https://stackoverflow.com/questions/25450/abstraction-away-from-css#25462">Dan</a>, learn it and it\'s not so much of a problem, even fun.</p>\n'}, {'answer_id': 25508, 'author': 'Rob Allen', 'author_id': 149, 'author_profile': 'https://Stackoverflow.com/users/149', 'pm_score': 2, 'selected': False, 'text': '<p>The key to a real understanding of CSS (and the browser headaches) is a solid understanding of the <a href="http://www.w3.org/TR/REC-CSS2/box.html" rel="nofollow noreferrer">box model</a> used by the CSS Standards, and the incorrect model used by some browsers. Once you have that down and start learning selectors you will get away from browser specific properties and CSS will become something you look forward to.</p>\n'}, {'answer_id': 25517, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 2, 'selected': False, 'text': '<p>See, this is the problem with SO-- every answer so far has made a valid point and should be considered the final answer. Let me try to sum up:</p>\n\n<ul>\n<li><a href="https://stackoverflow.com/questions/25450/abstraction-away-from-css#25457">CSS is good</a>! <a href="https://stackoverflow.com/questions/25450/abstraction-away-from-css#25462">To expand further</a>, there is a learning curve but once you learn it many things will be much easier.</li>\n<li>(Some) Browser inconsistencies <a href="https://stackoverflow.com/questions/25450/abstraction-away-from-css#25471">are solvable</a> generically.</li>\n<li>(Some of your) <a href="https://stackoverflow.com/questions/25450/abstraction-away-from-css#25467">Variable and calculated</a> field functionality can be taken care of through whatever templating engine you use.</li>\n</ul>\n\n<p>I think a combination of all these certainly solves a large sum of problems (although to be fair deeply learning CSS is not an option for everyone; some people just don\'t use it enough to justify the time).</p>\n\n<p>There are some problems none of the above points cover (certain types of calculated fields would require writing a JS library for, me thinks) but it\'s certainly a good start.</p>\n'}, {'answer_id': 25525, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 4, 'selected': False, 'text': '<p>If by some chance you happen to be using Ruby, there\'s <a href="http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html" rel="noreferrer">Sass</a>. It supports hierarchical selectors (using indentation to establish hierarchies), among other things, which makes life easier to an extend from a syntactical perspective (you repeat yourself a lot less). </p>\n\n<p>I am certainly with you, though. While I would consider myself a small-time CSS expert, I think it would be nice if there were tools for CSS like there are with Javascript (Prototype, JQuery, etc.). You tell the tool what you want, and it handles the browser inconsistencies behind-the-scenes. That would be ideal, methinks.</p>\n'}, {'answer_id': 25534, 'author': 'Kevin', 'author_id': 40, 'author_profile': 'https://Stackoverflow.com/users/40', 'pm_score': 3, 'selected': False, 'text': "<p>This elaborates on my previous answer.</p>\n\n<p>When I first started using CSS I also thought it was a pain that it didn't support variables, expressions, etc.  But as I started to use it more and more, I developed a different style to overcome these issues.  </p>\n\n<p>For example, instead of this:</p>\n\n<pre><code>a { color: red }\n.entry { color: red }\nh1 { color: red }\n</code></pre>\n\n<p>You can do:</p>\n\n<pre><code>a, .entry, h1 { color: red }\n</code></pre>\n\n<p>You can keep the color declared in one spot by doing this.</p>\n\n<p>Once you use CSS enough you should be able to overcome most browser inconsistencies easily.  If you find that you need to use a CSS hack there is probably a better way to do it.</p>\n"}, {'answer_id': 25553, 'author': 'Rob Allen', 'author_id': 149, 'author_profile': 'https://Stackoverflow.com/users/149', 'pm_score': 2, 'selected': False, 'text': '<p>For variable support, I have used PHP with CSS headers to great effect for that. I think you can do it in any language. Here is a php sample: </p>\n\n<pre><code>&lt;?\nheader(\'content-type:text/css\');\nheader("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); \n\n$someColorVar = "#cc0000";\n?&gt;\nBODY {\n      background-color: &lt;?= someColorVar ?&gt;;\n     }\n</code></pre>\n'}, {'answer_id': 25581, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>Solutions to problems seem to often involve jiggering numbers around like some chef trying to work out exactly how much nutmeg to put in his soon-to-be famous rice pudding</p>\n</blockquote>\n\n<p>I only get this when trying to make stuff work in IE.</p>\n\n<p>If you learn CSS to the point where you can code most things without having to look up the reference (if you're still looking up reference regularly you don't really know it and can't claim to complain I think), and then develop for firefox/safari, it's a pretty nice place to be in.</p>\n\n<p>Leave the pain and suffering of IE compatibilit to the end after it works in FF/Safari, so your mind will attribute the blame to IE, where it damn well belongs, rather than CSS in general.</p>\n"}, {'answer_id': 25698, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 2, 'selected': False, 'text': '<p>Also check out <a href="http://code.google.com/p/blueprintcss/" rel="nofollow noreferrer">BlueprintCSS</a>, a layout framework in CSS. It doesn\'t solve all your problems, but many, and you don\'t have to write the CSS yourself.</p>\n'}, {'answer_id': 26314, 'author': 'Scott Muc', 'author_id': 1894, 'author_profile': 'https://Stackoverflow.com/users/1894', 'pm_score': 2, 'selected': False, 'text': '<p>I believe the common errors beginners have with CSS are to do with specificity. If you\'re styling the <em>a</em> tag, are you sure you really want to be styling every single one in the document or a certain &quot;class&quot; of <em>a</em> tags?</p>\n<p>I usually start out being very specific with my CSS selectors and generalize them when I see fit.</p>\n<p>Here\'s a humerours article on the subject, but also informational:\n<a href="http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html" rel="nofollow noreferrer">Specificity Wars</a></p>\n'}, {'answer_id': 42081, 'author': 'Sam Murray-Sutton', 'author_id': 2977, 'author_profile': 'https://Stackoverflow.com/users/2977', 'pm_score': 2, 'selected': False, 'text': '<p>For CSS frameworks, you could consider <a href="http://developer.yahoo.com/yui/grids/" rel="nofollow noreferrer" title="YUI Grids">YUI Grids</a>. It makes basic layout a lot quicker and simpler, although used in its raw form it does compromise on semantics.</p>\n'}, {'answer_id': 249152, 'author': 'codeinthehole', 'author_id': 32640, 'author_profile': 'https://Stackoverflow.com/users/32640', 'pm_score': 0, 'selected': False, 'text': "<p>CSS takes a bit of time to learn, but the thing I initially found most discouraging was the fact that so many hacks were needed to get all browsers to behave the same way.  Learning a system which doesn't adhere to logic seems dumb... but I've clung to the vague belief that there is logic behind each browser's idiosyncrasy, in the form of the W3 spec.  It seems that the new generation browsers are slowly coming into line - but IE6 still makes my life hell on a daily basis.</p>\n\n<p>Maybe creating an abstraction layer between compliant/valid CSS code and the browsers' shoddy implementations wouldn't be a bad thing.  But if such a thing was created - would it need to be powered by JS (or jQuery)? (and would that create an unreasonably burden, in terms of processing cost?)</p>\n\n<p>I've found that it useful to 'level the ground' when scripting with CSS.  There are probably loads of different flavours of reset script out there - but using YUI resets has helped me to reduce the number of quirks I'd otherwise encounter - and YUI grids make life a little easier sometimes.  </p>\n"}, {'answer_id': 250219, 'author': 'Herb Caudill', 'author_id': 239663, 'author_profile': 'https://Stackoverflow.com/users/239663', 'pm_score': 0, 'selected': False, 'text': "<p>@SCdF: I think your summary here is fair. But the argument that some people don't have the time to learn CSS is bogus - just think about for a second. Substitute a technology that you've mastered and you'll see why: </p>\n\n<blockquote>\n  <p>I. Hate. Java. Is there something out there that will just write it for me? Not everyone has the time to master Java. </p>\n</blockquote>\n\n<p>CSS is certainly an imperfect technology - I have high hopes that 5 years from now we won't be dealing with browser incompatibilities any more (we're almost there), and that we'll have better author-side tools (I've written a Visual Studio macro for my own use that provides the the sort of variables and calculations that you describe, so it's not impossible) - but to insist that you should be able to use this technology effectively without really understanding it just isn't reasonable. </p>\n"}, {'answer_id': 250262, 'author': 'Adam Lassek', 'author_id': 1249, 'author_profile': 'https://Stackoverflow.com/users/1249', 'pm_score': 2, 'selected': False, 'text': '<p>CSS variables <a href="http://disruptive-innovations.com/zoo/cssvariables/" rel="nofollow noreferrer">are coming</a> (relatively) soon, but I agree they are long overdue. In the meantime, it is possible to use a CSS templating engine such as Sass, or even the dynamic web language of your choice, to generate your stylesheets programmatically.</p>\n'}, {'answer_id': 1097003, 'author': 'Artyom Shalkhakov', 'author_id': 134814, 'author_profile': 'https://Stackoverflow.com/users/134814', 'pm_score': 3, 'selected': False, 'text': '<p>Sorry to say that guys, but all of you missed the point.</p>\n\n<p>The word <em>abstraction</em> is the key. Say you and Sally are making a website. You are styling forms while she makes the corners round. Both you and she have defined a handful of selectors.</p>\n\n<p>What if, unknowingly, you picked class names that clash with the ones of Sally? You see, you can\'t "hide" (abstract out) the details when you work in CSS. That\'s why you can\'t fix a bug in IE then create a self-contained solution that others can use as-is, much like you call procedures in a programming language only caring about pre- and postconditions and not thinking of <em>how</em> it works on the inside. You just think of <em>what</em> you want to accomplish.</p>\n\n<p>This is the biggest problem with the web: it completely lacks abstraction mechanisms! Most of you will exclaim, "It\'s unnecessary; you stop smoking crack!"</p>\n\n<p>You will instead do the job of say, fixing layout bugs or making round corners or debating on the "best" markup for this or that case over and over again. You will find a site that explains the solution, then copy-paste the answer then adapt it to your specific case without even thinking what the hell are you doing! Yes, that\'s what you will do.</p>\n\n<p>End of the rant.</p>\n'}, {'answer_id': 4475869, 'author': 'Rimian', 'author_id': 63810, 'author_profile': 'https://Stackoverflow.com/users/63810', 'pm_score': 0, 'selected': False, 'text': '<p>You are thinking about this correctly though, you\'re probably still going to need to understand the different browser implementations of CSS. This is just understanding the environment your application lives in.</p>\n\n<p>To clarify: this isn\'t about understanding CSS. If you know the language well, you\'ve still got to handle the redundancy, duplication and lack of control structures in the language. </p>\n\n<p>Ive been writing CSS solidly for more than 10 years and I\'ve come to the conclusion that while the language is powerful and effective, implementing CSS sucks. So I use an abstraction layer like <a href="http://sass-lang.com" rel="nofollow">Sass</a> or <a href="http://lesscss.org/" rel="nofollow">Less</a> or <a href="http://xcss.antpaw.org/" rel="nofollow">xCSS</a> to interface to the language. These tools use a syntax similar to CSS so you\'re solving the problem in the problem\'s domain. Using something like PHP to write CSS works but is not the best approach.</p>\n\n<p>By hiding the problems in the language through an abstraction layer, you can deliver a better product that will maintain its integrity throughout the full life cycle of your project. Writing CSS by hand accelerates <a href="http://en.wikipedia.org/wiki/Software_rot" rel="nofollow">software rot</a> unless you\'re providing solid documentation which most CSS coders aren\'t. If you\'re writing a well documented CSS framework, you probably wouldn\'t write it by hand anyway. It\'s just not efficient.</p>\n\n<p>Another problem with CSS is due to it\'s lack of support for nesting block declarations. This encourages coders to build a flat, global set of classes and handle the name collisions with a naming convention. We all know globals are evil but why do we write CSS in such a way? Wouldn\'t it be better to give your classes a context instead of exposing them to the whole document model? And your naming convention may work but it\'s just another task you must master to get the language written.</p>\n\n<p>I encourage those of you who pride yourselves on writing good CSS to start applying some of the best practices from programming to your markup. Using an abtraction layer doesn\'t mean you lack the skill to write good CSS, it means you\'ve limited your exposure to the weaknesses of the language.</p>\n'}, {'answer_id': 39580288, 'author': 'keithjgrant', 'author_id': 212443, 'author_profile': 'https://Stackoverflow.com/users/212443', 'pm_score': 0, 'selected': False, 'text': '<p>You don’t need an abstraction away from CSS—you need to realize that CSS itself in an abstraction. CSS isn’t about putting pixels just so on the screen. Instead, it’s about writing a system of rules that help the browser make those decisions for you. This is necessary, because at the time you write CSS, you don’t know the content the browser will be applying it to; neither do you know the environment where the browser will be doing it.</p>\n\n<p>Mastering this takes time. You can’t pick up up CSS in a weekend and be good to go. It’s a bit deceiving, because the language has such a low barrier of entry, but the waters run <em>deep</em>. Here is just a few <a href="https://manning.com/books/css-in-depth" rel="nofollow">of the topics</a> you should seek to master to be proficient in CSS:</p>\n\n<ul>\n<li>The Cascade and Inheritance</li>\n<li>The Box Model</li>\n<li>Layout methods including floats and the new flexbox</li>\n<li>Positioning</li>\n<li>Current best practices such as SMACSS or BEM to keep your styles modular and easy to maintain</li>\n</ul>\n\n<p>You don\'t need to know this all up front, but you should continue pushing forward. Just as with other languages and programming in general, you need to continually seek to learn more and master the craft. CSS is a fundamental part of web development, and more developers need to treat it with the same respect they afford other languages.</p>\n'}]
['https://Stackoverflow.com/questions/25450', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1666/']