Forum Moderators: not2easy

Message Too Old, No Replies

CSS Variables, Mixins, Nesting, and Modules

Chrome, CSS preprocessors, W3C proposal

         

SuzyUK

11:14 am on Jan 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tab Atkins Jr, a Google employee working on the Chrome Browser, also a member of the CSS Working Group has some interesting proposals to make to the W3C - [xanthir.com...]

Many of these improvements are directly inspired by CSS preprocessors like SASS and LESS.

NOTE: Everything described in this post is experimental, and in the process of being proposed to the CSS Working Group. All syntax may vary, possibly significantly, when the experimental implementation is finally exposed..

swa66

1:05 am on Jan 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The mere concept of taking CSS variables serious implies one does not understand how CSS works.

Sad that such proposals even get off of the desk of the one dreaming it up, even sadder to see it go to a standards group.

Why dumb it down ...

Why CSS variables are evil:
[webmasterworld.com...]



On first looks the other proposals are of the same level of lack of quality and thought.

SAD.

SuzyUK

5:02 pm on Jan 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



maybe not so bad?

Once they hit public, these things should be usable in "protected spaces" like Chrome extensions or apps from the Chrome Web Store.


that is how they should be used, browser specific code for browser specific apps. Maybe his proposal to the CSSWG is just a courtesy, and also I suppose if browsers are to bring in their own preprocessing rules then some agreement on syntax would be an advantage

Once a preprocessor is built, you can start using the syntax immediately for your public site, potentially even before this becomes public in Webkit!

That sounds to me like the old reasons for NOT building it into CSS itself may be adhered to, and a preprocessor (like Ruby for Sass, or PHP) is still going to be a requirement.

It's not the variables/functions themselves which are a problem, well no more than CSS Frameworks ;) people will still use them - it's that CSS doesn't need changed into a scripting language IMHO!

Anyone see a Google API/Tool in their future ;)

@swa.. I's so forgotten about that thread, however thankfully I didn't cringe, it makes a good read today especially in light of surge of use of "Frameworks" although CSS preprocessing is not a framework itself it can unfortunately be used to "plug them in".

it's horses for courses, I can't be bothered to remove all the unnecessary classes my CMS spits out in the HTML, (though have removed some) however just because they're there also doesn't mean I always use the "plug-in" CSS for themes

At the end of the day variables and scoped mixins is a way of plugging themed buttons/menus/gradients/shadows/boxes into a site - that's what people want, instructions how to upload and go! They don't all care to learn the required CSS to do it. Their stylesheets may suffer, but in some cases they do already under the weight of entire frameworks because they don't know how to/or can't be bothered to remove the unnecessary parts.

- actually playing around with Sass/LESS [webmasterworld.com] properly, I discovered you could plug in a framework AND hide the bits you don't need, so maybe it will actually help some?

It will happen, has already really, and it would be nice if they could somehow agree on syntax, my vote is for ScSS at the minute however I see the value in Tab Atkins proposal for scoped mixins - and as long as it doesn't happen within CSS itself it's not so bad

swa66

10:13 pm on Jan 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For those reading this that might not know why I'm saying it's not needed at all:

to take the proposed example for mixins:

@mixin error {
background: #fdd;
color: red;
font-weight: bold;
}
div.error {
border: thick solid red;
padding: .5em;
@mix error;
}
span.error {
text-decoration: underline;
@mix error;
}


can today be done without any problem in 99.99% of the browsers in use as

span.error, div.error {
background: #fdd;
color: red;
font-weight: bold;
}
div.error {
border: thick solid red;
padding: .5em;
}
span.error {
text-decoration: underline;
}

It'll do exactly what they propose.

And due to the cascade -at least for those who care to learn how it works- there are nuances to play with with what overrules what - but since they didn't talk about how conflicts would be resolved in the proposal, I can't match it here as the proposal is not clear. not that the example is complex enough to have any such issue anyway.

I'm sure it's easy to get developer excited on first looks, but somebody who understands and knows CSS should not get exited over this as it's already well within the reach of today's CSS.
And where it isn't yet, server side processing (on the fly or pre-processing); or client side scripting (e.g. javascript) already are quite possible.

Browser specific extensions should not start with e.g. @mixin, they are to be marked in their case with -webkit... that's where the proprietary stuff belongs and they should not be sent to standard setting bodies.

