iTranslated by AI
Implementing an Omikuji with Only CSS
This is the Useless CSS for Practical Work series.
I originally wanted to post this article at the beginning of the year, but I'll probably be sleeping through the New Year holidays, so I wrote it ahead of time.
It's a bit early for a joke article for the New Year, but think of it as getting ahead of the season.
I hope this serves as a reference for anyone who wants to create an Omikuji (fortune slip) using only CSS.
Final Result
First, here is the final result.
You can see that you can draw fortunes ranging from "Great Curse" (Daikyo) to "Great Blessing" (Daikichi).
I also added a redraw feature, so please keep drawing until you get "Great Blessing" (though that's not exactly how it's supposed to work).
Below, I will explain the code by excerpting only the important parts.
Please refer to CodePen if you want to check the entire code.
Implementation
First, let's start with the visual coding.
3D Representation
I want to create a box with depth, so I will use the transform-style property.
I'll leave the detailed explanation to MDN.
This property might be unfamiliar to those who don't often do 3D representations in CSS.
By specifying transform-style: preserve-3d on the parent element, you can arrange child elements in a 3D space.
Creating a Cube
It's a bit of a detour, but let's try making a cube with CSS.
.box
- for(let i = 0; i < 6; i++)
.box__surface
.box {
position: relative;
width: 200px;
height: 200px;
transform-style: preserve-3d;
background-color: rgba(100, 0, 0, 0.3);
&__surface {
position: absolute;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
border: 5px solid lighten(rgba(0, 0, 100, 0.3), 50%);
background-color: rgba(0, 0, 100, 0.3);
&:nth-child(1) {
top: -100%;
transform-origin: center bottom;
transform: translateZ(100px) rotateX(90deg);
}
&:nth-child(2) {
left: -100%;
transform-origin: right center;
transform: translateZ(100px) rotateY(-90deg);
}
&:nth-child(3) {
transform: translateZ(100px);
}
&:nth-child(4) {
transform: translateZ(-100px);
}
&:nth-child(5) {
left: 100%;
transform-origin: left center;
transform: translateZ(100px) rotateY(90deg);
}
&:nth-child(6) {
top: 100%;
transform-origin: center top;
transform: translateZ(100px) rotateX(-90deg);
}
}
}
I have specified transform-style: preserve-3d for the parent element .box (the red one).
By shifting the positions of the child elements .box__surface relative to the center square of .box (the red one), I was able to create a cube.
With the code above, we can confirm that 3D representation is possible with CSS alone.
Making the Omikuji Box
By now, you should understand how to create a cube using CSS.
Creating the Omikuji box is almost the same process.
While we created all six sides for the cube, in reality, only up to three sides are visible from a single direction at once. So, this time, I will only prepare three sides.
Omikuji Functionality
Now, let's move on to the implementation of the main Omikuji functionality.
Branching Logic
In this case, we need to branch between multiple states ranging from "Great Curse" (Daikyo) to "Great Blessing" (Daikichi), so let's use radio buttons.
Place the radio buttons directly under the body, as siblings of .wrapper.
Prepare as many radio buttons as there are types of fortunes, and branch the processing based on which button is :checked.
//- Place radio buttons as siblings of .wrapper
- for(let i = 0; i < 7; i++)
input(type="radio" id="num-" + i name="omikuji")
.wrapper
.box
.box__surface.__top
span.hole
span.omikuji
.box__surface.__side
.box__surface.__side
span Omikuji
.btn
.labels
//- Set :checked on radio buttons using label elements
- for(let i = 0; i < 7; i++)
label.label(for="num-"+i) Draw
// Display the fortune when :checked is applied
input:checked ~ .wrapper .omikuji {
transform: rotateX(-90deg) translateY(-50%);
transition: all 0.2s ease-out;
}
// Repeat for each type of fortune
input[id="num-1"]:checked ~ .wrapper .omikuji {
&::before {
content: "Great Curse";
}
}
input[id="num-2"]:checked ~ .wrapper .omikuji {
&::before {
content: "Curse";
}
}
// Loop continues...
Implementing Randomness
The most troublesome part of this article is likely the implementation of randomness.
I'll state upfront that it's not possible to implement complete randomness; rather, it's just something that looks random.
How is this "randomness" implemented in CSS? It's achieved by lining up labels horizontally and sliding them at high speed.
In the code above, the parts overflowing from the button are visualized for clarity, but in reality, they are hidden using overflow: hidden (so only the "↓ Display Area" part is shown). By doing this, it becomes possible to make it look like the same button is being clicked while actually clicking a different one.
Redraw Functionality
Finally, we'll implement the redraw functionality. We'll add one more radio button (this time #num-7) and implement it such that it resets when it's :checked.
When I say "reset," you might imagine overwriting, but it simply means putting it back into the same state as when no input has :checked applied.
Using :not is likely the easiest way to implement this. In other words, the fortune slip will only be in a "drawn" state when something other than #num-7 is :checked.
// When :checked is applied to anything other than #num-7, draw the fortune
// → When :checked is applied to #num-7, it is reset
input:not(#num-7):checked ~ .wrapper .btn {
.label {
// Stop the animation and display #num-7
animation: none;
transform: translateX(-700px);
}
}
input:checked:not(#num-7) ~ .wrapper .omikuji {
// Fortune drawing process
}
If you include the above features, it's complete!
Final Code
The content is the same as the CodePen at the beginning, but I'll provide the entire code again here.
Final Code
- for(let i = 0; i < 8; i++)
input(type="radio" id="num-" + i name="omikuji")
.wrapper
.box
.box__surface.__top
span.hole
span.omikuji
.box__surface.__side
.box__surface.__side
span Omikuji
.btn
.labels
- for(let i = 0; i < 7; i++)
label.label(for="num-"+i) Draw
label.label(for="num-7") Redraw
* {
box-sizing: border-box;
}
input[name="omikuji"] {
position: absolute;
visibility: hidden;
}
.wrapper {
height: 600px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: whitesmoke;
}
.box {
position: relative;
width: 200px;
height: 300px;
transform: rotateX(-30deg) rotateY(30deg);
transform-style: preserve-3d;
&__surface {
position: absolute;
border: 5px solid darken(darkorange, 20%);
background-color: darkorange;
width: 100%;
&.__top {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
transform-style: preserve-3d;
transform-origin: center bottom;
transform: translateZ(100px) translateY(-100%) rotateX(90deg);
}
&.__side {
width: 100%;
height: 100%;
&:nth-child(2) {
left: -100%;
transform-origin: center right;
transform: translateZ(100px) rotateY(-90deg);
}
&:nth-child(3) {
display: flex;
justify-content: center;
align-items: center;
font-size: 50px;
font-weight: 600;
writing-mode: vertical-rl;
transform: translateZ(100px);
}
}
}
}
.hole {
display: inline-block;
width: 60px;
height: 60px;
background-color: #000;
border-radius: 50%;
}
.omikuji {
position: absolute;
top: 0;
bottom: 0;
display: inline-block;
width: 30px;
margin: auto 0;
height: 50%;
padding-top: 8px;
background: #fff;
font-size: 20px;
font-weight: 600;
text-align: center;
transform: rotateX(-90deg) translateY(51%);
box-shadow: 0px 0px 3px 0px rgb(0 0 0 / 20%);
}
.btn {
position: relative;
margin-top: 100px;
width: 100px;
height: 50px;
overflow-x: hidden;
}
.labels {
position: absolute;
top: 0;
left: 0;
display: flex;
width: 800%;
height: 100%;
animation: anime 0.7s steps(7) infinite;
z-index: 5;
&:active {
animation-play-state: paused;
}
&:hover {
.label {
background-color: lighten(dodgerblue, 5%);
}
}
}
.label {
display: flex;
justify-content: center;
align-items: center;
width: 98px;
height: calc(100% - 3px);
margin: 0 1px;
border-radius: 5px;
background-color: dodgerblue;
box-shadow: 0 3px 0 darken(dodgerblue, 40%);
color: lighten(dodgerblue, 40%);
font-weight: 600;
cursor: pointer;
}
input:checked:not(#num-7) ~ .wrapper .btn {
.labels {
animation: none;
transform: translate(-700px, 0);
}
}
input:checked:not(#num-7) ~ .wrapper .omikuji {
transform: rotateX(-90deg) translateY(-50%);
transition: all 0.2s ease-out;
}
$omikuji: "Great Curse", "Curse", "Blessing", "Small Blessing", "Future Blessing", "Half Blessing", "Great Blessing";
@each $o in $omikuji {
$i: index($omikuji, $o);
input[id="num-#{$i - 1}"]:checked ~ .wrapper .omikuji {
&::before {
content: "#{$o}";
}
}
}
@keyframes anime {
to {
transform: translate(-700px, 0);
}
}
Summary
That was how to implement an Omikuji functionality using only CSS!
I referred to various blogs, articles, and CodePens for the method of implementing randomness using only CSS. I am grateful for the wisdom of my predecessors.
While the content of this article is highly likely to be useless in practice, I hope you all enjoy experimenting with CSS.
References
Discussion