id and class Attributes — HTML Technical Interview Questions & Answers

Unique vs reusable hooks for CSS and JavaScript.

id and class Attributes

  • Unique vs reusable hooks for CSS and JavaScript.
  • id and class Attributes is an HTML attribute — extra info on the opening tag. Example: class, id, href, src, alt, for, type. <p class="fail">Fail</p>
  • id is unique on the page. class can repeat. Don’t use the same id twice. Prefer class for CSS.
  • Trap: styling with id for everything. Trap: inline style when a class would do.

Technical Interview Tip: id and class Attributes: show it on a tag. id vs class if relevant.

Code Examples for id and class Attributes

id and class Attributes — working example

<section id="faq" class="panel panel--wide" data-topic="html">
  <h2>FAQ</h2>
  <p hidden>Screened until revealed</p>
  <p contenteditable="true">Editable note</p>
</section>

id and class Attributes — id is unique; class is reusable; data-* stores custom values for CSS/JS hooks.

How to Explain id and class Attributes to an Interviewer

What is id and class Attributes?

An HTML attribute Goes on the opening tag