I'm working on some CSS from a tutorial, a div has this class:
<div class="related products">
How can I reference it in the stylesheet?
You reference it by div. related. products which literaly translates to "a div with class of related and class of products". Or, you could reference it by using either class names, since it will catch both.
The .class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.
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.
Yes, any HTML element (like div, input, nav, body, etc) can have both “id” and “class” together and at the same time. The only difference here is that “id” can have only one unique value and “class” can have more than one.
The div
actually has two classes, related
and products
. You can reference it in your stylesheet with either .related
or .products
, and it will pick up the styles from both of those rules. For example, with the following CSS, the text in the div
in your question would appear red with font size 12:
.related { color:#ff0000 }
.products { font-size:12px }
If you want to select elements with both classes, use .related.products
in your stylesheet. For example, add the following to the above example:
.related.products { font-weight:bold }
And the text in your div
will receive all three rules, because it matches all 3 selectors. Here's a working example.
div.related.products
is the general method
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