Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple classes on single element html [closed]

Tags:

html

css

Is it a good practice to use many classes on one single HTML element? For example:

<div class="nav nav-centered nav-reversed navbar navigation-block"></div> 

I don't mean that two or three classes on one element is bad, but how about four, five or even six?

like image 592
Andrius Avatar asked Jun 28 '13 13:06

Andrius


People also ask

Can one HTML element have multiple classes?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.

Can a div have 2 classes?

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 a button have 2 classes?

Yes, just put a space between them. Of course, there are many things you can do with CSS inheritance.


1 Answers

Short Answer

Yes.


Explanation

It is a good practice since an element can be a part of different groups, and you may want specific elements to be a part of more than one group. The element can hold an infinite number of classes in HTML5, while in HTML4 you are limited by a specific length.

The following example will show you the use of multiple classes.

The first class makes the text color red.

The second class makes the background-color blue.

See how the DOM Element with multiple classes will behave, it will wear both CSS statements at the same time.

Result: multiple CSS statements in different classes will stack up.

You can read more about CSS Specificity.


CSS

.class1 {     color:red; }  .class2 {     background-color:blue; } 

HTML

<div class="class1">text 1</div> <div class="class2">text 2</div> <div class="class1 class2">text 3</div> 

Live demo

like image 158
Jeff Noel Avatar answered Oct 05 '22 22:10

Jeff Noel