Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS class and id with the same name

Tags:

css

class

Is there anything wrong with having a css class and id with the same name? Like .footer for the article/post's footer and #footer for the page footer.

like image 639
Spencer Avatar asked Jul 15 '10 08:07

Spencer


People also ask

Can an element have the same class and id?

Yes, you can. But note that Id's must be unique within your html file, while classes can be used in multiples elements.

Can you name 2 differences between classes and IDs?

In Html for an element ID name starts with the “#” symbol followed by a unique name assigned to it. On the other hand class assigned to an element has its name starts with “.” followed by class name. Only one ID selector can be attached to an element. Multiple class selectors can be attached to an element.

Can I use class and id together in HTML?

Remember the difference between Class and ID: A Class name can be used by multiple HTML elements, while an ID name must only be used by one HTML element within the page.

Is ID and class the same?

The difference between an ID and a class is that the first one is unique, and the second one is not. This means that each element can have only one ID, and each page can have only one element with that ID. When using the same ID on multiple elements, the code won't pass validation.


1 Answers

Nope, perfectly acceptable.

A class is defined using a . and an ID is defined using a #. So as far as the browser is concerned, they're two totally separate items.

The only thing to be careful of is generating any confusion for yourself. It's probably best practise to keep the names different purely for code readability, but there's no harm in them being the same.

like image 55
djdd87 Avatar answered Sep 22 '22 01:09

djdd87