Page Content

Tutorials

3D Transformations in CSS

CSS also supports 3D transformations. To correctly view 3D transformations on child elements, the parent element often needs the transform-style property set to preserve-3d. The default value is flat, which renders 3D transformations on a 2D plane.

CSS 3D Transform limitations

  • Computational Demand: 3D object movement needs powerful processing and hardware acceleration.
  • Math complexity: Simple 3D transformations are straightforward, but complex ones may require advanced math skills or specialized equipment to calculate.
  • Layout compatibility: 3D transformations may not perform well with percentage sizes and may need considerable adjustments for responsive layouts.
  • Potential Conflicts: Not all 3D elements can be preserved, such as when their overflow feature is hidden.
  • The 3D Transforms Module was a working draft, with limited hardware/software combinations at the time of source writing. We anticipated modifications.
  • Historical Production Readiness: We deemed 3D transformations unsuitable for client locations in the near future due to their early stage and inadequate support. They were less robust for cross-browser sites; hence, jQuery or other JavaScript solutions for carousels were chosen. However, with increased browser support, they can migrate sophisticated JavaScript effects to CSS stylesheets, demonstrating their promising potential.

3D transform functions

rotateX (angle), rotateY (angle), rotateZ (angle): These functions rotate an element around the X, Y, or Z axis, respectively. The effect of rotateZ() is identical to that of the 2D rotate() function.

Example:

 .trans-x { transform: rotateX(-60deg); }
 .trans-y { transform: rotateY(22.5deg); }
 .trans-z { transform: rotateZ(22.5deg); }

translateZ (value): This function moves an element along the Z axis (forward or backward).

Example:

 .trans-z { transform: translateZ(40px); }

translate3d (x, y, z): This function allows you to move an element simultaneously along the X, Y, and Z axes with a single function.

Example:

.trans-xyz { transform: translate3d(10px,0,-20px); }

The perspective (depth): function establishes the distance between the viewer and the z=0 plane, which is the origin of the element, in a 3D transformation. The parent element’s perspective property, which applies perspective to its children, can achieve a similar effect.

    CSS 3D transformations let you alter elements in 3D. This feature adds the z-axis to 2D transformations. To display items in genuine 3D, a browser with hardware acceleration for graphics is necessary, requiring a significant amount of computing power.

    CSS 3D transformations Browser Support

    • Over time, browser support for CSS properties, including transformations, evolves. Many older publications address browser support based on the time they were written.
    • Early Support:The 3D Transforms Module was initially suggested by the WebKit team and supported by Safari and Chrome. When one source was created, Safari 4.03+ on Mac OS X 10.6 and iPhone OS 2+ had hardware acceleration and required the -webkit- prefix for properties.
    • Support for 3D transforms was added to Firefox 10+ but not IE until version 10,. Older tables show WebKit with prefix support but no support in Firefox, Opera, or IE, occasionally indicating predicted support in later versions like IE9 or Firefox 4 for related features like transitions. Their WebKit roots made them widely supported on Android (v3 onwards) and iOS (all versions) when one source was published.
    • Vendor Prefixes: Browser support for new CSS3 capabilities may need vendor prefixes such as -webkit- (Chrome, Safari), -moz- (Firefox), -ms- (Internet Explorer, Edge), and -o- (Opera). commonly utilise -webkit- for 3D features like perspective and transform-style. You can generate these prefixes using Autoprefixer.
    • Detect features using the @supports at-rule to account for browser support variances. This lets you apply CSS rules only if the browser can handle a given property and value combination, such as @supports (transform: translateZ(1px)) and (transform-style: preserve-3d) and (perspective: 1px) to identify 3D capabilities
    • Modernizr: JavaScript library detects browser functionalities, including 3D transformations (csstransforms3d). By adding classes to the element (e.g.,.csstransforms3d .no-csstransforms3d), you may give fallback styles or hide content for non-supporting browsers.
    • Compared to other CSS3 features, 3D transformations were less supported than CSS3 rules like text shadows, gradients, rounded borders, and multiple background images.


    Set the transform-style attribute to preserve-3d on a parent element to place its children in 3D space rather than flattening them into a 2D plane.


    3D transformations have great potential, but their implementation may be complicated, needs validating browser support on caniuse.com, and may require cautious production use, especially for intricate effects or broad browser compatibility.

    CSS3 Topics

    Index