RSS Feed

‘LESS CSS’ Category

  1. CSS3 do’s and don’ts

    May 8, 2012 by Dave

    I’ve been seeing a lot about the Opera webkit craze recently and its gotten quite annoying because they’ve been pushed into it by people claiming they know CSS3 and how to appropriately code for mobile devices.

    Clearly this isn’t the case as Opera have said they are backing down on it all and “just doing it” because of us, the developers not using the vendor prefixes.

    WHAT?!

    Now its this that really pushes me over the edge, why aren’t we using the vendor prefixes? Because we don’t know them? Because its an extra line in our CSS and we can’t budget for it? The client didn’t say “We want it to work across all browsers on as many devices as possible.” none of these excuses cut it for me its just shoddy workmanship in my eyes.

    What would you say if a builder turned up to build an extension on your house with just the right number of bricks but PVA glue for mortar because you didn’t specify you wanted it to stay up during the rain?

    Most of what I see these days in terms of CSS3 is round corners and text-shadows which is disappointing considering the massive potential CSS3 has for animations and timed events. Although I don’t personally believe that behavioural code lives within CSS we even have click listeners in the latest spec. Very cool, when will JS be but a distant memory?

    But back to round corners and text shadows, I see this a lot which is the very reason Opera had to bend over and take it.

    .my-class {
    	-webkit-border-radius: 4px;
    	text-shadow: 1px 1px 1px #000;
    }

    If you can spot what annoys the hell out of me then good on you, we can be friends. If you can’t then keep reading on because this is a pet hate of mine.

    You’ve already started, why stop?

    In the code above you’ve already made it half way out of being a douche by not prefixing text-shadow so why is border-radius prefixed? Because people that don’t keep up with the specs, browsers or live under a rock are being allowed explicit access to a code base they don’t know enough about to add commercial value to, but I can’t pin it all on the developers.

    Where are our QA team?

    These people are our lifeline when it comes to releasing something to a wild, they make sure our tamed beast doesn’t turn around and bite us in the ass or eat out leg; they’re the one’s that check the quality of our code and make sure it works across as many devices as possible and they *should* give us a compatibility chart after each test to show us where our site doesn’t work so we can improve in the next version.

    Sadly, these days QA means “Check it looks ok and give it the green light ASAP” I remember when QA-ers opened your code to check there wasn’t anything disgusting in it like this

    #header h1 a span#logo span {
    	border:1px solid #F00!important;
    	border-bottom:2px solid #00F!important;
    }

    *shudder* ok that was a bit too gross but still, they do not check for these things anymore, I call them browser vulnerabilities because the code is so bad its actually likely to break in newer versions of any browser or when some clever clogs inspects element and plays with the inner workings of your site.

    So what aren’t they doing, well they’re not snooping at our code making sure we have all the prefixes we need, see below for the prefixes we’re all failing to use:

      • -o-
      • -moz-
      • -ms-
      • prefix-less

    There aren’t many additions so why are we skipping them all and just using a poor mixture of prefix-less properties and webkit, pushing web innovators into bending over backwards to support a competing vendors prefix.

    We’re all human

    We’re all human and its natural to want to get from a to b as quickly as possible with as little expenditure as possible, in this case the expenditure is the energy used to think and type.. poor I know but it seems most of us don’t have the energy, time, money, whatever other excuses you can come up with I’m sure.

    And ok, not all of us have the time to prefix everything, this is why pre-processing libraries such as Sass, LESS and Compass are all great (and by the way I have built a library for all of them) because we can consolodate our code into one function and call it willy nilly and not have to worry about the prefixes because the functions handle that for us; but I’ll get that a bit later.

    What I want to talk about is how to ensure you’re providing the best solution to your clients and the sub-sequent developers that get hold of your project. This can be ensured by QA-ing your own work on multiple screens/devices and at multiple resolutions to ensure the CSS you have written is solid enough for any of the *people* out there to play with, and we all know they’re going to play with it, its human nature to understand everything we can about something we’re interested in (So unless you’re a marketing genius or pirate and have people with no interest visiting your site) that’s hopefully everyone.

    Its simple, make sure you use the technologies available to make your job easier such as LESS and Sass and use libraries that are all over the internet to help you, there’s no shame in learning from someone else’s ideas and work; I wouldn’t be in the position I’m in now to tell you how to do it if I weren’t where you are now so firstly lets look at the LESS option.

    LESS extends CSS with dynamic behavior such as variables, mixins, operations and functions. LESS runs on both the client-side (Chrome, Safari, Firefox) and server-side, with Node.js and Rhino

    Its truly brilliant, much like Sass but with easier to remember syntax and there are some truly brilliant libraries out there to cut the work down even further, even I made one available here

    https://github.com/davemackintosh/My-HTML5-CSS3-templates—tools

    and there are many more, its the same with Sass; below is their blurb

    Sass makes CSS fun again. Sass is an extension of CSS3, adding nested rules, variables, mixins, selector inheritance, and more. It’s translated to well-formatted, standard CSS using the command line tool or a web-framework plugin

    And my effects library will be converted to Sass just as soon as I can find the time! If you don’t want to use LESS or Sass or can’t use anything like that, at least settle for the brilliant prefix-less JavaScript library to help you out.

    </rant>

    I hope we all learned something here. I will continue to rant about this issue if I have to!


  2. CSS pre-processing with LESS

    January 27, 2012 by Dave

    While I spend much of my time elbow deep in a mixture of PHP, ColdFusion and .NET I probably spend about 60% of my time cringing at the state of old websites CSS and writing new stylesheets and I try my best to keep up with the latest front end technologies and for the last 6 to 8 weeks I have been using (and abusing I must admit) LESS which has in my opinion been an absolute god send, from both a management and developer point of view.

    Gripe…

    It allows the use of mixins, variables and adopts a fairly DRY methodology, until you render.

    I’ll get the gripe over with before I move onto some of the plusses of LESS; during any CSS writing I’ll try to visualise the formation of my selectors so that I can maximise the effect of DRY and for years and years that has worked for me, so when I heard about LESS, researched it I was gob-smacked to see I could write arrow-head formated code and not generally have to worry about repeating selectors over and over. This was all until I came to compiling, take this LESS snippet for instance:

    #myunorderedlist {
        //Clear the element
        overflow:hidden;
        border:1px solid #F00;
    
        li {
            float:left;
            border-right:1px solid #F00;
    
                a {
                    padding:8px 20px;
                    text-align:center;
    
                    &:hover {
                        cursor:pointer;
                        text-decoration:underline;
                    }
                }
        }
    }

    Will end up like this, with a lot of repeated selectors that aren’t exactly necessary:

    #myunorderedlist {
        overflow:hidden;
        border:1px solid #F00;
    }
    #myunorderedlist li {
        float:left;
        border-right:1px solid #F00;
    }
    #myunorderedlist li a {
        padding:8px 20px;
        text-align:center;
    }
    #myunorderedlist li a:hover {
        cursor:pointer;
        text-decoration:underline;
    }

    This isn’t a massive issue for me, I recently said for everything good out there, there is something twice as bad and twice as stupid so I took this with a pinch of salt and hit the drawing board to try and think of a way to morph my working method into something to reduce this. I failed. But having said that, the amount of time saving involved with the use of mixins is fundamentally one of the biggest time savers of my day and the ability to organise my CSS into separate files and compile out to one is something of a miracle.

    @imports make me shudder..

    Now, normally when I see @import ‘some-file.blah’; I feel a little sick and shaky, these are a personal hate of mine. I have no idea why, but I hate these but LESS has taken it to another level and allowed for *.less inclusions which on compilation are actually written to the callee.

    Take the below code for example:

    @import 'effects.less';
    //For anyone out there reading this, NORMALISE stop resetting!! :) 
    @import 'normalise.less';
    @import 'html5.less';
    
    html {
        min-height:100%;
    
        body {
            min-height:100%;
        }
    }
    
    @import 'home.less';
    @import 'about.less';
    @import 'services.less';

    So I’ll take it from the top, the other day I made public a CSS3 effects bootstrap that is cross browser (of course excluding IE) and I import that into every project that I think is going to use any kind of transitional effects (which lets face it is becoming more and more popular/regular) this file contains nothing but a disclaimer and a set of mixins to make my job easier and faster and if I don’t use any of the functions the disclaimer is the only thing that is output in the CSS.

    I NEVER reset, sorry Eric Mayer you’re a legend but its not the best anymore. I normalise which I find much better and I’ve got my own LESS file for this, just run the CSS > Sass converter tool to convert this to LESS and tidy it up a bit.

    In the example I set a few basic rules on the html and body tags, this was just representational so you don’t need these; I then start importing my page specific files.

    The actual ability to import page specific stylesheets is brilliant (in the sense it ends up in the same file) although it does create an unhelpful amount of bloating but its quicker than writing plugins for CMS’ to attache specific stylesheets to pages. It also allows for more than one person to work on a website and compile to the same file with no loss of data which is brilliant for where I work and what I do.

    Opinions on other issues

    A few people mentioned that the use of debug toolbars (Firebug, webkits toolbar, Dragonfly, etc) to check css layouts is annoying when using CSS pre-processors as 100% of the time the line-number is wrong making it difficult to find the actual line of code that is wrong. Big deal I say, organise your code more efficiently using LESS imports and mixins/variables or any other way you find beneficial.

    Its also been heard that LESS/Sass makes front end developers code lazily, I completely disagree with this. I find it incredibly hard to code lazily with LESS as the arrow-head formation makes it easy to read and search through so amends are easy to implement, also with the ability to use JavaScripts // comment syntax you can clearly label your selectors to something more memorable to search by or note a change made/original value.

    Conclusion

    LESS/Sass and in general CSS pre-processors are great for some projects, perhaps not all but definitely a time saver for me 80% of the time, it requires no training because it is just far too easy to use. For any HTML5 or CSS3 projects its almost an absolute must with the likely-hood of falling into the no-DRY hole, easily fixed with imports and libraries containing different effects its a dead-cert that you’ll love using LESS but find it gainful only half of the time depending on your work type.

    Give it a go, after all its free and it takes no time at all to learn it. Also download Crunch its the best LESS editor/compiler currently available. Even if it doesn’t work on Ubuntu just yet ;)


  3. LESS CSS effects bootstrap

    January 22, 2012 by Dave

    LESS CSS is possibly the greatest thing to happen to any front end developers job, it handles mixins, parameters and on top of all of that it can be play a big role on the performance of your site with its compiler. To add to the Awesome (yeah, with a capital A) I often involve the compiler within my Capistrano scripts to automate everything during deployment.

    But this post isn’t about that, not this time :) I’m here because I’ve been working on a generic effects.less which contains a set of generic mixins I use so very regularly its a simple include. Download it here!

    To use the file just stick this in the top of your LESS file.

    @import 'directory/to/effects.less';

    And you will have added the below effects to your CSS library instantly:

    1. .rounded ([@radius]);
      This adds a border radius to your element with the specified radius
    2. .border-radius ([@topleft: 4px, @bottomleft: 4px, @topright: 4px, @bottomright: 4px]);
      This allows you to specify each corners radius seperately
    3. .transition ([@transit: all, @speed: 1s, @ease: linear]);
      This watches for transitions and allows you to, cross browser (Bar IE) set the transition methods
    4. .boxShadow ([@horiz:0px, @vertic:0px, @blurRad:5px, @color: #000]);
      Adds a shadow to any element capable of having a shadow, this is an OUTER shadow
    5. .insetBoxShadow ([@horiz:0px, @vertic:0px, @blurRad:5px, @color: #000]) ;
      Adds a shadow to any element capable of having a shadow, this is an INNER shadow
    6. .gradient([@color: rgb(36,36,36), @start: rgb(36,36,36), @stop: rgb(89,89,89)]);
      This applies a very simple bottom/bottom linear gradient to the selected element
    7. .perspective ([@amount:800, @left: 50%, @top: 50%]);
      This applies perspective to your element for 3D transforms
    8. .perspectiveOrigin ([@left:50%, @top:50%]);
      This is automatically called by .perspective() and sets the origin of the transform
    9. .preserve3D ();
      This preserves any 3D aspect attached to the element during transforms

    I hope very much that you all enjoy using this file, its sped up my development no end and deserves sharing :)

    If you like it and think I deserve a beer then just send me a buck or two via Paypal to the address in the top of the downloadable file.

    happy LESSING!