Currently, I am facing an issue where I need to display a variable number of items with a uniform margin setting that changes across the set.
In simple terms, if I have a set like [1,2,3,4,5], it would look like this:
1 2 3 4 5
If the number of items doubles, they should still occupy the same amount of space:
1 2 3 4 5 6 7 8 9 10
I have come up with some solutions, but what struck me was the idea of using a css-class (since the margin is consistent) to standardize the layout. It would be great if I could dynamically update the class to achieve this effect. However, I haven't found any information on how to do this. For instance, if the initial margin in the first example is margin-right: 10px;
, then changing the class through something like
.myclass.setProperty('margin-right', '5px');
would modify the margin property for all elements assigned to that class.
Currently, I'm using a class for shared behavior and setting individual styles for each element, which can be quite laborious:
for (var i in mynumbers) {
mynumbers.style.marginRight = new_margin;
}
Is there a way to dynamically change a class or if you have any suggestions on how to improve this approach? Your input and ideas are greatly appreciated. Thank you for taking the time to read and consider this problem.