I've been coding html, xhtml and css for years, always done it fully by hand and always tried to adhere to standards as much as I could manage. I find it utterly sad that the latest trend is to make it all a lot dumber and without any respect for what it is supposed to be.

SuzyUK

11:19 pm on Jan 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree swa, I looked over a few of my stylesheets and couldn't really write them easier or quicker, but they are one off, and usually as small as I can make them and still read them BUT I do not work in a team development environment so I'm not really up for too much templating apart from copy/paste snippets.

However I did find something which might help illustrate an example on behalf of the "for" side - SaSS actually tidied a single line off one of my final CSS :o

Underlining that precompiling could have benefits when used along with a knowledgeable dev person/team - So, if you actually know how to use grouped selectors and specificity, but want the development sheets to remain grouped by their content area (which is my favourite way of organising a stylesheet anyway) for readability or you have to be because different teams work on different sections or sheets:

in a 'defaults' sheet you have :
p {color: #00f; background: #fff;}
p:hover {color: #000; background: #efefef;}


then in another sheet dealing with the main content area you have more specific styles..

#main {background: #efefef;}
#main p {color:#000; background: #efefef;}


A bit of a weird example perhaps, but I wanted to keep it as simple as possible (no variables/constants mixins or formula included) and not use links because of the other ways of getting specific with them

Now the obvious easy way to shorten, or remove the repetition (the DRY principle that programmers like ;)), for those of us who know how with CSS itself, and they are just two lines apart in this example ;) - is to group the selectors which have common property:value; pairs - like:
p {color: #00f; background: #fff;}
#main {background: #efefef;}
p:hover, #main p {color:#000; background: #efefef;}


BUT regardless of even if you know how - to do that you have to have to be in personal control of your stylesheet and also not care about keeping the stylesheet "pretty", all you've done is tidied it by one line, yes removing one repetition. In my case I would usually stick with repetition (and yes that's the line which Sass cleaned!) or I have to break my nicely specifically sectioned bits AND if they were actually in two different sheets which were in the control of different dev depts in an organisation, they might have no choice but to stick with the repetition as they may not have access to the default sheet to get them grouped.

With compilers and the extensions they offer, both me and the devs are going to be happy, I can "section" my one already fairly tidy sheet and now use as many comments as I like - the dev teams too can keep their repeated directive in the two different sheets while developing, again with however many reference notes and reasons they need to - because for both of us - the sheet or sheets can then be COMPILED into the grouped selector version, into one single, compressed sheet, without comments, for production

That's the positive side for developers and workflow, the original precompiled versions might not, probably won't be, very much shorter than what we know the final result will be - it doesn't matter, the end sheet will be perfect, ONE, small compressed CSS from as many sources as it takes - everyone wins

so that's where this is interesting and time/bandwidth/http request saving (eventually after the training/learning of best practices in the first place!). I will repeat that although I'm excited about this sort of technology I still agree with the article swa linked in post #2

- this functionality DOES NOT belong in CSS itself, it is a tool, a means to an end, the end being a perfectly formed CSS doing no more than CSS is capable of doing in the first place.. you have to know CSS in order to get the best out of a precompiler in the first place. hence I'm sure it will, does have it's place in applications - so if Google wants to introduce it in their browser for those reasons so be it

Variables (or "symbolic constants" for those that prefer their proper name ;)) in CSS itself would just be the start of a decline into muddying the different technolgies


----
btw to do the above example in Sass btw you would use @extend

