Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1111

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T05:06:07+00:00 2024-11-25T05:06:07+00:00

The Golden Ratio in CSS

  • 62k

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:

cssshowdevtutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.