12

Hello and thank you for taking your time to read my question. My question pertains to html best practices.

I have a html skeleton that looks like this..

    <!DOCTYPE html>
<html lang="en">
    <head>
        <title>Template</title>
        <meta charset="utf-8">
    </head>
    <body>
        <header>
        </header>
        <main>
        </main>
        <footer>
        </footer>
    </body>
</html>

and I want to start adding HTML elements that I will later select these elements by ID and CLASS within CSS and JavaScript. My "Best Practice" related questions are..

  • Do ID and CLASS have identical naming conventions?
  • What is the minimum and maximum recommended ID and CLASS attribute length?
  • Why do websites like facebook name there ID and CLASS atributes with random numbers and letters?
  • Should I use camelcase?
  • Should I use underscores or numbers?
  • Should I shorten words with abbreviates like "Detail" becomes "Det" when naming?
  • Should I give ever elements that is a child of body a ID and CLASS atribute?
  • Would I ever need to specify an ID or CLASS attribute for a element within the HEAD element?
Kyle
  • 135
  • 1
  • 1
  • 8

2 Answers2

9

Some of these answers are down to personal preference:

Do ID and CLASS have identical naming conventions?

The naming conventions are up to you.

What is the minimum and maximum recommended ID and CLASS attribute length?

There are, as far as I know, no such recommendations.

Why do websites like facebook name there ID and CLASS atributes with random numbers and letters?

To avoid accidental duplication and for tracking purposes.

Should I use camelCase?

It's one good option.

Should I use underscores or numbers?

These are more good options.

My own system (I don't know anyone else who uses it) is the following:

  • HTML - <div class="my-string"> [hyphens]
  • CSS - .my-string [hyphens]
  • Javascript - var myString = ''; [camelCase]
  • PHP - $My_String = ''; [underscores]

By using different syntax for variables and classes, I avoid mixing up different types of data.

Should I shorten words with abbreviates like "Detail" becomes "Det" when naming?

You can but personally I wouldn't. But it's up to you.

Should I give ever elements that is a child of body a ID and CLASS atribute?

Not sure I understand this question.

Would I ever need to specify an ID or CLASS attribute for a element within the HEAD element?

No. There is no need for this.

Rounin
  • 27,134
  • 9
  • 83
  • 108
  • 1
    Your css naming convention goes against the PSR-1 and PSR-12 conventions, which are basically to use camelcase for variables. Your PHP examples should be `$myString = '';` I personally would never include the variable type in the name of the variable, but I get that these were contrived examples. – gview Feb 16 '23 at 01:32
  • 1
    Sorry, in the comment above I wrote 'css' when I meant 'php'. My comment mainly goes to PHP, although I'm a fan of using [BEM](https://keepinguptodate.com/pages/2020/05/bem-visually-explained/) for css naming. – gview Feb 16 '23 at 01:45
  • Thanks for the comments regarding, **PSR**, @gview. I'm less familiar with the conventions than I should be and yours is a timely reminder that I should spend some time reading up on them. When I wrote this in June 2017, that was how I wrote CSS classnames. I adopted BEM in October 2018 and have never really looked back. – Rounin Feb 17 '23 at 23:13
2

I feel like an updated answer is warranted here. These are obviously guidelines and opinions, but also informed by my observations of what conventions are defacto community standards and/or widely adopted.

PHP

Variables, functions and method names should be camelcase: $myVariable = 'foo';

Class names and interfaces should be Pascal Case

class MyClass implements MyInterface { .. }

PHP has two widely adopted standards that cover this and much more in regards to code standardization, and many of these standards are followed by the major PHP project like Laravel and Symfony. Those standards are documented in PSR-1 and expanded/revised in PSR-12.

HTML

HTML5 is the document standard, which also expanded content specific tags. Beyond that, there's been a move towards lowercasing everything, where in the early days people often uppercased the names of any tags, even though that didn't matter.

As of 2023, lowercase your names. If you have multiple words in a name separate the words with hyphens.

Consider attribute naming of things like the data attributes as representing the emergence of this convention.

<div id="my-products" name="my-products" data-category="5">

CSS

I've seen fairly wide adoption of BEM as well as the emergence of css frameworks beyond bootstrap, like materialUI and Tailwind.

BEM also is helpful in thinking about how to organize styles for html "components" where you have a nested grouping of elements that equate to a page component.

<style>
  .btn {
    border: none;
    color: white;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    padding: 12px 28px;
    cursor: pointer; 
  }
  .btn--submit {
    background-color: green;  
  }
   
  /* a product card component with styles for each child element of the component */

  .product-card { .. }
  .product-card__title { .. }
  .product-card__image { .. }
  .product-card__price { .. }
  .product-card__description { .. }
  .product-card__order-button { .. }
   /* used if item out of stock */
  .product-card__order-button--disabled { .. }
</style> 

Javascript

Javascript and PHP conventions are very similar. Google has published a js coding style guide similar to those that came from the PHP framework interop group I linked to above.

The major js frameworks (angular, react, vue, etc) sometimes have conventions specific to them, that are worth researching.

Variables camelcase

let myObj = { name: 'Michael' }

Classes uppercase

class MyClass {
  constructor() { .. }
}

PYTHON

The Python manual has a link to 2 extension proposal documents

To summarize the basics:

Variables and functions should be lowercase with underscores between words Use Pascal Case/Studly caps for Classes

my_variable = 'Something'
class MyClass:

Python styles is explored more fully in this question.

I only included the languages most typically involved in Web Development as that was the focus of the original question, as well as the accepted answer.

gview
  • 14,876
  • 3
  • 46
  • 51