The Golden Ratio in CSS
Mads Stoumann

Mads Stoumann @madsstoumann

About: I'm a tech director, web developer, graphic designer, musician, blogger, comicbook-geek, LEGO-collector, food lover … as well as husband and father!

Location:
Copenhagen, Denmark
Joined:
Nov 16, 2020

The Golden Ratio in CSS

Publish Date: Sep 25 '24
243 47

The golden ratio, also called the golden number, golden proportion, or even the divine proportion, is a special relationship between two numbers that equals approximately 1.618. It’s often symbolized by the Greek letter "phi." What’s fascinating is how closely this ratio is tied to the Fibonacci sequence—a series of numbers where each number is the sum of the two before it. The Fibonacci sequence starts with 0, 1, and then continues: 1, 2, 3, 5, 8, 13, 21, and so on. As you move further along in the sequence, the ratio between each number and the one before it gets closer and closer to 1.618, or phi. This unique ratio shows up in nature, art, and architecture, making it both mathematically intriguing and visually pleasing!

In this tutorial, we'll re-create the Golden Ratio Diagram in CSS, using a few grid-declarations and some additional tricks.

Let's get started!


We'll use this part of the Fibonacci-sequence:

1, 1, 2, 3, 5, 8, 13, 21
Enter fullscreen mode Exit fullscreen mode

That’s 8 digits, so our markup will consist of an <ol> with 8 <li> elements.

In CSS grid, we'll create a canvas with the dimensions derived from the sum of the last two digits, 13 + 21 = 34, for the width, and the largest digit, 21, for the height:

ol {
  all: unset;
  display: grid;
  grid-template-columns: repeat(34, 1fr);
  grid-template-rows: repeat(21, 1fr);
  list-style: none;
}
Enter fullscreen mode Exit fullscreen mode

Not much to see yet, but if we enable Dev Tool´s Grid Inspector, we can see the grid:

Grid inspector

Next, we'll add some common styles for the <li>-elements:

  li {
    aspect-ratio: 1 / 1;
    background: var(--bg);
    grid-area: var(--ga);
}
Enter fullscreen mode Exit fullscreen mode

Still nothing to see, so let's fill out the --bg (background-color) and --ga (grid-area) variables with some content:

&:nth-of-type(1) {
  --bg: #e47a2c;
  --ga: 1 / 1 / 22 / 22;
}
&:nth-of-type(2) {
  --bg: #baccc0 ;
  --ga: 1 / 22 / 23 / 35;
}
&:nth-of-type(3) {
  --bg: #6c958f;
  --ga: 14 / 27 / 22 / 35;
}
&:nth-of-type(4) {
  --bg: #40363f;
  --ga: 17 / 22 / 22 / 27;
}
&:nth-of-type(5) {
  --bg: #d7a26c;
  --ga: 14 / 22 / 17 / 25;
}
&:nth-of-type(6) {
  --bg: #ae4935;
  --ga: 14 / 25 / 17 / 27;
}
&:nth-of-type(7) {
  --bg: #e47a2c;
  --ga: 16 / 26 / 17 / 27;
}
&:nth-of-type(8) {
  --bg: #f7e6d4;
  --ga: 16 / 25 / 17 / 26;
}
Enter fullscreen mode Exit fullscreen mode

And now, we get this:

Fibonacci

Cool! So what happened? We gave each <li> it's own background-color using the --bg custom property. Then we used grid-area to place and size the rectangles within the grid. grid-area is a shorthand for:

row-start / col-start / row-end / col-end
Enter fullscreen mode Exit fullscreen mode

Now, how about creating the spiral effect? While CSS can’t directly draw spirals, we can fake it by adding circles as ::after pseudo-elements on each <li>:

li {
  /* as before */
  overflow: hidden;
  position: relative;

  &::after {
    aspect-ratio: 1 / 1;
    background-color: rgba(255, 255, 255, .3);
    border-radius: 50%;
    content: '';
    display: block;
    inset: 0;
    position: absolute;
  }
}
Enter fullscreen mode Exit fullscreen mode

This gives us:

Circles

Not quite there yet, but if we double the size of the circles and translate them slightly, it starts looking better:

&::after {
  /* as before */
  scale: 2;
  translate: var(--tl);
}
Enter fullscreen mode Exit fullscreen mode

However, not much changes until we update the --tl (translate) property for each <li>:

&:nth-of-type(1) {
  --tl: 50% 50%;
}
&:nth-of-type(2) {
  --tl: -50% 50%;
}
/*  and so on for the rest */
Enter fullscreen mode Exit fullscreen mode

Now we get this:

Final

What happened here?

We created a circle double the size of its element, then used translate to shift the circle in different directions to give the appearance of a continuous spiral. By adjusting the translation for each element, the circles "flow" into one another, mimicking a spiral.

Finally, because we added overflow: hidden to the <li> elements, the circles are "cut off" when they overflow their containers, giving us the illusion of a perfect spiral!

Here’s the finished result in a CodePen:

