HTML elements look like this:
<tagname attribute="value">content</tagname>Examples:
<a href="https://example.com">Link</a>
<img src="photo.jpg" alt="Description">Let’s break that down:
- Element (tag) →
a,img,div,p, etc. - Attribute → extra information that changes behavior or provides data
Examples of what attributes do:
href→ tells<a>where to gosrc→ tells<img>where the image isalt→ describes the image for accessibilitytype→ changes how inputs or buttons behave
So mentally:
Element = what it is
Attribute = extra information about it
These work only on certain elements:
href→<a>,<link>src→<img>,<script>,<iframe>type→<input>,<button>,<script>alt→<img>
Using them on the wrong tag is invalid HTML.
Global attributes can be used on almost any HTML element.
Examples:
idclassstyledata-*hiddentabindextitlelangcontenteditabledraggable
Think of global attributes as:
“Useful powers you can attach to almost any tag.”
<p id="intro">Hello, I am Himanshu.</p>- A unique name for one specific element
- Must be unique in the entire HTML document
Think of id like:
a passport number — only one person can have it
<p id="intro">Hello, world</p>#intro {
font-size: 1.25rem;
color: #1f2933;
}#intromeans “the element whose id is intro”- IDs have high CSS specificity
- Use sparingly for styling
const intro = document.getElementById('intro');
intro.textContent = 'Updated text';Very common:
- JS frequently selects elements using
id - Fast and direct access
<h2 id="contact">Contact</h2>
<a href="#contact">Go to Contact section</a>href="#contact"scrolls to the element withid="contact"- This is how in-page navigation works
- Use each
idonly once per page - Use descriptive names:
main-headerhero-sectioncontact-section
- Start with letters, then letters/numbers/hyphens
- Don’t use ids everywhere — prefer classes for styling
<p class="highlight">Important note</p>
<p class="highlight">Another important thing</p>- Not unique
- Many elements can share the same class
- One element can have multiple classes
<div class="card featured"></div>Here:
card→ base stylefeatured→ extra behavior or style
.highlight {
background-color: yellow;
}
.card {
padding: 1rem;
border-radius: 0.5rem;
}
.featured {
border-color: orange;
}You will use classes for 90% of your styling.
const cards = document.querySelectorAll('.card');
cards.forEach(card => {
// do something with each card
});| Feature | id | class |
|---|---|---|
| Unique | ✅ yes | ❌ no |
| Reusable | ❌ no | ✅ yes |
| CSS usage | Rare | Very common |
| JS usage | Direct lookup | Group selection |
id→ identityclass→ category
<p style="color: red; font-weight: bold;">
This is important.
</p>- CSS written directly on the element
- Called inline CSS
- Hard to maintain
- Hard to override
- Not reusable
- Breaks separation of concerns
<p class="important">This is important.</p>.important {
color: red;
font-weight: bold;
}- Quick learning experiments
- Small dynamic styles set by JS
- Tiny one-off tweaks (rare)
<div
class="product-card"
data-product-id="123"
data-category="books"
>
Book Title
</div>- Custom attributes starting with
data- - Valid HTML
- Invisible to users
- Designed for JavaScript
Old (bad) way:
<div my-custom-info="123"></div> ❌Correct way:
<div data-info="123"></div> ✅const card = document.querySelector('.product-card');
console.log(card.dataset.productId); // "123"
console.log(card.dataset.category); // "books"Mapping rule:
data-user-name→dataset.userName
- Filtering
- UI behavior
- State flags
<button data-action="open-modal">Open</button>
<button data-action="close-modal">Close</button><html lang="en">- Tells screen readers how to pronounce text
- Helps search engines
- Improves accessibility
Example for Hindi:
<html lang="hi"><p>
This phrase is in <span lang="fr">français</span>.
</p>Screen reader switches pronunciation automatically.
- Always set
langon<html> - Use correct language codes:
en,en-US,hi,fr, etc.
<button title="Saves your changes">Save</button>- Shows tooltip on mouse hover
- Sometimes read by screen readers
titleis not a replacement for labels- Not reliable on mobile
- Not fully accessible alone
Better:
- Clear visible text
aria-labelfor icon-only elementstitleonly as optional extra info
8. hidden – hide elements completely
<p hidden>This text is hidden</p>- Removes element from visual view
- Removed from accessibility tree
- Same as
display: none
Use cases:
- Toggling UI with JS
- Conditional content
<div tabindex="0">Focusable box</div>tabindex="0"→ focusable in normal ordertabindex="-1"→ focusable only via JStabindex="1+"→ ❌ avoid (breaks natural order)
Use sparingly, mostly for:
- Custom components
- Skip links
<p contenteditable="true">Edit me</p>- Allows users to edit text directly
- Used in rich text editors
- Changes are not saved unless JS handles them
<div draggable="true">Drag me</div>- Enables drag-and-drop behavior
- Needs JS to be useful
- Rare in beginner projects
| Attribute | Purpose |
|---|---|
| id | Unique identifier |
| class | Reusable grouping |
| style | Inline CSS |
| data-* | Custom JS data |
| lang | Language info |
| title | Tooltip text |
| hidden | Hide element |
| tabindex | Keyboard navigation |
| contenteditable | Editable content |
| draggable | Drag support |
- Attributes = extra information
- Global attributes = available everywhere
- Prefer:
classfor stylingidfor unique identity & JS- semantic HTML first
- Avoid overusing:
style- random
tabindex
- Accessibility matters more than visuals