Skip to content

Commit 8d4a22b

Browse files
authored
Update ID and class selector/attribute guidance
We now recommend avoiding unnecessary `id` attributes and prefer using class selectors for styling. Where `id` attributes are strictly required, we recommend always including a hyphen in the value.
1 parent 5ac8b60 commit 8d4a22b

File tree

1 file changed

+73
-29
lines changed

1 file changed

+73
-29
lines changed

htmlcssguide.html

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,34 @@ <h4 id="type_Attributes" class="numbered"><code>type</code> Attributes</h4>
371371
&lt;script src="https://www.google.com/js/gweb/analytics/autotrack.js"&gt;&lt;/script&gt;
372372
</code></pre>
373373

374+
<h4 id="id_Attributes" class="numbered"><code>id</code> Attributes</h4>
375+
376+
<p>Avoid unnecessary <code>id</code> attributes.</p>
377+
378+
<p>Prefer <code>class</code> attributes for styling and <code>data</code> attributes for scripting.</p>
379+
380+
<p>Where <code>id</code> attributes are strictly required, always include a hyphen in the
381+
value to ensure it does not match the JavaScript identifier syntax, e.g. use
382+
<code>user-profile</code> rather than just <code>profile</code> or <code>userProfile</code>.</p>
383+
384+
<p>When an element has an <code>id</code> attribute, browsers will make that available as a
385+
<a href="https://html.spec.whatwg.org/multipage/window-object.html#named-access-on-the-window-object">named property on the global <code>window</code> prototype</a>,
386+
which may cause unexpected behavior. While <code>id</code> attribute values containing a
387+
hyphen are still available as property names, these cannot be referenced as
388+
global JavaScript variables.</p>
389+
390+
<pre><code class="language-html bad">&lt;!-- Not recommended: `window.userProfile` will resolve to reference the &lt;div&gt; node --&gt;
391+
&lt;div id="userProfile"&gt;&lt;/div&gt;
392+
</code></pre>
393+
394+
<pre><code class="language-html good">&lt;!-- Recommended: `id` attribute is required and its value includes a hyphen --&gt;
395+
&lt;div aria-describedby="user-profile"&gt;
396+
397+
&lt;div id="user-profile"&gt;&lt;/div&gt;
398+
399+
&lt;/div&gt;
400+
</code></pre>
401+
374402
<h3 id="HTML_Formatting_Rules" class="numbered">HTML Formatting Rules</h3>
375403

376404
<h4 id="General_Formatting" class="numbered">General Formatting</h4>
@@ -477,12 +505,12 @@ <h4 id="CSS_Validity" class="numbered">CSS Validity</h4>
477505
CSS code that may not have any effect and can be removed, and that ensures
478506
proper CSS usage.</p>
479507

480-
<h4 id="ID_and_Class_Naming" class="numbered">ID and Class Naming</h4>
508+
<h4 id="ID_and_Class_Naming" class="numbered">Class Naming</h4>
481509

482-
<p>Use meaningful or generic ID and class names.</p>
510+
<p>Use meaningful or generic class names.</p>
483511

484-
<p>Instead of presentational or cryptic names, always use ID and class names that
485-
reflect the purpose of the element in question, or that are otherwise generic.</p>
512+
<p>Instead of presentational or cryptic names, always use class names that reflect
513+
the purpose of the element in question, or that are otherwise generic.</p>
486514

487515
<p>Names that are specific and reflect the purpose of the element should be
488516
preferred as these are most understandable and the least likely to change.</p>
@@ -494,45 +522,45 @@ <h4 id="ID_and_Class_Naming" class="numbered">ID and Class Naming</h4>
494522
document or template changes.</p>
495523

496524
<pre><code class="language-css bad">/* Not recommended: meaningless */
497-
#yee-1901 {}
525+
.yee-1901 {}
498526

499527
/* Not recommended: presentational */
500528
.button-green {}
501529
.clear {}
502530
</code></pre>
503531

504532
<pre><code class="language-css good">/* Recommended: specific */
505-
#gallery {}
506-
#login {}
533+
.gallery {}
534+
.login {}
507535
.video {}
508536

509537
/* Recommended: generic */
510538
.aux {}
511539
.alt {}
512540
</code></pre>
513541

514-
<h4 id="ID_and_Class_Name_Style" class="numbered">ID and Class Name Style</h4>
542+
<h4 id="ID_and_Class_Name_Style" class="numbered">Class Name Style</h4>
515543

516-
<p>Use ID and class names that are as short as possible but as long as necessary.</p>
544+
<p>Use class names that are as short as possible but as long as necessary.</p>
517545

518-
<p>Try to convey what an ID or class is about while being as brief as possible.</p>
546+
<p>Try to convey what a class is about while being as brief as possible.</p>
519547

520-
<p>Using ID and class names this way contributes to acceptable levels of
521-
understandability and code efficiency.</p>
548+
<p>Using class names this way contributes to acceptable levels of understandability
549+
and code efficiency.</p>
522550

