Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a div with class="name1 name2"?

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?

like image 444
Rob Avatar asked Aug 11 '11 13:08

Rob


People also ask

How do you reference a div?

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.

How do I refer a div class in CSS?

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.

Can I use 2 class in a div?

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.

Can you give a div a class and ID?

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.


2 Answers

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.

like image 181
James Allardice Avatar answered Sep 28 '22 08:09

James Allardice


div.related.products is the general method

like image 30
Joseph Marikle Avatar answered Sep 28 '22 08:09

Joseph Marikle