?? HTML Tips: Global vs. Custom Attributes ??
Ever wondered how websites store extra data or make elements interactive? Let’s break it down:
Global Attributes
These are like the default settings—you can use them on ANY HTML element.
?? Examples:
id: A unique name for an element (like a label).
Copy code
<div id="main-header"></div>
class: Group elements for CSS or JavaScript.
Copy code
<p class="highlight">Styled Text</p>
title: A little tooltip when you hover.
Copy code
<img src="photo.jpg" title="Cute Puppy">
Custom Attributes (data-*)
These are your DIY attributes—add them to store extra data.
?? Examples:
Track a user’s ID:
Copy code
<button data-user-id="007">Click Me</button>
Save a theme preference:
Copy code
<input type="checkbox" data-theme="dark" checked>
?? Why use them?
You can access these custom attributes in JavaScript for extra functionality:
javascript
Copy code
const userId = document.querySelector('[data-user-id]').dataset.userId;
console.log(userId); // Outputs: 007
? TakeaWhy use them?
You can access these custom attributes in JavaScript for extra functionality:
Copy code
const userId = document.querySelector('[data-user-id]').dataset.userId;
console.log(userId);
// Outputs: 007
? Takeaway:
Global attributes = universal tools.
Custom attributes = make your code smarter and more dynamic!
What’s one HTML trick you love using? Let’s chat in the comments! ??
#HTML #WebDevMadeSimple #LearnAndGrow #CodingTipsway:
Full-Stack Web Developer || SEO
2 个月I like using “id” I can target individual element using JavaScript,CSS or both.
BCA '24 | MCA '26 | Junior Frontend Developer @Cronix | MERN Enthusiast
2 个月One HTML trick I love using is the placeholder attribute within <input> elements. It provides a subtle hint to the user about the expected input, enhancing the user experience without being intrusive. ?? HTML <input type="text" placeholder="Enter your name">