p {color: #00f; background: #fff;}
p:hover {color: #000; background: #efefef;}

#main {background: #efefef;}
// you can put as many hidden notes as you like to refer to where the original is if you like
#main p {
@extend p:hover
} 


it will come out looking like we know it should honest, actually it comes out like this..

p {color: #00f; background: #fff;}
p:hover, #main p {color:#000; background: #efefef;}
#main {background: #efefef;}


While I am interested in the technology, I'm not happy with some of the marketing or conference speeches I've seen regarding it, the mud-slinging or name calling is not helping your cause. This technology is exciting, it will be abused and will produce unnecessarily complicated ways of doing what is already possible until it or its users settle down to the realisation that it can't do any more than output CSS that will function exactly the way it functions at this time. precompiler functions, mixins, plugins, extensions etc only help you achieve a final CSS output - they do not increase its supported abilities.

swa66

8:11 pm on Jan 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the basis of this is (in part) a lack of awareness on what CSS can do and the philosophy at the base of it all.

Even examples in courses and on sites such as these often neglect some of that philosophy, either out of a concern to keep it simple. Or -maybe- as an effect of propagation of the author him/herself not knowing it either.

When I see out here and elsewhere questions like what order the classes should be mentioned in a CSS file, and getting serious answers without anybody pointing out the order has a certain importance in the cascade, but that beyond that there's no need for an order, let alone that you need to group all settings you do for a certain selector together in one block.

Honestly, originally I was asking for variables myself, it was too hard to type the right colors in all the right spots to keep with the chosen theme.

Then I started to do it like this (inspired on a real site):

/* reset */
* {
margin:0;
padding:0;
}
/* defaults that get inherited */
body {
font-family: Palatino, Times New Roman, Times, serif;
font-size: 90%;
}
/* theme red */
.mainmenu a span em, h2, h3, h4, h5, h6, .menu a {
color: #be171a;
}
.extra, .footer {
background-color: #be171a;
}
/* sans-serif font */
h1, h2, h3, h4, h5, h6 {
font-family: Arial, Helvetica, sans-serif;
font-weight: normal; /* Opera needs this */
}
/* full height body */
html {
background-color: #ccc;
height: 100%;
}
body {
background: white;
min-height: 100%;
width: 770px;
border-right: 1px solid black;
border-left: 1px solid #555;
position:relative;
}
...
/* extra class on the body generates 2 columns */
body.extra {
background: white url('redstripes.png') repeat-y -14px top;
}
...

SuzyUK

10:16 pm on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



an excellent example of the right way to do it :)

but, you knew that but was coming right ;)

I found a wee exercise that I could waste some time on, (actually it's only the SitePoint HTML & CSS Competition), the idea is not necessarily to enter the comp, though I might, if of course I don't lose my patience first.. but to find something that gave me set of rules to work within and also to take the pressure off (no client breathing down the neck!) so that I didn't keep my trusty old HTML editor open for hand coding!

I'm test-driving a new editor [webmasterworld.com], very compass/sass friendly and also An HTML Zen Coding plugin.. all at the same time.. actually does the fact that HTML also has a "shorthand" plugin means the "mixin/extend" functionality needs built into HTML too? ;) Rhetorical!

I want to see if I can have my cake and eat it too, and as still don't think it's CSS's responsibility to do that for me I'd better try what is available, huh?

interesting results so far, and only just done an IE Filter section.. competition rules state no JS or images, just HTML and CSS with graceful degrade for IE, so filters are the order of the day I'm afraid ;)

Step one: I cheated already sorry due to trial and error with the filters which work differently across IE versions this wasn't a job for "how to code" it was a job for hand code and preview LOL.

but after finalising that bit, my original, hand-written CSS with usual notes (a bit like Swa's example) and all properties on one line for readability = 96 lines

from here I'm switching over to Aptana, so I pasted my handwritten CSS and then Sassified it - the same CSS with MORE notes added for my learning Sass curve, = 94 lines

First surprise result! I had expected the Scss to be longer but it's actually 2 lines shorter, it still has every property:value on one line, but it now makes use of the @extend property to save retyping selectors - I can keep my IE6/7/8 css (yes there are three different sets! though obviously with a lot in common) in separate sections with notes because I don't usually go to so much bother, or use so many IE filters - I want to!) AND then because I'm new to Scss I left loads of comments too, lots more than I had in my original CSS - so not bad considering

However I then have the CSS configured to compile so that each ruleset is on one line (the config can be changed at any time to recompile showing line numbers for debugging if required).. and the "final" CSS, for this part of the project, is just 14 lines

comments are not in final sheet, and I could compact it even further (minimise it) but there ya go. I'm impressed

All the above is still not using any "variables" (I'm not kidding you, to even get a graceful degradation the colors are nothing like I will use in "good" browser CSS, in fact for them it will probably be RGBA - whereas IE is plain hex or rgb) - so perhaps I'm not getting this variables are the be all and end all and "should've been in CSS since day one" ideas yet.. maybe that's what Bert Bos meant when he said they was only the beginning? who knows

So far the tidying and hard work is being done by Compass (it's watching and updating my CSS file live, the one that will be used in "production") - I just slightly rewrote the nesting structure using @extend so that the original Scss file still makes perfect sense to my preferred way of writing the CSS (virtually the same as Swa's example above!) very little change to my preferred logic none actualy so far, I even used some extra nesting and spacing! - for readability.. just because I could

<< learns by doing and is amazed so far
:)