Just wondering when you use multiple classes on the one element such as class="foo bar"
and those classes are setup as below:
.foo { margin-right: 10px; } .bar { margin-right: 0px; }
Which class will have specificity? Will the margin be 10px or 0px?
HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.
To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.
Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well. All you have to do is separate each class with a space.
It works based on precedence within the CSS. Therefore the item to occur most recently will override any previous styles.
CASE 1
.foo { background : red; } .bar { background : blue; }
class = 'foo bar'
would be blue in this instance.
CASE 2
.bar { background : blue; } .foo { background : red; }
class = 'foo bar'
would be red in this instance.
Working Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With