Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using uppercase, dashes or underscores when naming css selectors [closed]

What are the best practices when naming CSS selectors?

SomeContainerContent some_container_content some-container-content 

I read the other similar questions here on stack overflow, but there is no general consensus in the answers. I was wondering if anyone else had more to add.

like image 937
deb Avatar asked Feb 26 '10 22:02

deb


People also ask

Should CSS classes be capitalized?

Using Title CSS, you'd do the following: For any global CSS class, use a capitalized name (title case). For any modifier or descendant class, use a lowercase letter for the beginning of th name.

What does __ mean in a CSS class?

__ represents the element and -- represents the modifier.

What are the naming conventions for CSS?

Naming rulesNames are written in lowercase Latin letters. Words are separated by a hyphen ( - ). The block name defines the namespace for its elements and modifiers. The element name is separated from the block name by a double underscore ( __ ).

Can CSS classes have Underscores?

In CSS, one can use underscore(_) instead of hyphen(-) for CSS selectors(class, id, span, …), but preference is given based on its ease to use. Underscores require hitting the Shift key and are therefore harder to type.


2 Answers

The general thought is to use hyphens. There are, in fact, potential compatibility issues with other options.

  • Underscores are not allowed in class names or IDs in certain (granted, old, NS4-type) browsers.
  • The CSS spec does not specify how browsers should handle case, so using camelCase leaves one open to ambiguous interpretation.

Additionally, CSS uses hyphens internally for things like first-child.

Though the compatibility issues are generally a non-issue in modern browsers, that's how the standard came about, and I recommend sticking with it. You'd be fine if you were to deviate, but why bother?

like image 193
Matchu Avatar answered Oct 05 '22 17:10

Matchu


.hyphens-for-multi-word-names .underscores_for_pseudo_namespacing 

Results in:

.grid-system_push-100  \_________/ \______/   namespace   object 

Namespace and object can easily be purposed for a sort of BEM implementation.

.grid-slot { }          /* block */  .grid-slot_size-100 { } /* block-specific modifier */  ._size-100              /* generic modifier */ 

My $0.02

like image 28
Dan Lugg Avatar answered Oct 05 '22 17:10

Dan Lugg