CSS Recursive Design

My goal is to design a responsive layout using the Golden Ratio (1:1.618) that can infinitely nest within itself.

I understand that it may not be practical to have layers upon layers of nesting, but I am fixated on the idea of achieving infinite recursion!

Take a look at the code snippet below:

* { box-sizing: border-box; }
.top {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

/**
*
* GOLDEN RATIO LAYOUT
*
**/

/**
* By default, the layout flows from left to right and top to bottom.
* To implement the Golden Ratio Layout, create a container.
* Inside that container, have only two children.
* The first child should have the class .large-side,
* and the second child should have the class .small-side.
* Consider nesting layouts within the small side by adding the
* classes .small-side and .golden-ratio-layout to elements within.
**/

.golden-ratio-layout {
    display: flex;
    flex-wrap: nowrap;
    flex-direction: column;
}
.golden-ratio-layout > .large-side {
    flex: 1.618 1 0%;
}
.golden-ratio-layout > .small-side {
    flex: 1 1 0%;
}
/* Rest of the CSS styles remain unchanged */

Seems pretty neat so far, doesn't it?

In reality, you may never need more than five nested panels, but let's explore what happens when we introduce a sixth panel:

* { box-sizing: border-box; }
.top {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

/**
*
* GOLDEN RATIO LAYOUT
*
**/

/**
* Default layout is left to right, top to bottom
* To use the Golden Ratio Layout, create a container.
* Within that container, have only two childen.
* The first child should have the class .large-side,
* and the second child should have the class .small-side .
* I suggest nesting layouts on the small side, where the
* element would have .small-side and .golden-ratio-layout .
**/

/* Same CSS styles as before */

The attempt failed!

Let's inspect the last .golden-ratio-layout.

We could target this element with:

.golden-ratio-layout > .golden-ratio-layout > .golden-ratio-layout > .golden-ratio-layout > .golden-ratio-layout {
    flex-direction: {{ whatever }};
}

Although this solution fixes the issue, it demands adding rules for each depth level we want to support.

So, is it feasible to accommodate an infinite number of nested levels in these rules? Is there a way?

Answer №1

There are two main issues that need to be addressed.

The first issue lies within the HTML structure. In the functioning example, there is a consistent pattern of golden-ratio-layout -> panel-wrapper -> golden-ratio-panel. However, in the broken example, the golden-ratio-layout is missing at the last level. Adding this element resolves the glitchy behavior, although it does not completely fix the problem.

The second issue arises when you continue adding boxes beyond the fourth level. This causes the spiral effect you desire to break down as boxes keep stacking on top of each other. By adding colors to the background, you can visualize how the rules are repeated only up until the fourth level, rather than beginning from the first as intended.

To remedy this situation, I had to implement a workaround by introducing a reset class and applying it to every fourth div starting from the first. Additionally, I made modifications to the CSS rules that affect the flex-direction, ensuring that the top-most class in the rule was the reset class. If possible, I would have utilized a pseudo-selector instead of a separate reset class.

* { box-sizing: border-box; }
.top {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

.golden-ratio-layout {
    display: flex;
    flex-wrap: nowrap;
    flex-direction: column;
}
.golden-ratio-layout > .large-side {
    flex: 1.618 1 0%;
}
.golden-ratio-layout > .small-side {
    flex: 1 1 0%;
}
.golden-ratio-layout .panel-wrapper {
    padding: 0;
     box-shadow: 1px 1px 10px 2px #024;
}
.golden-ratio-layout .golden-ratio-panel {
    width: 100%;
    height: 100%;
    border: 1px solid black;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Relevant changes past this point */
/* Standard flow */
.golden-ratio-layout.reset {
    flex-direction: column;
    background: #800;
    color: white;
   
}
.golden-ratio-layout.reset  > .golden-ratio-layout {
    flex-direction: row;
    background: #601;
}
.golden-ratio-layout.reset  > .golden-ratio-layout > .golden-ratio-layout {
    flex-direction: column-reverse;
    background: #402;
}
.golden-ratio-layout.reset  > .golden-ratio-layout > .golden-ratio-layout > .golden-ratio-layout {
    flex-direction: row-reverse;
    background: #203;
}

@media screen and (orientation:landscape) {
    .golden-ratio-layout {
        flex-direction: row;
    }

    /* Standard flow */
    .golden-ratio-layout.reset  {
        flex-direction: row;
    }
    .golden-ratio-layout.reset  > .golden-ratio-layout {
        flex-direction: column;
    }
    .golden-ratio-layout.reset  > .golden-ratio-layout > .golden-ratio-layout {
        flex-direction: row-reve...
<div class="top golden-ratio-layout reset">
  <div class="large-side panel-wrapper">
    <div class="golden-ratio-panel">1</div>
  </div>
  <div class="small-side golden-ratio-layout">
    <div class="large-side panel-wrapper">
      <div class="golden-ratio-panel">2</div>
    </div>
    <div class="small-side golden-ratio-layout">
      <div class="large-side panel-wrapper">
        <div class="golden-ratio-panel">3</div>
      </div>
      <div class="small-side golden-ratio-layout">
        <div class="large-side panel-wrapper">
          <div class="golden-ratio-panel">4</div>
        </div>
        <div class="small-side golden-ratio-layout reset">
          <div class="large-side panel-wrapper">
            <div class="golden-ratio-panel">5</div>
          </div>
          <div class="small-side golden-ratio-layout">
            <div class="large-side panel-wrapper">
              <div class="golden-ratio-panel">6</div>
            </div>
            <div class="small-side golden-ratio-layout reset">
              <div class="large-side panel-wrapper">
                <div class="golden-ratio-panel">7</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose an item that does not have any offspring

I am facing an issue with a webpage that contains either of the following: <span id='size'>33</span> Or <span id='size'> <b>33</b> <strike>32</strike> </span> I need to extract ...

Remove vertical spacing from rows of thumbnail grid in bootstrap

I have been attempting to eliminate the vertical space between rows in a thumbnail grid, similar to how I did for horizontal spacing, but have not been successful. .gutter-0.row { margin-right: 0; margin-left: 0; /* not working */ margin-t ...

The appearance of HTML varies depending on the type of mobile device being used

My email template is having display variations on different mobile devices, with the textbox sometimes centered instead of left aligned as intended. Any suggestions on what might be causing this issue and how to resolve it? Desired Display on All Devices ...

Adding a PDF generated from an HTML div to an email using Java

I am looking for a solution to convert the contents of an HTML div tag into a PDF file while preserving its associated CSS. This is essential as I will be using Java on the back-end to send emails, and I need to attach the PDF with the CSS intact when send ...

Adding an active class to a large image when a thumbnail image is clicked can enhance user experience and

I've created a photo gallery using jquery. Currently, when I click on "next", the image changes based on the index. However, I also want to be able to click on the thumbnails (small images) and display the larger image according to that specific inde ...

Tips for designing a horseshoe-inspired meter using CSS

  I want to create a horseshoe-shaped gauge using CSS that resembles the image below:     My attempt involved creating a circle and cutting off the bottom, similar to the technique demonstrated in this fiddle: http://jsfiddle.net/Fz3Ln/12/   Here& ...

Is there a way to retrieve the href attribute from an <a> tag using PHP?

Similar Question: Retrieving the href attribute of an Anchor Element Provided hyperlink $link="<a href='test.php'>test</a>"; Is there a way to extract href and anchor text using PHP? ...

Generate a .row.cells row for every <tr> element's length

Greetings, I am interested in creating a script that can dynamically generate code based on the number of table rows it finds. Let me explain with an example: Let's say I have a piece of code that calculates the number of table rows (excluding the ta ...

Showing information to several classes using JavaScript

I am currently developing a "Gamebook Engine" that enables users to set a custom username. The user name is extracted from an input element with the id="setUserNameInput" and stored using the function setUserName(). It is then displayed or loaded into an e ...

Issue with Google Tag Manager where the class name is ending with '-ok' is causing problems

Utilizing Google Tag Manager for monitoring clicks within my search engine, which showcases various book details such as cover images, titles, and authors. When a book is in stock, it displays a checkmark and "Leverbaar" (In stock) below the price tag ($1 ...

Angular: Maximizing Input and Output

I'm having trouble with the function displaying within the input field. My goal is to simply allow the user to enter a name and have it displayed back to them. HTML: <div ng-app = "mainApp" ng-controller = "studentController"> <tr> < ...

What issues are present in this PHP code?

Can you identify the issue in this PHP code? I am using Netbeans 8.1 and whenever I type $ _GET, an error occurs <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> ...

Using a Laravel foreach loop to display data with two different properties

I am seeking assistance with my foreach loop in the view. Controller.php $expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id'); $categories = C ...

"To enhance the functionality of your website in Chrome, incorporate Bootstrap affix and

When I add a website name as a prefix, the name is initially hidden. As the user scrolls down, the website name becomes visible, and as they scroll back up, it disappears again. I have implemented this feature using jQuery. $(document).ready(function(){ ...

Switching the class does not initiate the transition

I'm having trouble getting a transition to work when toggling a class. The class is being applied and the style changes accordingly, but the transition isn't triggering. I've checked that the transition applies to all properties, so I don&ap ...

When using Vue.js, binding may not function properly if it is updated using jQuery

Link to JsFiddle Below is the HTML code: <div id="testVue"> <input id="test" v-model="testModel"/> <button @click="clickMe()">Click me</button> <button @click="showValue()">Show value</button> </div& ...

What is the best way to horizontally align three animated progress bars alongside images?

Check out this jsfiddle that shows a vertical alignment of the progress bars. How can I modify it to align them horizontally and centered? https://jsfiddle.net/bLe0jLar/ .wrapper { width: 100px; height: 100px; } .wrapper > #bar, #bar2, #bar3 { ...

"Troubleshooting issue in Django 1.6.2 where CSS styles are not being applied to

I am encountering an issue with this code and would greatly appreciate some assistance. The base.html file contains a {% block content %}{% endblock %}. I have created a Signup.html file structured like this: {%extends 'base.html'%} {% block co ...

The nightmare of Parent-Child Inheritance in JQuery/CSS is a constant struggle

Having difficulty implementing specific JQuery effects into a Bootstrap framework due to its extensive CSS causing issues. Created a test file named testjquery.html connected to a stylesheet defining a hidden element and activating the fade in of this ele ...

Attempting to make a gallery image expand when clicked on

Having trouble with image expansion in a gallery when clicking on different images. Currently, the expanding feature only works on the first image in the set. If I click on another image, the first one is the one that expands. <div ng-repeat="album ...