I am currently building a book section for this site and thought it would be cool to show the books in 3D and also to make it visible how many pages a book has. In this article I would like to show you how to use CSS custom properties to adapt the depth of a 3D book showing how many pages the book has.
3D Book in CSS #
There are many resources and code examples how to build a 3D Book in CSS. As a starting point I used this.
Here is the HTML for creating the book:
<img class="book__cover" src="/books/world-wide-waste.jpg" alt="World Wide Waste: How Digital Is Killing Our Planet-and What We Can Do About It by Gerry McGovern "/>
And here is the CSS:
transition: transform 1s ease;
box-shadow: 5px 5px 20px #666;
content: ' ';
height: calc(300px - 2 * 3px);
transform: translateX(calc(200px - 50px / 2 - 3px)) rotateY(90deg) translateX(calc(50px / 2))
content: ' ';
box-shadow: -10px 0 50px 10px #666;
Customize via CSS custom properties #
This looks already really great, so now enhance this and change the depth of the book based on the pages of the book.
First let's define a range – I decided to increase the depth every 50 pages, starting from 50 pages and ending at 1000 pages.
--page-count-range: clamp(1, calc(var(--page-count) / 50), 20);
--page-width: calc(10px * var(--page-count-range));
/* only showing the updated stuff here */
transform: translateX(calc(200px - var(--page-width) / 2 - 3px)) rotateY(90deg) translateX(calc(calc(var(--page-width)) / 2))
transform: translateZ(calc(var(--page-width) * -1));
Let's go through this.
First, we set three CSS custom properties whereas the first one defines the actual page count.
In the second one it is getting interesting. There we use clamp() to define our minimum as 1 (50 pages or fewer), our preferred value (page count divided by 50) and a maximum of 20 (1000 pages or more).
In the last property we multiply the range by 10px, so we can use that in our CSS for the transform and width properties that we need to adapt.
If you open the demo you can change the --page-count property to see how the depth changes. And that's it. With some small changes, the depth of the book is now reactive. CSS: What a wonderful language!
That was a fun challenge and can't wait to share my book section where you will see this in action.
Create an animated 3D book in CSS, step by step