3D Cube using CSS transformations

Update (July 21st): Using newly released (cur­rently only in Web­kit Nightly releases) 3D trans­forms, a three dimen­sional, rotat­ing cube with cor­rect per­spect­ive is pos­sible, and as a bonus – the cube can be nav­ig­ated using arrow keys. Check out the latest demo, “Anim­ated CSS3 cube using 3D trans­forms”.

The impres­sion of a three dimen­sional cube can be cre­ated using mod­ern CSS tech­niques, without the need for JavaS­cript, imagery, can­vas or SVG. Using the pro­pri­et­ary trans­form prop­erty to skew and rotate shaded rect­angles, indi­vidual cube faces can com­bine to form a 3D object. Cur­rently only sup­por­ted in recent Web­Kit and Gecko based browsers, most import­antly Fire­fox 3.5+ -moz-transform (doc­u­ment­a­tion) and Safari 3.2+ -webkit-transform (doc­u­ment­a­tion).

To demon­strate the power of this effect a second exper­i­ment with mul­tiple cubes and pro­pri­et­ary Web­Kit trans­itions is also available.

Res­ults

A 3D cube cre­ated with CSS
Update (June 7th): Altered CSS slightly to use skew(x,y) rather than skewY, the lat­ter of which is not sup­por­ted in Safari 3 / Chrome.
Sup­por­ted browsers: Safari 3.2+, Google Chrome, Fire­fox 3.5+

Exper­i­ment with mul­tiple cubes and CSS trans­itions, still no JavaS­cript
Sup­por­ted browsers: Safari 4+, Google Chrome

Multiple cubes created using CSS

How To

Sim­ilar to my pre­vi­ous exper­i­ments, the HTML markup is very simple. Each of the faces has its own DIV, class and con­tent. The top face requires some extra markup to aid the trans­form­a­tion, more on that shortly.

<div class="cube">
	<div class="topFace">
		<div>
			Content
		</div>
	</div>
	<div class="leftFace">
		Content
	</div>
	<div class="rightFace">
		Content
	</div>
</div>

A short dis­claimer, the geo­metry in this example is ‘fudged’, in that the val­ues have been adjus­ted to appear roughly cor­rect. I know that the dimen­sions are slightly out of whack, this is merely to save my head from math­em­at­ics and to get the concept out there quickly for people to see. With that said, let’s crack on with the CSS.

Each of the three rect­angles is given a slightly dif­fer­ent shade of gray to give the impres­sion of depth, in this example the left face is in shadow. The faces are each posi­tioned abso­lutely, rel­at­ive to the cube con­tainer. Each face is 200 x 200 pixels, includ­ing 10 pixels of padding.

.cube {
	position: relative;
	top: 200px;
}

.rightFace,
.leftFace,
.topFace div {
	padding: 10px;
	width: 180px;
	height: 180px;
}

.rightFace,
.leftFace,
.topFace {
	position: absolute;
}

Now for the fun bit. The left and right rect­angles are skewed by ±30˚ along the ver­tical axis, with the right face shif­ted left by 200px, cleanly lin­ing up the two edges to cre­ate a corner that is cen­ter aligned.

.leftFace {
	-webkit-transform: skewY(30deg);
	-moz-transform: skewY(30deg);
	background-color: #ccc;
}

.rightFace {
	-webkit-transform: skewY(-30deg);
	-moz-transform: skewY(-30deg);
	background-color: #ddd;
	left: 200px;
}

The top face proves more prob­lem­atic; it needs to be skewed, scaled, rotated and posi­tioned. The skew is the same, –30˚ along the ver­tical axis, this skewed rect­angle must then be rotated clock­wise by 60˚. Rotat­ing the rect­angle itself leads to a change in ori­ent­a­tion of its con­tent, a con­tainer must be added and then rotated.

A simple way of cre­at­ing a top face without resort­ing to maths is to duplic­ate the left and right rect­angles, skew them in the oppos­ite dir­ec­tions (by invert­ing the sign, e.g. left face is now skewed by –30˚) and pos­i­tion them against the exist­ing faces to cre­ate a dia­mond shape between the two sets. Now use pos­i­tion­ing and scal­ing to fill this dia­mond and form the top face, delet­ing the duplic­ates when fin­ished. My res­ults led to a scal­ing factor of 1.16 in the Y dir­ec­tion which I have accoun­ted for by redu­cing the font-size by the same factor.

