I have a scenario where I am getting ID generated like this

<div> <div></div> <div></div> <div></div> <div></div> </div> 

and so on

is there a way I could write some css to target them through a loop? maybe something like

#new[i; for(i=0; i<="containerLength.length"; i++)]{ float:left; } 

Probably I am day dreaming correct?

9

5 Answers

You can't do loops with pure CSS, however, if you're using something like SASS or LESS then you can do both like:

SASS:

@for $i from 1 through 4 .#{$class-slug}-#{$i} width: 60px + $i 

LESS:

Can you do a javascript for loop inside of LESS css?

However, assuming you just want to apply the same style to each nested div, you can just do

.containerLength > div{ float: left; } 

or perhaps create a class named .float-left and apply it to each element you want floated right.

You can do

div.containerLength > div { /* > Matches only first level children of the wrapper div */ float: left; } 
4
div[id|="new"]{ float: left; } 

documentation

You may or may not need the quotes, it's weird sometimes.

you can't write any logic at all in css. you can, however, managed css with JavaScript, or include multiple id's in one rule, or just use a class.

You also may be able to use Css attribute selectors, depending on how the ids are arranged and how broad you need your browser support to be.

1

Why don't you try this:

.containerLength > div {float:left} 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.