Creating a generic CSS3 property retriever function

Now that we understand the principle behind picking out the variant of a CSS3 property the browser supports, lets build on that and create a more generic function that accepts any original CSS3 property, and returns the variant the browser supports. What's involved here is first to define an array of browser CSS prefixes:


Notice how the first element is an empty string, for when the browser supports the original, unprefixed version of the property. We'll then loop through this array and combine it with any original CSS3 property to form the final JavaScript property for each variant, testing for support for that property. JavaScript properties follow the camel case syntax, meaning a CSS property such as -moz-border-radius needs to be transformed into MozBoxShadow, and ms-transform to msTransform etc. The following table lists the various vendor prefixes and their JavaScript equivalents:

CSS vendor prefixes and JavaScript equivalents
CSS prefix JavaScript equivalent
-ms- ms, such as msTransform
-moz- Moz, such as MozBoxShadow
-webkit- Webkit, such as WebkitBorderRadius
-o- O, such as OTransform
-khtml- Khtml, such as KhtmlTransform

So as you can see, with the exception of -ms-, all of the other CSS vendor prefixes when converted to JavaScript camel case should have their first letter capitalized. After that, the conventional camel case rules apply when converting the original CSS property to camel case.

Ok, it's time now to wrap all of the above into a function that will test for and return the version of the entered CSS3 property supported by the browser:

function getcss3prop(cssprop){
	var root = document.documentElement
	function camelCase(str){
		var css3propcamel = camelCase( css3vendors[i] + cssprop )
		if (css3propcamel in
			return css3propcamel
	return undefined

To use this function, simply pass in an unaltered CSS property, and the function will return the vendor specific, JavaScript equivalent property supported by the browser:

getcss3prop('border-radius') // returns 'borderRadius' or one of the variants, such as 'MozBorderRadius', 'WebkitBorderRadius' etc

If the browser doesn't support a particular CSS property period, undefined is returned. With the appropriate CSS3 JavaScript property returned, you can go about manipulating it in your code, such as:

var el = document.getElementById('mydiv')[getcss3prop('border-radius')] = '15px'[getcss3prop('transform')] = 'rotate(180deg) scale(1.05, 1.05)'[getcss3prop('transition')] = 'all 1s ease-in-out'

To further illustrate our getcss3prop() function in action, enter any CSS property inside the form above and press enter to see it return the corresponding CSS3 JavaScript property:

getcss3prop() function demo:

With the getcss3prop() function, we can easily retrieve the supported variant of any CSS property and manipulate it in JavaScript!