title: Best practice for CSS class naming for use with jQuery selectors
body: <p>While building a Javascript-heavy web application, what is the best practice for naming CSS classes to keep the Javascript code and CSS stylesheets clean and the UI structure flexible?</p>
  
  <hr>
  
  <p>Option 1: <em>Name every single element uniquely</em>.</p>
  
  <p>For example,</p>
  
  <pre><code>// HTML
  &lt;div id="list"&gt;
    &lt;button class="list-delete" /&gt;
    &lt;div class="list-items"&gt;
      &lt;div class="item"&gt;
        &lt;button class="item-delete" /&gt;
        &lt;h1 class="item-name"&gt;Item 1&lt;/h1&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  // CSS
  .list-delete {
    color: black;
  }
  
  .item-delete {
    color: blue;
  }
  
  // Javascript
  $(".list-delete").show();
  $(".item-delete").hide();
  </code></pre>
  
  <p>Pros:</p>
  
  <ul>
  <li>Selecting an item for styling or JS manipulation is easy</li>
  </ul>
  
  <p>Cons:</p>
  
  <ul>
  <li>Element names start becoming really long and hard to keep track of</li>
  <li>Changing the HTML structure requires lots of renaming</li>
  </ul>
  
  <hr>
  
  <p>Option 2: <em>Name every element semantically, and select elements hierarchically</em>.</p>
  
  <p>For example,</p>
  
  <pre><code>// HTML
  &lt;div id="list"&gt;
    &lt;button class="delete" /&gt;
    &lt;div class="items"&gt;
      &lt;div class="item"&gt;
        &lt;button class="delete" /&gt;
        &lt;h1 class="name"&gt;Item 1&lt;/h1&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  // CSS
  #list &gt; .delete {
    color: black;
  }
  
  #list &gt; .items &gt; .item &gt; .delete {
    color: blue;
  }
  
  // Javascript
  $("#list &gt; .delete").show();
  $("#list &gt; .items &gt; .item &gt; .delete").hide();
  </code></pre>
  
  <p>Pros:</p>
  
  <ul>
  <li>Naming feels more natural, and names are short and clear</li>
  </ul>
  
  <p>Cons:</p>
  
  <ul>
  <li>Selecting an element for styling or manipulation is unwieldy</li>
  <li>Changing the HTML structure requires changing a lot of selectors, since names are tied to hierarchical structure</li>
  </ul>
  
  <hr>
  
  <p>Option 3...n: Some hybrid approach? A totally different approach altogether?</p>
  
  <p>Keep in mind the problem of name collision when adding more elements in the future, especially when you have nested elements. Also, the ideal solution would make it easy to change the HTML structure of existing elements without too much disruption everywhere else.</p>