I am working on an AngularJS project and the attributes are numerous in many of my HTML elements:

  <button type="submit" 
          class="btn btn-success pull-right">
      Submit this Product

Is there a standardized convention for the order of these attributes? Alphabetical? Grouped by meaning (e.g. all angular attributes together)? Standard HTML attributes ("type" for example) first?

  • 6
    No, there's no standard. W3C says that the order of attributes does not matter. Find some Angular code examples somewhere on the Internet or in an Angular book, and follow whatever convention they've set there; chances are it's close to "correct," if not actually "correct." – Robert Harvey Dec 17 '15 at 16:48
  • I dont think so. But if you look at the dom in console it is alphabetically ordered. – Abel D Dec 17 '15 at 16:49
  • 1
    In addition, you can find loads of advice by Googling "Ordering attributes in HTML tags." – Robert Harvey Dec 17 '15 at 16:52
  • Thanks AbelD and @RobertHarvey. I am indeed finding plenty of opinions via Google, but was hoping that there is a more standard approach. Thanks again. – Matt Cashatt Dec 17 '15 at 16:58
  • 1
    @AbelD Alphabetical ordering of HTML attributes sounds like the worst idea ever though, for a developer. – Alternatex Dec 18 '15 at 13:15

There is no standard. Optimize for ease of understanding, and try to be consistent.

Personally I think it's also good to always put the id attribute first, so that it's easier for testers to find it.

  • 3
    I find the most important thing is to be concise. If working in a team, make sure you follow the same pattern for the aforementioned reason. I tend to stick to type, id/for, name, class. If I'm using angular then all ng--attributes will go first. It's down to personal preference. – die maus Dec 17 '15 at 17:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.