Try Tuts+ Premium, Get Cash Back!
Quick Tip: Never Type a Vendor Prefix Again

Quick Tip: Never Type a Vendor Prefix Again

Tutorial Details
  • Topic: CSS3, LESS
  • Difficulty: Moderate
  • Format: Screencast

You know the drill quite well. Want to give some section of your website rounded corners with CSS3? Then you’ll require nothing short of three vendor prefixes: webkit, moz, and the W3C recommended form. Isn’t that a huge waste of time — not to mention screen space? What if, instead, we could use a class file? Well, we can! I’ll show you how today.


The Key

If we use a tool like LESS or SASS, we can create our own class files quite easily. Have no idea what I’m talking about? Well, first, review this quick tip. It’ll teach you exactly how to get up and running with Less.


Classes File

Next, we need to create a core classes file that will be used in every project. Feel free to store this file anywhere you wish, though, in the video above, I use our popular (and exclusive) Structurer app.

We’ll do the first one together, but be sure to review the screencast for more details.

.border-radius( @radius: 3px ) {
  -webkit-border-radius: @radius;
  -moz-border-radius: @radius;
  border-radius: @radius;		
}

In terms of naming conventions, I’ve found that it’s smartest to use the officially recommended name for your class name — in this case, “border-radius.” To declare variables with Less, we preface them with an @ symbol. In this case, we’re setting a default value of 3px, though, should we need to override that value in our project, it’s a cinch!

#someElement {
   .border-radius(10px);
}

Some Examples

At this point, simply rinse and repeat for each property that requires multiple vendor prefixes. Here’s a handful to get you started:

Box Shadow

.box-shadow( 
	@x : 2px, 
	@y : 2px, 
	@blur : 5px, 
	@spread : 0, 
	@color : rgba(0,0,0,.6) 
) {
	-webkit-box-shadow: @x @y @blur @spread @color;
	   -moz-box-shadow: @x @y @blur @spread @color;	
			box-shadow: @x @y @blur @spread @color;	
}

Transition

.transition(
	@what : all,
	@length : 1s,
	@easing : ease-in-out
) {
	-webkit-transition: @what @length @easing;
	   -moz-transition: @what @length @easing;
	     -o-transition: @what @length @easing;
		  	transition: @what @length @easing;			
}

Box

.box(
	@orient : horizontal,
	@pack : center,
	@align : center
) {
	display: -webkit-box;
	   display: -moz-box;	
	        display: box;

	-webkit-box-orient: @orient;
	   -moz-box-orient: @orient;
	   		box-orient: @orient;
	   		
	-webkit-box-pack: @pack;
	   -moz-box-pack: @pack;
	        box-pack: @pack;		
	        
	-webkit-box-align: @align;
	   -moz-box-align: @align;
			box-align: @align;		
}

Flex

.flex( @val : 1 ) {
	-webkit-box-flex: @val;
	   -moz-box-flex: @val;
	        box-flex: @val;
}

Conclusion