.topFace div {
	-webkit-transform: skewY(-30deg) scaleY(1.16);
	-moz-transform: skewY(-30deg) scaleY(1.16);
	background-color: #eee;
	font-size: 0.862em;
}

.topFace {
	-webkit-transform: rotate(60deg);
	-moz-transform: rotate(60deg);
	top: -158px;
	left: 100px;
}

The final CSS looks like this:

.cube {
	position: relative;
	top: 200px;
}

.rightFace,
.leftFace,
.topFace div {
	padding: 10px;
	width: 180px;
	height: 180px;
}

.rightFace,
.leftFace,
.topFace {
	position: absolute;
}

.leftFace {
	-webkit-transform: skewY(30deg);
	-moz-transform: skewY(30deg);
	background-color: #ccc;
}

.rightFace {
	-webkit-transform: skewY(-30deg);
	-moz-transform: skewY(-30deg);
	background-color: #ddd;
	left: 200px;
}

.topFace div {
	-webkit-transform: skewY(-30deg) scaleY(1.16);
	-moz-transform: skewY(-30deg) scaleY(1.16);
	background-color: #eee;
	font-size: 0.862em;
}

.topFace {
	-webkit-transform: rotate(60deg);
	-moz-transform: rotate(60deg);
	top: -158px;
	left: 100px;
}

25 Comments

  1. Posted May 8, 2009 at 5:50 am | Permalink

    Nice one! I did the very same for a demo last year, but with a single back­ground tex­ture that gets pro­jec­ted on the cube (this is cool for online games).

    Also, you might con­sider scal­ing down the outer box again so the size will be equal to the ori­ginal size.

  2. Posted May 8, 2009 at 8:03 am | Permalink

    I am without words for this tutorial. Congratulations.

  3. Posted May 8, 2009 at 9:00 am | Permalink

    Hello, very cool.

    so, wie can nice cre­ate nice Web­designs in the future, but now ist music for tomorrow.

    Best regards from ger­many
    Kasi

  4. Posted May 8, 2009 at 11:24 am | Permalink

    Awe­some

  5. Rodney Reid
    Posted May 8, 2009 at 12:44 pm | Permalink

    Wow very nice Paul — you are a CSS god :)

  6. Posted May 8, 2009 at 5:55 pm | Permalink

    Very cool fea­ture!
    Thanks for sharing.

  7. Posted May 9, 2009 at 10:50 am | Permalink

    great work paul :)

  8. wavded
    Posted May 9, 2009 at 2:16 pm | Permalink

    Your second demo seems to work OK on Fire­fox 3.5 Beta 4. Unless I’m miss­ing something.

  9. Posted May 9, 2009 at 2:54 pm | Permalink

    The anim­a­tion which moves the boxes in and out on hover will not work in FF3.5

  10. michel perrin
    Posted May 10, 2009 at 2:28 am | Permalink

    Curi­ously, the first demo doesn’t work in my safari V3.2.1

  11. Posted May 10, 2009 at 7:39 am | Permalink

    Super cool. Thank you for the tutorial and excel­lent work !

  12. Posted May 10, 2009 at 9:48 pm | Permalink

    Amaz­ing, i dont think we can do this using css before

  13. Posted May 11, 2009 at 1:38 am | Permalink

    Very nice job on this one! Let’s hope trans­forms will be there in the released W3C stand­ard at some point.

  14. Posted May 12, 2009 at 4:18 am | Permalink

    Where’s the Z axis then?

  15. RaBBi
    Posted May 13, 2009 at 1:14 am | Permalink

    Like michel per­rin, the first demo doesn’t work in last updated Safari 3.2.3 ^^
    Only the top­face cube is skewed.

  16. Posted May 14, 2009 at 3:47 am | Permalink

    excel­lent!
    thx

  17. Posted May 14, 2009 at 6:30 am | Permalink

    Inter­est­ing stuff.

  18. Posted May 21, 2009 at 12:22 pm | Permalink

    Now if it could just take per­spect­ive into account, then i would be REALLY impressed.

    For me this is cur­rently look­ing more like ‘gim­mick’ than ‘use­ful fea­ture’. We shall se …

  19. Ken
    Posted May 23, 2009 at 12:24 pm | Permalink

    Nifty!

    FWIW: the first example, which says it works in Chrome, doesn’t work in Chrome here. But the second one does.

  20. Posted May 28, 2009 at 8:32 am | Permalink

    Inter­est­ing. Thanks for sharing.

  21. Posted June 22, 2009 at 9:36 pm | Permalink

    Wow nice info,…

  22. Posted July 20, 2009 at 1:45 am | Permalink

    This is truly beau­ti­ful. Thank You for that post. Big up.

  23. Posted July 20, 2009 at 1:16 pm | Permalink

    The faulty per­spect­ive bugs me but it’s still amazing!

  24. Posted July 21, 2009 at 1:29 am | Permalink

    Good job with this!

  25. anil
    Posted July 21, 2009 at 3:21 am | Permalink

    not work­ing in FF also IE7

