Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML - Using same name for an ID and a Class [duplicate]

Tags:

html

css

Is it O.K. to use the same name for a classname and an ID name? Is it a code smell?

<div class="wrapper" id="wrapper"></div>
like image 755
user2952265 Avatar asked Jan 14 '14 00:01

user2952265


Video Answer


2 Answers

Yes, it's valid to do it. It's not common to need to do it, though: it's a bit of a code smell. It may indicate you need to revisit your naming conventions.

To expand on the issue of naming conventions and semantics:

When you use an ID of wrapper, you're saying "This is the wrapper - the only one of its kind". When you use a class of wrapper you're saying "This is one of the wrappers, and there may be others like it". Saying both of those things at the same time is a bit weird, right?

I'd suggest that you use a specific ID: #primaryWrapper, #outerWrapper, etc. That means you're expressing 'primaryWrapper is one of the wrappers', which makes heaps more sense.

like image 137
Ben Hull Avatar answered Nov 15 '22 09:11

Ben Hull


Yes, it's totally valid and you can do it

Hopefully you know that
#ID must be unique-per-page, while
.CLASS can be assignet to more than one element.

If you incline to write clean and readable HTML markup, try always to keep your attributes at an understandable minimum.

like image 37
Roko C. Buljan Avatar answered Nov 15 '22 07:11

Roko C. Buljan