Explain Codes LogoExplain Codes Logo

Why are dashes preferred for CSS selectors / HTML attributes?

html
css-selectors
html-attributes
best-practices
Nikita BarsukovbyNikita Barsukov·Aug 30, 2024
TLDR

When it comes to CSS and HTML, dashes (-) hold the trump cards. They provide clarity by separating words rather than blending them together like underscores or camelCase do. Dashes, being more CSS-friendly, steer clear of any confusion with JavaScript's camelCase conventions. In the realm of HTML5, data- attributes further catapulted the dashes to the standards podium. A quick glimpse at the preference for dashes —

<!-- Good ol' dashes saving the day --> <div class="user-profile" data-user-id="123"></div>
/* Ooh, those dashes look slick! */ .user-profile { color: #333; /* As cool as CSS can get */ }

Following the naming conventions

In the broader world of coding practices and URL segments, the love for dashes remains undiminished. They promise a uniform layout in the code, and their standardized use, pioneered by historical usage and bigger influencers like Google's SEO guidelines, further validate their prominence.

A nod to historical bias

When traversing the course of history, you'll stumble upon the point when dashes in CSS and HTML became deep-rooted due to earlier CSS versions’ tantrums. The usage of underscores came with the inconvenience of needing to be escaped, making the code less readable. Therefore, the technical community embraced dashes, keeping the ball of backwards compatibility and maintainability rolling.

Balancing semantic and design

Accessibility is at the heart of design. It emphasizes the importance of textual breaks in attributes and selectors, indispensable for screen readers and assistive technologies. Sweating over semantic clarity pays off, and using dashes in kebab-case ensures the conveyed information remains meaningful and easily digestible.

Bowing down to SEO guidelines

Search engines have a soft spot for dashes. Google's algorithm treats hyphens as spaces, transforming dash-separated phrases in URLs into something meaningful for SEO rankings. These best practices indirectly encourage developers to use dashes in their classes and IDs to maintain consistency with URL conventions.

Making coding efficient

Typing dashes is faster and consistently smoother compared to underscores. The experience of tabbing through code is noticeably more fluid. Less fiddling with the shift key significantly improves the working rhythm for developers over time. Awfully tempting, right?

Ensuring syntactic harmony

Brushing off syntax ambiguity is crucial. In JavaScript, an underscore could potentially masquerade as a private member, while dashes skirt around confusion with subtraction operations. Let's hear it for dashes, maintaining syntactic harmony across different languages.

Respecting syntactic norms

Just like every language has its quirks, developers must respect each language's conventions. In CSS, that means adhering to lowercase-hyphenated syntax for uniformity and to lighten the cognitive load for developers knee-deep in code.

Sticking to community guidelines

Community norms create a coding road less bumpy. Aligning with the established conventions, like using dashes, ensures code consistency while offering an intuitive developer experience.