Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an element have both an id and a class?

Tags:

html

css

class

pretty self-explanatory.

like image 707
kim holder wants Monica back Avatar asked May 11 '10 19:05

kim holder wants Monica back


People also ask

Can ID and class be the same?

Yes, you can use same name for both id and class because both parameters have their own significance.

Can we use ID and class together in CSS?

You Can Use Both ID and CSS Class Selectors This <div> tag will be subject to the styles for the class backgroundOrange .


2 Answers

Yes, an element can have one ID (which must be unique!) and multiple classes at the same time. To have multiple classes, use a space between them, here's an example:

<div id="myID" class="class1 class2 class3">Content</div> 
like image 82
Nick Craver Avatar answered Oct 22 '22 14:10

Nick Craver


I would like to add that if you add both ID and a class that contradict each other, the ID will have higher priority.

For example:

CSS:

.par_color{     color:red; }  #par_color{     color:blue; } 

HTML:

<section id="par_color" class="par_color">Some txt</section> 

Some txt string will be shown in blue and not in red.

like image 33
Vandervidi Avatar answered Oct 22 '22 14:10

Vandervidi