?? HTML Tips: Global vs. Custom Attributes ??

?? 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).

  • html

Copy code
<div id="main-header"></div>        
class: Group elements for CSS or JavaScript.        

  • html

Copy code
<p class="highlight">Styled Text</p>        
title: A little tooltip when you hover.        

  • html

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:

  • html

Copy code
<button data-user-id="007">Click Me</button>        

Save a theme preference:

  • html

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        
Exloring Basics

? TakeaWhy 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        

? 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:


Suraj Sharma

Full-Stack Web Developer || SEO

2 个月

I like using “id” I can target individual element using JavaScript,CSS or both.

Chhaya Kumari

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">

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了