Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML: Naming conventions for ID attributes

Tags:

html

naming

Lately I've been doing a lot of front-end work. Some developers here have been naming their elements things like "divPhotoGalleryContainer" and sometimes I'll just see "galleryContainer."

Is there a good naming convention? Is adding "div" really useful?

like image 219
JamesBrownIsDead Avatar asked Dec 16 '09 01:12

JamesBrownIsDead


People also ask

How do you NAME a ID in HTML?

Using The id Attribute It is also used by JavaScript to access and manipulate the element with the specific id. The syntax for id is: write a hash character (#), followed by an id name. Then, define the CSS properties within curly braces {}.

Should IDs be camelCase?

“ID” and "UID" should be correctly capitalized in all functions, variables, etc. according to camelCase standards.


3 Answers

The stupid thing is, Hungarian notation like divPhotoGalleryContainer is totally unnecessary with CSS. You can name the ID PhotoGalleryContainer and target it to a <div> element in the CSS:

div#PhotoGalleryContainer {
  /* rules */
}

Inside that rule you can usually assume certain properties like display: block, unless you're targeting generic divs somewhere else (kinda bad practice).

There aren't really any specific conventions for naming, just use names that are clear and simple.

like image 100
DisgruntledGoat Avatar answered Sep 29 '22 20:09

DisgruntledGoat


I don't think it's particularly useful, but I don't think it's harmful either. Consistency is the most important convention.

like image 33
Kaleb Brasee Avatar answered Sep 29 '22 18:09

Kaleb Brasee


The best naming convention is the one that makes sense to the developers/designers involved in the project. Given the two examples in your question, I'd be willing to bet that the "divPhotoGalleryContainer" contains "div" because either: it's referenced in a CSS selector, or some javascript code is looking at it and it's somehow helpful to know what type of element the id is referring to.

The "divPhotoGalleryContainer" convention seems like an HTML-ish flavor of Hungarian notation.

like image 42
JasonWyatt Avatar answered Sep 29 '22 20:09

JasonWyatt