I’d love to hear your thoughts on this. If you like the idea, let’s turbo-charge this stylesheet.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • mihai

    Hi, any idea why i cant compile classes.less with lessc? it works fine with the javascript, but when i try to compyle with lessc i get the following error:

    ←[31m! Syntax Error←[39m: on line 1: expected one of ←[33m! , )←[39m got ←[33m:←
    [39m after:

    .border-radius( @radius

    since i'm new to this it's look strange for me... i removed the default value for @radius and still nothing

    I also searched for a text editor that can work with less (i found one). It compiles the .less files on save, using lessc, and it's funny because in that, it passes 1st error that i get in cmd(if i remove spaces and value for @radius), and stops on line 7 at .outline-radius with the following error

    classes.less:7: syntax error, unexpected '.', expecting $end
    .outline-radius( @radius: 3px ) {
    ^
    [Finished in 0.219 seconds]

    I think it would be better to have the css file compiled for the live project, i haven’t tried php version yet.

  • http://twitter.com/madebycreature madebycreature

    Great tutorial and such a simple idea, however I’ve got to agree with Will Steinmetz and attempt some sort of integration with CSS3PIE, sadly IE will always be around!! :o)

  • vlight

    Hello, Guys! I have an issue with the importing an another *.less file (@import url(‘bla.less’))

    if I try to import another *.less file, I get an Error-Message:

    Couldn’t load http://localhost/LESS/_less/http://localhost/LESS/_less/classes.less (403)

    For some reason, less.js (1.0.41) puts “http://localhost/LES/_less/”-prefix to an @import url(…) declaration

    Any Ideas?

    P.S. Sorry for bad English…

    • mihai

      You’re doing it wrong, you don’t have to use url(…), just @import “filename.less”; or just @import “filename”; check less website, there are some good examples…

      • vlight

        @ mihai
        Thank you. I’ve also found it out today. Didn’t saw new site with documentation…

  • vlight

    Hey Guys! What stands @spread in .box-shadow() for ? and what kind of ‘data’ it takes? string/number/something else?
    I’m familiar only with x/y-offset, blur and color…

  • http://www.somnia-themes.com Nick

    @Vlight

    With spread you can change the width off the shadow. When you have a box off 100px, you can add a spread of 10px to the shadow. It will display as a 80px shadow centered under the box.

  • iVan

    hi there!
    beautiful post. most appreciated!
    i tried giving this classes sheet a twist but now it doesn’t compile…. this is what i added:

    .transform(
    @how : scale,
    @much : 1.1
    ) {
    -webkit-transform: @how(@much);
    -moz-transform: @how(@much);
    transform: @how(@much);
    }
    .opacity(
    @opa : 0.6
    )
    {
    filter: alpha(opacity=@opa * 100);
    -moz-opacity: @opa;
    opacity: @opa;
    }

    any help would be appreciated!

  • Axl

    This all sounds wonderful… Just that I’ve hit a wall here… I build all my sites around Joomla and the Gantry Framework. This doesn’t work there :(

    Does anybody have any good input ideas on how this could be used in the above mentioned setup (Joomla site with Gantry template)? :)

  • http://www.thoughtresults.com Saeed Neamati

    I love LESS. The only downside is that you should configure your web server to serve .less Internet Media Type, which is not a big deal in IIS7. But I don’t know about Apache.

  • http://biglifelabs.com Leo O’Donnell

    Great addition to any developer’s toolbox! We’re using SASS, so I’ve done a quick conversion and put it up as a public GIST here: http://gist.github.com/1621663

    Hope it helps…

    Leo

  • http://twitter.com/jitendravyas Jitendra Vyas

    How to get gradient in IE9, IE8 and IE7 using LESS class?

  • Jannik

    .transition(
    @what : all,
    @length : 1s,
    @easing : ease-in-out
    ) {
    -webkit-transition: @what @length @easing;
    -moz-transition: @what @length @easing;
    -o-transition: @what @length @easing;
    transition: @what @length @easing;
    }

    Why not do it like this:

    .transition(
    @what : all,
    @length : 1s,
    @easing : ease-in-out
    ) {
    -webkit-transition: @arguments;
    -moz-transition: @arguments;
    -o-transition: @arguments;
    transition: @arguments;
    }

    If you now want to change the arguments, you only have to change it once (DRY).
    (See the @arguments-Section in the LESS manual http://lesscss.org/)

  • http://sjevsejev.blogspot.com Sergej Jevsejev

    using this: http://sjevsejev.blogspot.com/2012/07/lessjs-function-generates-prefixes-for.html

    you need to write only this:

    .pf(‘transition’,'all 1s ease-in-out’);
    .pf(‘border-radius’,3px);
    .pf(‘box-shadow’,’2px 2px 5px 0 rgba(0,0,0,.6)’);
    .pf(‘flex’, 1);

    thats it :)