Forum
>
Hyphens or underscores in CSS and HTML identifiers...
Welcome, Guest

Hyphens or underscores in CSS and HTML identifiers?
Topic Normal Hyphens or underscores in CSS and HTML identifiers?
by Rishi on 05/27/2010 16:54

Hyphens or underscores in CSS and HTML

identifiers?

 

As  building the Classes or ID's in  CSS and HTML, there are some times we encounter with the question of which identifier we use for making large class. As per our CSS Frameworks and other CSS Tools we can use 2 identifiers. These are:

1. Hyphen (-)

2. Underscore (_)

 

But in case of using the this identifiers, we have question that "Which one (Hyphen or Underscore) is better to use?"  

 AS the regular learning i come to the point that-

As both hyphen (-) and underscore (_) are valid characters in CSS and HTML identifiers, what are the advantages and disadvantages using one or the other? I prefer writing CSS class names with hyphens (e.g. field-text) and underscores for IDs (e.g. featured_content). Is there a best practice or it's only the matter of taste? 

 

It's really just preference, but if you are going to be doing programming to manipulate the DOM or passing identifiers such as form names to programming languages, usually underscores are nicer.

For example:

<input type="text" name="my_input" id="my_input" />

<input type="text" name="my-input" id="my-input" />

If you are scripting it, you can do var my_input = document.getElementById('my_input'); or parse the incoming value when POSTed to php with <?php $my_input = $_POST['my_input']; ?>. However, the dashed version won't be translatable in either JavaScript or PHP because dashes are not allowed there.

You forgot to mention that camelCase, like myInput would be yet another convention for classes and ids.