I'm Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with HTML, CSS and SVG. To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

my books

Pro CSS3 Animation book coverPro CSS3 Animation, Apress, 2013

my other blogs

Massive Head CanonMassive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

The New DefaultsThe New Defaults — A Sass color keyword system for designers.

CSSslidyCSSslidy — an auto-generated #RWD image slider. 3.8K of JS, no JQuery.

More Tricks and Tips For CSS 3D: Smoothing Transforms & Fixing Floated Elements

Previously I’ve talked about common errors made in developing CSS 3D content. Today’s article will discuss another one, plus a bonus technique.

Previously I’ve talked about common errors made in developing CSS 3D content. Today’s article will discuss another one, plus a bonus technique.

Smoothing The Edges of Transformed Elements

Rotating elements (especially images) in three dimensions using CSS can result in jagged edges that lack antialiasing, depending on the browser build and GPU. Right now, Firefox is particularly affected by this.

The CSS, shown without vendor prefixes to save space:

.church { perspective: 700px; padding: 2rem; }
.church img { transform: rotateY(12deg); }
Rotated element without smoothing applied, as viewed in Firefox

While I expect this visual treatment to improve in the near future, right now there is a simple fix: adding a 1 pixel thick transparent outline:

.church img { transform: rotateY(12deg); outline: 1px solid transparent; }

The live result looks like this:

Rotated element with smoothing applied

Collapsed Containers Affect 3D Perspective

It’s a behaviour that’s confused me more than once: perspective doesn’t appear to be applied to images correctly, despite the fact that I’ve triple-checked the code. The issue is due to what I’ve called the “float flag” quirk: floated elements do not contribute to the height of their containers. (The same issue occurs with elements given position: fixed or absolute). As a result, the perspective vanishing point shifts.

For example, a simple image inside a div, with CSS 3D applied, but with the image floated. I’ve added a border to the div to show its area and shape:

A floated 3D element

<div class=taj>
<img src=taj-mahal-full.jpg alt="A photograph the Taj Mahal at sunset"  style=width:300px>
</div>

The CSS:

.taj { perspective: 700px; border: 1px solid red; }
.taj img { transform: rotateY(12deg); float: left; outline: 1px solid transparent; }

With a floated element, the 3D vanishing point is “buried” in the collapsed div, changing the orientation of the image. Depending on circumstances, this shift can be subtle or bizarre. The solution, however, is always the same: ensure that the container reflects the actual size of the 3D elements inside it. There are a number of ways you could do this: adding a clearfix solution, overflow: hidden, extra padding, or some combination of the three.

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.