Creating the worth of CSS content through a LESS loop

We've implemented a CSS style that incorporates dots both before and after a heading to signify the nesting level of the heading:

.heading.nesting-1:before,.heading.nesting-1:after{content:"\00B7"}
.heading.nesting-2:before,.heading.nesting-2:after{content:"\00B7\00B7"}
.heading.nesting-3:before,.heading.nesting-3:after{content:"\00B7\00B7\00B7"}
...

Although I attempted using LESS for concatenating strings, the results were not as expected.

@entity-dot: '\00B7';
@nesting-levels: 9;

.heading {
   .nesting-loop (@i) when (@i <= @nesting-levels) {
      &.nesting-@{i} {
         &:before, &:after  {
            .nested-dots(@j, @current: "") when (@j > 0) {
               @nested-dots: "@{current}@{entity-dot}";
               .nested-dots((@j - 1), @nested-dots);
            }
            .nested-dots(@j: @i, "");
            content: "@{nested-dots}";
         }
      }
      .nesting-loop(@i + 1);
   }
   .nesting-loop (1);
}

The output now appears as follows:

.heading.nesting-1:before,.heading.nesting-1:after{content:"\00B7"}
.heading.nesting-2:before,.heading.nesting-2:after{content:"\00B7"}
.heading.nesting-3:before,.heading.nesting-3:after{content:&q
t;\00B7"}
...

It seems like the issue stems from the inability to iteratively update the @nested-dots property in this manner. Any suggestions for a solution would be greatly appreciated!

Thank you for your assistance.

Answer №1

After finding inspiration in a helpful post by Martin Turjak on Stack Overflow, I managed to resolve the issue.


@nesting-levels: 5;
@entity-space: '\00A0';
@entity-dot: '\00B7';

.content-dots (@numdots) {

    //Mixin output
    .nested-dots(1, @dots1) {
        @dots: "@{dots1}@{entity-dot}";
    }

    //Mixin iterator
    .nested-dots(@index, @dots1) when (@index > 1) {
        @dots2: "@{dots1}@{entity-dot}@{entity-space}";
        .nested-dots((@index - 1), @dots2);
    }

    //Call mixin
    .nested-dots(@numdots, "");

    //Output dots
    content: "@{dots}";
}

.heading {
    .nesting-loop (@i) when (@i >= 1) {
        &.nesting-@{i} {
            &:before, &:after  {
                .content-dots(@i);
            }
        }
        .nesting-loop (@i - 1);
    }
    .nesting-loop (@nesting-levels);
}

The resulting code is as follows:

.heading.nesting-5:before,.heading.nesting-5:after{content:"\00B7\00A0\00B7\00A0\00B7\00A0\00B7\00A0\00B7"}
.heading.nesting-4:before,.heading.nesting-4:after{content:"\00B7\00A0\00B7\00A0\00B7\00A0\00B7"}
.heading.nesting-3:before,.heading.nesting-3:after{content:"\00B7\00A0\00B7\00A0\00B7"}
.heading.nesting-2:before,.heading.nesting-2:after{content:"\00B7\00A0\00B7"}
.heading.nesting-1:before,.heading.nesting-1:after{content:"\00B7"}

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

Designing Your WordPress Website's Header Structure

When working on my non-WP sites, I usually handle columns using Bootstrap. However, this is my first time delving into CSS positioning without it. I am trying to align my site header elements similar to the image shown, where the text elements are flush ri ...

Is using a div inside a li considered incorrect?

For instance; <ul> <li> <div class="img1"><img src="img.jpg" /></div> <div class="cs1">Some text</div> <div class="cs2">other text</div> <div class="button">Click here</div> ...

Creative Ways to Use jQuery Hover Effects: Make One Div Vanish While Unveiling Another

I posted a question earlier, but I forgot to include the second part of it. The initial question was about creating a hover animation (which is working perfectly). However, I am facing an issue where one div disappears and another one needs to appear in i ...

Testing for Compatibility Across Different Web Browsers

What resources do you turn to for testing cross browser compatibility? Lately, I've primarily relied on Chrome developer tools, but they often lack accuracy. I attempted to test some projects using websites that claim to simulate various devices, bu ...

What steps do I need to follow to get this AngularJs Gallery up and running

As I delve into expanding my knowledge in AngularJS, I've encountered some issues while trying to run code on Plunker. Could someone take a look at the code and point out what I might be doing incorrectly? The code snippet is provided below: var a ...

Preventing the copying and pasting of HTML ruby tags

I am currently using ruby tags to include pinyin in my text. For instance: <p> <ruby>与<rt>yǔ</rt></ruby><ruby>摩<rt>mó</rt></ruby><ruby>拜<rt>bài</rt></ruby><ruby& ...

Attempting to zoom in when hovering over the circular image causes it to spill out of the container

Can someone help me figure out why my circular image isn't zooming when hovered? I've tried adding CSS properties, but the image keeps overflowing the container. What am I missing here? See the code snippet below. .col-lg-4.col-md-6.p-4{ ove ...

Tips for placing a div within a flex item

Struggling to get it right, here's my query: This is how my DOM looks: <div id="content"> <div class="listitem"> <img class="thumbnail"/> <div class="option_icon"></div> ...

Adjusting the window size to see the responsive sidebar menu in Bootstrap 4

I am attempting to transform the left sidebar into a small icon (=more settings) when the window is resized for smaller devices. Problem: My code displays an overlap of two columns when the window is resized to medium size. How can I resolve this overlap ...

Having dual fixed navigation bars on a single webpage

I am looking to create two fixed navigation bars, one at the top and another in the center of the page. The idea is that when scrolling reaches the second nav bar, the first one should hide (or become relative) and the second one should become fixed. Then, ...

Connecting to a nearby version of Bootstrap causes CSS to malfunction

Currently, I am facing an issue while developing an angular+bootstrap application for my work. At the initial stages of development, I encountered a problem where all the CSS breaks whenever I link to a local copy of Bootstrap, whether minified or not. My ...

Why does this function properly on every browser except for Opera?

I have implemented a script that allows me to replace the standard file upload input with a custom image. The script also ensures that an invisible 'browse' button appears underneath the mouse pointer whenever it hovers over the custom image. Thi ...

Lock the initial column in an HTML table

Hey there! I've been trying to freeze the first column of my HTML table, and while I managed to do so after a few attempts, I encountered an issue. When I scroll the table horizontally, the columns on the left seem to overlap with the first column, an ...

CSS to Reverse Align Navigation Bar Text

I'm currently working on my first website and I've encountered a problem with the Navigation Bar. Using CSS rules to align the bar, I noticed that when I apply float:right;, the text floats to the right but ends up aligning backwards on the bar. ...

Tips for adding jQuery UI styling to elements generated dynamically

I have a challenge with generating text fields using jquery while applying jquery ui styling to the form. The issue is that the dynamically created elements do not inherit the css styles from jquery ui: let index = 0; $('#btn_generate').on(& ...

Guide to positioning elements in CSS

I'm struggling to get all the elements to align in a single row. I've tried using display: inline-block, but it's not working as expected. I'm working with DataTables and I want the button images to be aligned with the page number box. ...

Switch the accordion to open or close

I am struggling to create an accordion menu that toggles open and close using the same button. Right now, it only opens and I cannot figure out how to make it close as well. Below is my current code, or you can view it on jsFiddle: http://jsfiddle.net/nd ...

What is the best way to differentiate between two CSS elements with the same class name?

I ran into an issue where two elements share the same class name, specifically "img" Is it possible to apply different styles to these elements that are nested under different parent classes, despite having the same class name? I am trying to customize t ...

Cease and Begin Page Overflow / Scroll with a Single Click Feature

Background: I recently followed a tutorial on how to create a lightbox using HTML, CSS, and JavaScript. However, I encountered an issue where I wanted to disable the scrollbar functionality when the lightbox is displayed, preventing users from scrolling ...

organizing the elements in the list into a visually appealing layout

I am currently working on transforming table data into a pivot format and presenting it in a horizontal list. My goal is to ensure the design is responsive enough to be viewed on mobile devices as well. Below you can find the CSS code I have implemented fo ...