18 Trackbacks

  1. […] Paul Hayes came up with 3D Cube using CSS transformations: […]

  2. […] Hayes has been play­ing with CSS trans­form­a­tions and mak­ing 3D cubes: The impres­sion of a three dimen­sional cube can be cre­ated using mod­ern CSS […]

  3. […] Paul Hayes shows us how to cre­ate three dimen­sional cubes without  javas­cript, Can­vas or SVG. Using straight CSS, he’s designed some pretty slick 3D cubes. […]

  4. […] The rest is here:  3D Cube using CSS transformations […]

  5. […] Hayes has been play­ing with CSS trans­form­a­tions and mak­ing 3D cubes: The impres­sion of a three dimen­sional cube can be cre­ated using mod­ern CSS […]

  6. […] Hace ya más de un año hab­lamos de una serie de prop­ues­tas para CSS3 que el equipo de Web­Kit estaba haciendo sobre las CSS Trans­form­a­tions. A lo largo del año, hemos visto algunas aplic­a­ciones de estas propiedades que ya podemos encon­trar en algunos nave­gadores. La última viene de manos de Paul Hayes, y via Ajaxian, des­cubro que está mont­ando cubos 3D con CSS transformations. […]

  7. By CSS 的新花樣 - 3D 立體 « 林中路 on May 12, 2009 at 9:05 am

    […] ps: 原作者 Paul Hayes 的 demo page. […]

  8. […] 3D Cube Using CSS Transformations […]

  9. By Cubo 3D com CSS » CSS no Lanche on May 14, 2009 at 6:24 am

    […] você quer apren­der a desen­volver um cubo semel­hante a esse, basta dar uma olhada no tutorial que Paul Hayes […]

  10. […] 3D Cube using CSS trans­form­a­tions — FofR Online […]

  11. By Crear cubos 3D con CSS | CSSBlog ES on May 20, 2009 at 10:05 pm

    […] Vía | Fofronline […]

  12. […] Hayes has been play­ing with CSS trans­form­a­tions and mak­ing 3D cubes: The impres­sion of a three dimen­sional cube can be cre­ated using mod­ern CSS […]

  13. […] work­ing on the 3D cube using 2D trans­forms back in April I’ve exper­i­mented with per­spect­ive to cre­ate some­thing more power­ful, playing […]

  14. […] is prob­ably one of the coolest CSS tech­niques I’ve seen. This tutorial shows how to build a 3D cube with text or other con­tent on each side of the cube. It does it […]

  15. By ODC » 50 CSS techniques on July 20, 2009 at 4:09 pm

    […] course I don’t know why you’d want to use the 3d cube trans­form­a­tion other than to simpy show off your amaz­ing CSS skills. I havn’t seen an user inter­face where […]

  16. […] Did you know you can cre­ate 3D Cubes using CSS transformations? […]

  17. […] is prob­ably one of the coolest CSS tech­niques I’ve seen. This tutorial shows how to build a 3D cube with text or other con­tent on each side of the cube. It does it […]

  18. […] is prob­ably one of the coolest CSS tech­niques I’ve seen. This tutorial shows how to build a 3D cube with text or other con­tent on each side of the cube. It does it […]

Post a Comment

Your email is never shared. Required fields are marked *

*
*