Notes · 3D CSS · Animation · Interface
A 3D bookshelf made with CSS only
Books that turn on their spine to show the cover, with no WebGL or libraries: transform-style, rotateY and a width that changes with the book.
The characters page is a bookshelf: each character is a book. I wanted only the chosen book to show its cover and the rest to show their spine, like on a real shelf.
A book is a box
Each book is a container with transform-style: preserve-3d and six faces: front, back, spine, edges and the inner page. The spine is a panel turned -90° on the left edge.
Turning on the spine
The trick is the transform origin. If the book turns around its left edge (transform-origin: 0 50%), a rotateY(90deg) leaves the spine facing you and the cover edge-on. The active book goes back to 0°.
.shelf-book { width: var(--bd); --ry: 90deg; transition: width .9s; }
.shelf-book.is-active { width: var(--bw); --ry: 0deg; }
.shelf-book__body {
transform-origin: 0 50%;
transform: rotateY(var(--ry));
}
The shelf makes room
Each book’s button is as wide as what shows: the spine’s thickness or the cover’s width. Animating that width together with the turn makes the other books move aside on their own with flexbox.
- Arrow keys change book.
- With reduced motion, books change without turning.
- On a phone the decorative spines hide so every character fits.