0

I'm trying to apply a CSS style to multiple div elements as in the following HTML code:

{% for i in player_range %}
<div id="container-frame-{{ i }}"
...
</div>
{% endfor %}

Where the value of i is variable and being passed to the HTML from the Python side of my software.

Below is the CSS style I'd like to apply for each element.

#container-frame-0{
   border-style: solid;
   border-width: 5px;
   border-color: green;
}

I tried to hardcode the different values of i (e.g, 0,1,2 etc...) to the container-frame style and it works fine. However, I'm searching for a cleaner way to do it.

Ideally I'm looking for something like this:

#container-frame-{{i}}{
   border-style: solid;
   border-width: 5px;
   border-color: green;
}

Where i is the same as in the HTML. Any idea how to do so?

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
Joseph Wahba
  • 660
  • 3
  • 9
  • 25
  • what you want is not possible with css, you have to use css preprocessor, like sass or less, alternatively you could add a class to your div `
    – azs06 Feb 28 '17 at 15:01
  • @azs06 — Don't use comments to give answers. Especially wrong answers. – Quentin Feb 28 '17 at 15:01

5 Answers5

6

Give the div a class. Use a class selector.

<div id="container-frame-{{ i }}" class="container-frame"

.container-frame {
    border-style: solid;
    border-width: 5px;
    border-color: green;
}

You could also use an attribute selector.

[id^="container-frame-"] {
    border-style: solid;
    border-width: 5px;
    border-color: green;
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

Use @Quentin approach or use this selector in your CSS:

div[id^="container-frame-"]{ ... }
Daniel Lagiň
  • 698
  • 7
  • 18
0

use class for mulitple elements, because id its just for on element

<div class="myClass"></div>
<div class="myClass"></div>

And in CSS:

.myClass{
            border-style: solid;
            border-width: 5px;
            border-color: green;
}
B.Denis
  • 23
  • 8
-1

So, your current code is not working because id's should only apply to one element. Try giving each div a class, and then style them like this:

/* Notice use of dot instead of hash */
.whatever-name-you-want {
   border-style: solid;
   border-width: 5px;
   border-color: green;
}

You can read up on this here: https://www.w3schools.com/css/css_syntax.asp

Hudson Taylor
  • 1,142
  • 2
  • 10
  • 30
-1

As mentioned by others, you could use a class but alternatively you can use an attribute selector.

 div[id^="container-frame-"], div[id*=" container-frame-"] {
    border-style: solid;
     border-width: 5px;
     border-color: green;
 }

Please see this question for more detail: wildcard * in CSS for classes

Community
  • 1
  • 1
Naqash Tanzeel
  • 103
  • 2
  • 8