0

I would like to repeat div class several times without having to code it, and being able to target each element individually with css. Possible?

Like instead of:

<div 
class="design">design</div>
<div 
class="design1">design</div>
<div 
class="design2">design</div>
<div 
class="design3">design</div>

I would have:

<div 
class="design">design</div> 
X4 

Is it better to use span class and is this possible to multiply too?

2
  • 1
    If each of these designX classes is unique you should use an id instead. id="design1" — IDs are meant to be unique, while the whole point of class is that many things can be of the same class. Also, it might make sense to use both: <div class="design" id="d1">...</div> Commented Jan 8, 2020 at 21:11
  • Like it......... Commented Jan 8, 2020 at 21:36

4 Answers 4

2

If I understood correctly what you want, then I think you can use nth-of-type(n). Although you'll need to repeat code in CSS... (you can avoid repeat HTML code by using Javascript, but since you didn't tagged it and not mentioned nothing about it, I think you want something in HTML and CSS only)

"The :nth-of-type() CSS pseudo-class matches elements of a given type, based on their position among a group of siblings."

.design:nth-of-type(1){
  color: purple
}

.design:nth-of-type(2){
  color: blue
}

.design:nth-of-type(3){
  color: red
}

.design:nth-of-type(4){
  color: green
}
<div class="design">design</div>
<div class="design">design</div>
<div class="design">design</div>
<div class="design">design</div>

And about span, it depends on what you are going to do, div is naturally display:block, while span is display:inline

further read about nth-of-type: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your post. But it's not possible to do without actually repeating the div class 4 times?
Is that what you need? Does this helps you? If yes, consider to upvote/accept
2

You could use javascript to accomplish this if you put it in a container.

<div id="div_container"></div>

<script>
    let output = "";
    for(i = 0; i<4; i++){
        output += "<div class='design'>design</div>"
    }
    document.getElementById('div_container').innerHTML = output;

</script>

You can further style it using .design:nth-of-type(1) .design:nth-of-type(2) etc.

This wouldn't make sense for 4 instances of the div, but more than 10 would be easier and scales to large numbers, simply change 'i' and create the div container.

EDIT: Define output prior to loop

2 Comments

@CalvinNunes - Sure, OP didn't mention specifically using JavaScript, but this is a valid answer because it's implied that he needs assistance with programatically creating/incrementing divs, RE: I would like to repeat div class several times without having to code it …
True, but OP doesn't mention anything about frameworks either which could be a fair argument. If OP explicitly said that they didn't want any other tools than HTML, I wouldn't recommend this solution. You wouldn't use a drill to cut a plank. I'm offering OP a Saw instead.
1

In order to output your HTML without rewriting it each time, you will need to use one of the following:

  • An actual programming language (Such as PHP)
  • A framework/library (such as React)
  • A preprocessor/templating system (such as Haml or Pug)

Using PHP is simple enough, if you have PHP installed on your webhost (you almost certainly do).

For instance, instead of this:

index.html:

<div class="design design-0">Design</div>
<div class="design design-1">Design</div>
<div class="design design-2">Design</div>
<div class="design design-3">Design</div>

You could have this:

index.php

<?php for( $i = 0; $i < 4; $i++ ){
    echo "<div class='design design-$i'>Design</div>";
} ?>

Or check out this Pug example: https://codepen.io/xhynk/pen/OJPvPMX if you would rather use a preprocessor.

For the CSS though, as @Calvin Nunes said, you can make use of the :nth-of-type() selector or even the Adjacent Sibling Combinator - though these largely make the need for the design-x type classes unnecessary, unless you have other reasons to include them.

Comments

1

If i understood correctly, I'll keep the record from the post upstairs, and no, you can't do it without making it repeat 4 times, each div or the group of divs in this case NEEDS something for you to work with 'em.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.