Comments 47 total

  • Mannuel
    MannuelSep 25, 2024

    very cool

  • Ed
    EdSep 25, 2024

    Noobs draw art on excel.
    Pros draw art with CSS haha
    Fascinating stuff you got there

    • Mads Stoumann
      Mads StoumannSep 26, 2024

      Thank you! I’m just a former graphic designer who see grids everywhere 😂

      • Peter Bismuti
        Peter BismutiSep 28, 2024

        There is a mathematician on YouTube who shows his process of using math to create images and it is a great example of how mathematicians can make great artists because they have so many tools at their disposal.

  • Best Codes
    Best CodesSep 26, 2024

    Love it! I like the Fibonacci sequence as well.

  • Mads Stoumann
    Mads StoumannSep 26, 2024

    This is without overflow:hidden — kinda beautiful!
    Without overflow

    • Ezpie
      EzpieSep 27, 2024

      Won't lie, this is better without over:hidden! This is kind of those things that look better by testing. Just like art in my opinion, you never know what would get cooked.

  • Drazen Bebic
    Drazen BebicSep 26, 2024

    That is a very creative project, Love it!

    This got me thinking: If you can find a way to maybe randomize patterns, streaks, colors, etc., you could programmatically create abstract art. Could also be using the HTML canvas tag instead of CSS. Not sure if something like this already exists?

  • Nathan Stevens
    Nathan StevensSep 26, 2024

    Now THAT is interesting. I want more of this!
    There's way too many listicles on this site.

    Every day there's a dozen posts about top 20 tools to bring back your loved ones from the dead or something at the top of the lists.

    Same rehashed stuff.

    This is what I want more of.
    And if there's a better place for things like this, please recommend. :)

    • Mads Stoumann
      Mads StoumannSep 26, 2024

      Thank you! You can take a look at some of my earlier posts — recently, I wrote about the solar system and the periodic table in CSS grid.

  • Martin Baun
    Martin BaunSep 26, 2024

    Brilliant move, respect!

  • Anna Villarreal
    Anna VillarrealSep 26, 2024

    I absolutely love this. Super clear, easy to follow, and it's just interesting. Thanks!

  • Aditya Mhambrey
    Aditya MhambreySep 26, 2024

    As someone starting out with web design, this article opened my eyes to a new world of possibilities. Bravo!

  • Mince
    MinceSep 27, 2024

    Awesome, 👍

  • Fiji
    FijiSep 27, 2024

    Great post! I’m also impressed by how well your colours are matching, did you use some online resource for palette generation or something?

    • Mads Stoumann
      Mads StoumannSep 27, 2024

      I “stole” the colors from an old Bauhaus poster! 🫢

      • Michael
        MichaelNov 14, 2024

        Hey, if it helps to enrich other people's lives, I think taking a little artistic license is, or at least should be, allowed. And with more than a few people commenting and complimenting on the colors alone, including all the other ones, I would say that you certainly did enrich many lives, mine included! Thx again!
        (commented earlier, what is even cooler...)

        MichaelTheGamer

  • Gregory Edgerton
    Gregory EdgertonSep 28, 2024

    Beautiful implementation! Working on a golden ratio project where i've been hung up on a li + :nth-child approach with relative widths while still leverage CSS grid. I couldn't see through the haze of my ideas.

    Picked the project back up two days ago after an inspiring meet-up poised to dedicate an hour a day. Had yet to look what was out there though, finally waved the white flag in search of others. I find myself inspired again.

    Thank you for sharing.

    • Mads Stoumann
      Mads StoumannSep 28, 2024

      Thanks, happy to hear that! grid-area is the key here, at least for me, who like to “think in grids”.

  • Nestor Diaz Valencia
    Nestor Diaz ValenciaSep 28, 2024

    Beautiful

  • Rachel Wallis
    Rachel WallisSep 28, 2024

    This is so cool, love it!

  • Martin Dimmock
    Martin DimmockSep 28, 2024

    Got to agree, this is great; maths + css! Colour me impressed.

  • Ricardo Esteves
    Ricardo EstevesSep 28, 2024

    Sooo cool this post/articles @madsstoumann. Was really nice to see your approach and how did you progress to the desired outcome!
    There are so many interesting topics to experiment with, keep them coming!

    • Mads Stoumann
      Mads StoumannSep 28, 2024

      Thank you so much — I have a bunch of geometric art generators coming up!

  • Ali Raza
    Ali RazaSep 29, 2024

    great

  • Code_Jedi
    Code_JediSep 29, 2024

    Peak CSS creativity. Nice!

  • Michael
    MichaelOct 12, 2024

    What is even cooler is that the Golden Ratio Nautilus Effect (look up nautilus images to see what I mean), is that it is also seen in the human hand! Seriously, try it starting with the tip of your finger (usually index or middle work fine) and then measuring it to the next joint, and using a tape measure or even a ruler, measuring from fingertip to next joint then the previous joint to the next, to hand (well, wrist) and it goes to the shoulder. Think about it: another proof of order in what we are always told is random chaos. It's too much order. We are not random! Neither are we alone! Seek and you shall find!

Add comment