523551
<pre><code class="language-css bad">/* Not recommended */
524-
#navigation {}
552+
.navigation {}
525553
.atr {}
526554
</code></pre>
527555

528556
<pre><code class="language-css good">/* Recommended */
529-
#nav {}
557+
.nav {}
530558
.author {}
531559
</code></pre>
532560

533-
<h4 id="ID_and_Class_Name_Delimiters" class="numbered">ID and Class Name Delimiters</h4>
561+
<h4 id="ID_and_Class_Name_Delimiters" class="numbered">Class Name Delimiters</h4>
534562

535-
<p>Separate words in ID and class names by a hyphen.</p>
563+
<p>Separate words in class names by a hyphen.</p>
536564

537565
<p>Do not concatenate words and abbreviations in selectors by any characters
538566
(including none at all) other than hyphens, in order to improve understanding
@@ -546,7 +574,7 @@ <h4 id="ID_and_Class_Name_Delimiters" class="numbered">ID and Class Name Delimit
546574
</code></pre>
547575

548576
<pre><code class="language-css good">/* Recommended */
549-
#video-id {}
577+
.video-id {}
550578
.ads-sample {}
551579
</code></pre>
552580

@@ -555,36 +583,52 @@ <h4 id="Prefixes" class="numbered">Prefixes</h4>
555583
<p>Prefix selectors with an application-specific prefix (optional).</p>
556584

557585
<p>In large projects as well as for code that gets embedded in other projects or on
558-
external sites use prefixes (as namespaces) for ID and class names. Use short,
559-
unique identifiers followed by a dash.</p>
586+
external sites use prefixes (as namespaces) for class names. Use short, unique
587+
identifiers followed by a dash.</p>
560588

561589
<p>Using namespaces helps preventing naming conflicts and can make maintenance
562590
easier, for example in search and replace operations.</p>
563591

564592
<pre><code class="language-css good">.adw-help {} /* AdWords */
565-
#maia-note {} /* Maia */
593+
.maia-note {} /* Maia */
566594
</code></pre>
567595

568596
<h4 id="Type_Selectors" class="numbered">Type Selectors</h4>
569597

570-
<p>Avoid qualifying ID and class names with type selectors.</p>
598+
<p>Avoid qualifying class names with type selectors.</p>
571599

572600
<p>Unless necessary (for example with helper classes), do not use element names in
573-
conjunction with IDs or classes.</p>
601+
conjunction with classes.</p>
574602

575603
<p>Avoiding unnecessary ancestor selectors is useful for
576604
<a href="http://www.stevesouders.com/blog/2009/06/18/simplifying-css-selectors/">performance reasons</a>.</p>
577605

578606
<pre><code class="language-css bad">/* Not recommended */
579-
ul#example {}
607+
ul.example {}
580608
div.error {}
581609
</code></pre>
582610

583611
<pre><code class="language-css good">/* Recommended */
584-
#example {}
612+
.example {}
585613
.error {}
586614
</code></pre>
587615

616+
<h4 id="ID_Selectors" class="numbered">ID Selectors</h4>
617+
618+
<p>Avoid ID selectors.</p>
619+
620+
<p>ID attributes are expected to be unique across an entire page, which is
621+
difficult to guarantee when a page contains many components worked on by many
622+
different engineers. Class selectors should be preferred in all situations.</p>
623+
624+
<pre><code class="language-css bad">/* Not recommended */
625+
#example {}
626+
</code></pre>
627+
628+
<pre><code class="language-css good">/* Recommended */
629+
.example {}
630+
</code></pre>
631+
588632
<h4 id="Shorthand_Properties" class="numbered">Shorthand Properties</h4>
589633

590634
<p>Use shorthand properties where possible.</p>
@@ -757,19 +801,19 @@ <h4 id="Declaration_Block_Separation" class="numbered">Declaration Block Separat
757801
rule.</p>
758802

759803
<pre><code class="language-css bad">/* Not recommended: missing space */
760-
#video{
804+
.video{
761805
margin-top: 1em;
762806
}
763807

764808
/* Not recommended: unnecessary line break */
765-
#video
809+
.video
766810
{
767811
margin-top: 1em;
768812
}
769813
</code></pre>
770814

771815
<pre><code class="language-css good">/* Recommended */
772-
#video {
816+
.video {
773817
margin-top: 1em;
774818
}
775819
</code></pre>
@@ -848,11 +892,11 @@ <h4 id="Section_Comments" class="numbered">Section Comments</h4>
848892

849893
<pre><code class="language-css good">/* Header */
850894

851-
#adw-header {}
895+
.adw-header {}
852896

853897
/* Footer */
854898

855-
#adw-footer {}
899+
.adw-footer {}
856900

857901
/* Gallery */
858902

0 commit comments

Comments
 (0)