Applying CSS to iterate through values using foreach instead of relying on JavaScript

I have been wondering whether it is achievable to shift each value to the right using only CSS with logic similar to this:

<div ng-show="c.reviewId==item.id" style="margin-left:" . {{$index}}*10 . "px"">

For instance, the desired output could look like:

1.something
   2.something
      3.something

Is it necessary to rely on javascript/angular for this task?

Answer №1

Experience the power of CSS Less:

.run(@n; @i : 1) when (@i =< @n)
{
  div:nth-of-type(@{i}) {
    margin-left: @i*10px 
  }

  .run(@n; (@i + 1));
}

.run(6);

See the magic unfold in the generated code:

div:nth-of-type(1) {
  margin-left: 10px;
}
div:nth-of-type(2) {
  margin-left: 20px;
}
div:nth-of-type(3) {
  margin-left: 30px;
}
div:nth-of-type(4) {
  margin-left: 40px;
}
div:nth-of-type(5) {
  margin-left: 50px;
}
div:nth-of-type(6) {
  margin-left: 60px;
}

Discover more about CSS Less here!

Answer №2

Using only CSS, you can achieve this by adding padding-left with index*5 in your code

 "<div style='padding-left:" + (index * 5).ToString() + "px'>"+index.ToString()+". hello</div>"

This will give you the desired output:

        <div style="padding-left: 0px">0. hello</div>
        <div style="padding-left: 5px">1. hello</div>
        <div style="padding-left: 10px">2. hello</div>
        <div style="padding-left: 15px">3. hello</div>
        <div style="padding-left: 20px">4. hello</div>
        <div style="padding-left: 25px">5. hello</div>
        <div style="padding-left: 30px">6. hello</div>
        <div style="padding-left: 35px">7. hello</div>
        <div style="padding-left: 40px">8. hello</div>
        <div style="padding-left: 45px">9. hello</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

Broken href link on background image with absolute positioning

Having trouble adding a link over a background image of the head-bg-image div into ul li. The link is not functioning properly. <div id="product_banner"> <div class="head-bg"> <div class="head-bg-img"></div> <ul class="d-inline ...

Checkbox in Bootstrap should be displayed before the submit button on smaller screens

I've been searching for a solution to this issue without any luck. In my setup, there are three input fields and a submit button displayed side by side on large screens, enclosed in <div class="col-sm"> to stack vertically on mobile devices. Ho ...

Place the <span> element at the bottom of the <ul> list

Struggling to align the captions of an image carousel at the bottom of the <ul>. No matter what I try, it just doesn't look right. Check out my Fiddle here Below is the HTML structure: <div class="carousel"> <ul> <l ...

Utilizing Typescript in conjunction with a personalized React hook

Exploring how to create a unique custom react hook in TypeScript that can take an object with all optional React.CSSProperties as keys, such as... const example = useExample({ font: { initial: 'Arial', new: 'Roboto' } }) ...

Tips for moving an element to the end of an array

Patients' data is stored in the MongoDB database, and all patients are mapped through on the frontend to display a list. An additional boolean value indicates whether a patient is archived or not. If a patient is archived, it should be displayed at th ...

Is it possible for bootstrap to be integrated with specific CSS styles?

I want to utilize bootstrap to create some columns using predefined classes like .col-sm-, and include additional widths for smaller phones. Despite seeing the layout looking fine on Chrome and a few devices, Netbeans throws a "class not found" message for ...

Input text into search box to transfer value to URL without using the equal (=) symbol

Currently, my website allows users to search by entering a value in the URL after /search/. For instance, searching for "hello" can be done by visiting www.mywebsite.com/search/hello. Now, I am looking to implement a simple search box that functions simil ...

Arrange moving shapes in the same spot

Every time I press the button, a unique shape is generated. These shapes are dynamic and can range from polygons to circles (there are hundreds of shapes). Each shape is composed of a series of lines. The problem arises when each shape is positioned diff ...

Restricting the movement of the mouse event

Is there a way to restrict the mousemove event to a specific area, such as a div located in the center of the page? Thank you in advance if (window.innerWidth > 765) if (matchMedia('(pointer:fine)').matches) $(document).ready(function() { $ ...

The functionality of two-way binding in a distinct controller is not functioning properly when integrated with angular-wizard

I've been working hard to integrate this amazing wizard controller into my project: However, I've hit a roadblock with two-way binding not functioning as expected outside of the <section> attribute: http://plnkr.co/edit/N2lFrBRmRqPkHhUBfn ...

Adding a fresh, spacious breakpoint in Bootstrap 4 with just CSS

I encountered an issue when the extra large Bootsrap 4 max container width, set at 1140px, was not wide enough for my needs. I aim to ensure that my websites look good on large monitors as well, but the bootstrap grid is too narrow. The most straightforwar ...

Prevent Console tracing in Angular when encountering errors with $http requests

As I work on my application, I rely on a REST api to fetch my data. When I make a request like this: $http.get('api/entity/' + $scope.entityId).success(/* DO STUFF */).error(/* DO STUFF */) In the service, if the entityId is not found, I return ...

Optimal method for aggregating results from a search query across a collection of 100,000 documents

Assuming I have a collection with 100k documents structured like this: { { userName: "cary" time: 50 userId: 1 } { userName: "john" time: 40 userId: 1 } { userName: "bliss" time: 50 ...

Is it possible to determine if the selected/clicked element is a multiple of "n" using jQuery?

If I have 'n' li elements within a ul, I am looking to trigger an alert message only when the li item that is selected or clicked on is a multiple of "n" (for example 3). In this scenario, the alert should only appear if I click on the 3rd, 6th, ...

Using Jquery to attach an event to a particular div which is part of a group of divs sharing

I am trying to implement a mouseup event on a series of divs that, when clicked, will reveal a child div ('menu'). All the parent divs have the same class. Here is an example: <div class="container"> <div class="menu"><p>Text ...

Displaying AngularJS form validation through code snippets

I need help creating a form validation using AngularJS and bootstrap. When the submit button is clicked, I want the form to simply print the information in a code snippet format. For example, the form fields would be: First Name: (input element) Last Name ...

Determine the dimensions of child components within Svelte templates

I am currently in the process of transitioning a VanillaJS website to Svelte, and I need to have divs randomly positioned on the webpage. It's important to note that each div's size can vary depending on its content, so they cannot have a fixed s ...

Maintain consistent alignment of the backgrounds for each term-description pair within a definition list resembling a table

I am trying to style a definition list in HTML to resemble a table using th and td elements in two separate columns, while also having alternating row backgrounds. I have implemented the following CSS for this purpose: dl { font-family: Verdana, Genev ...

What is the reason for JavaScript documentation using a nested array to define a function's parameters in its syntax?

I'm struggling to articulate my question, but as I delve into the documentation for JavaScript and Node.js, I notice they define syntax in a way such as this. var new_array = old_array.concat(value1[, value2[, ...[, valueN]]]) It seems like all th ...

Invoking a function from an AngularJS 1.x controller

I'm facing a simple issue. Within my Angular 1.x application, I have a function that I invoke from my template: searchesDTController as results results.filterResults = function(filter = null) {} However, I also want to call the same function within ...