Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use DIV class and ID together in CSS?

Tags:

html

css

Can I use DIV Class and ID together in CSS? For example:

<div class="x" id="y">     -- </div> 
like image 271
Billa Avatar asked Dec 06 '10 22:12

Billa


People also ask

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 .

Can we give ID to div?

The simple difference between the two is that while a class can be used repeatedly on a page, an ID must only be used once per page. Therefore, it is appropriate to use an ID on the div element that is marking up the main content on the page, as there will only be one main content section.

Is div class the same as div ID?

The main difference between div id and div class is that the div id involves assigning an id attribute to a specific div element to apply styling or interactivity to that element, while div class involves assigning the class attribute to several div elements to apply styling or interactivity to a set of div elements.

How do you reference a class and ID in CSS?

In the CSS, a class selector is a name preceded by a full stop (“.”) and an ID selector is a name preceded by a hash character (“#”).


2 Answers

Yes, yes you can.

#y.x {  /* will select element of id="y" that also has class="x" */ } 

Similarly:

.x#y {  /* will select elements of class="x" that also have an id="y" */ } 

Incidentally this might be useful in some use cases (wherein classes are used to represent some form of event or interaction), but for the most part it's not necessarily that useful, since ids are unique in the document anyway. But if you're using classes for user-interaction then it can be useful to know.

like image 185
David Thomas Avatar answered Sep 22 '22 10:09

David Thomas


You can also use as many classes as needed on a tag, but an id must be unique to the document. Also be careful of using too many divs, when another more semantic tag can do the job.

<p id="unique" class="x y z">Styled paragraph</p> 
like image 32
Pete Avatar answered Sep 18 '22 10:09

Pete