1. HTML Basics
Estimated reading: 9 minutes 9 views

๐Ÿ“š Complete Guide to HTML Attributes with Real-World Examples

HTML attributes are essential for adding meaning, behavior, and structure to your webpages. This guide covers the most important ones, categorized for easy learning and real-world application.

๐Ÿ“š Complete HTML Attributes Table with Real-World Examples

๐Ÿ”ง Global Attributes

AttributeDescriptionExample
idUnique identifier<div id="main-header">Welcome</div>
classGroup for styling<p class="error-text">Invalid login.</p>
styleInline CSS<h1 style="color: blue;">Styled Heading</h1>
titleTooltip on hover<span title="More Info">Hover me</span>
data-*Custom data for JS<div data-user-id="789">Profile</div>
hiddenHides element<div hidden>Secret Note</div>
tabindexKeyboard nav order<input tabindex="2">
contenteditableEditable content<p contenteditable="true">Edit this</p>
draggableDrag and drop<img draggable="true">
langLanguage declaration<p lang="fr">Bonjour</p>

๐Ÿงพ Form & Input Attributes

AttributeDescriptionExample
actionForm submission URL<form action="/register">
methodPOST or GET method<form method="POST">
nameInput identifier<input name="user_email">
valueDefault value<input value="Jane">
placeholderInput hint<input placeholder="Enter name">
requiredMandatory input<input required>
disabledNon-editable input<input disabled value="N/A">
readonlyView-only input<input readonly value="Locked">
autocompleteAutofill control<input autocomplete="off">
min / maxInput range<input type="number" min="1" max="10">
stepInput increments<input type="number" step="2">
patternRegex validation<input pattern="\d{4}">
multipleMultiple values<input type="file" multiple>

๐ŸŽฌ Media Attributes

AttributeDescriptionExample
srcFile path<video src="clip.mp4"></video>
altImage description<img alt="Sleeping cat">
width, heightDimensions<img width="100">
controlsPlayback UI<video controls>
autoplay, loopAuto behavior<video autoplay loop muted>
mutedNo sound<audio muted autoplay>
posterVideo placeholder<video poster="thumb.jpg">

๐Ÿ”— Anchor & Link Attributes

AttributeDescriptionExample
hrefURL destination<a href="https://example.com">Visit</a>
targetOpen in new tab<a target="_blank">New Tab</a>
downloadForce file download<a href="cv.pdf" download>Get CV</a>
relLink relationship<a rel="noopener">Secure</a>
typeMIME type<link type="text/css">

๐Ÿ“Š Table Attributes

AttributeDescriptionExample
colspanMerge columns<td colspan="2">Spanned</td>
rowspanMerge rows<td rowspan="2">Grouped</td>
scopeHeader scope<th scope="row">Name</th>

โš™๏ธ Script Attributes

AttributeDescriptionExample
asyncLoad script async<script async src="main.js"></script>
deferLoad after HTML<script defer src="main.js"></script>
typeScript type<script type="module">

๐Ÿ“ˆ SEO & Meta Attributes

AttributeDescriptionExample
charsetCharacter encoding<meta charset="UTF-8">
viewportMobile scaling<meta name="viewport" content="width=device-width, initial-scale=1.0">
name + contentMeta info<meta name="description" content="HTML guide">

โ™ฟ ARIA (Accessibility) Attributes

AttributeDescriptionExample
roleSemantic purpose<nav role="navigation">
aria-labelLabel for screen readers<button aria-label="Close">X</button>
aria-hiddenHide from screen readers<div aria-hidden="true"></div>

โŒ Deprecated Attributes (Avoid)

AttributeDescriptionExample
borderOld table border<table border="1"> <!-- Use CSS -->
alignOld alignment<td align="center"> <!-- Use CSS -->


๐Ÿ”ง Global HTML Attributes

id โ€“ Unique Identifier

Assigns a unique ID to an element, ideal for scripting or styling.

<div id="main-header">Welcome</div>

Example: A navigation script can target #main-header for dynamic updates.

class โ€“ Reusable Styling

Groups elements with the same styles or functions.

<p class="error-text">Invalid login.</p>

Example: Multiple elements can share .error-text for red warnings.

style โ€“ Inline Styling

Applies CSS directly on an element.

<h1 style="color: blue;">Styled Heading</h1>

Example: Use sparingly for quick design adjustments.

title โ€“ Tooltip Text

Displays extra information on hover.

<span title="More Info">Hover me</span>

Example: Enhances user experience with hints.

data-* โ€“ Custom Data Storage

Stores metadata for JavaScript use.

<div data-user-id="789">Profile</div>

Example: Dynamic UIs can fetch values from these attributes.

hidden โ€“ Invisible Element

Hides elements from view and screen readers.

<div hidden>Secret Note</div>

Example: Revealed conditionally via JavaScript.

tabindex โ€“ Keyboard Navigation

Defines the tabbing order of elements.

<input type="text" tabindex="2">

Example: Improve accessibility in forms.

contenteditable โ€“ Inline Editing

Makes any element editable by the user.

<p contenteditable="true">Edit this text</p>

Example: Useful for simple CMS interfaces.

draggable โ€“ Drag and Drop

Enables drag behavior.

<img src="logo.png" draggable="true">

Example: Create interactive UIs like Kanban boards.

lang โ€“ Language Declaration

Specifies the content’s language.

<p lang="fr">Bonjour</p>

Example: Helps with SEO and accessibility tools.


๐Ÿงพ Form and Input Attributes

action โ€“ Form Destination

Sets the URL for form data submission.

<form action="/register">

Example: Sends data to your backend.

method โ€“ Submission Type

Specifies POST or GET for data submission.

<form method="POST">

Example: Use POST for secure data transmission.

name โ€“ Input Identifier

Key for identifying submitted data.

<input name="user_email">

Example: Paired with a server-side script.

value โ€“ Default Input Value

Sets a pre-filled value.

<input type="text" value="Jane">

Example: Useful in editable forms.

placeholder โ€“ Input Hint

Displays hint inside empty input.

<input placeholder="Enter your name">

Example: Enhances form usability.

required โ€“ Mandatory Field

Ensures the field is filled before submission.

<input required>

Example: Avoids incomplete forms.

disabled โ€“ Non-editable Input

Disables input entirely.

<input disabled value="N/A">

Example: Show uneditable values like user ID.

readonly โ€“ View Only Input

Disables editing, but submits value.

<input readonly value="Locked">

Example: Prevents changes to auto-filled fields.

autocomplete โ€“ Autofill Control

Toggles browser autofill.

<input autocomplete="off">

Example: Use off for sensitive fields.

min and max โ€“ Range Limit

Controls numeric input range.

<input type="number" min="1" max="10">

Example: Age input from 1 to 10.

step โ€“ Increment Control

Defines stepping interval.

<input type="number" step="2">

Example: Only allows even numbers.

pattern โ€“ Regex Validation

Validates input format.

<input pattern="\d{4}">

Example: Forces four-digit input like a PIN.

multiple โ€“ Multiple Values

Allows multi-selection.

<input type="file" multiple>

Example: Upload several files at once.


๐ŸŽฌ Media Attributes

src โ€“ Source Path

Points to a media file.

<video src="clip.mp4"></video>

alt โ€“ Image Description

Accessibility text for images.

<img src="cat.jpg" alt="Black kitten sleeping">

width and height โ€“ Media Size

Controls display dimensions.

<img src="icon.png" width="100">

controls โ€“ Media Controls

Adds play, pause, etc.

<video controls></video>

autoplay and loop โ€“ Auto Behavior

Starts and repeats media.

<video autoplay loop muted></video>

muted โ€“ No Sound

Disables audio on load.

<audio muted autoplay src="bg-music.mp3"></audio>

poster โ€“ Preview Image

Sets placeholder before play.

<video poster="thumb.jpg" controls></video>

๐Ÿ”— Anchor & Link Attributes

href โ€“ Link Target

Destination of the link.

<a href="https://example.com">Visit</a>

target โ€“ Open Behavior

Defines where the link opens.

<a target="_blank">New Tab</a>

download โ€“ File Download

Forces file to download.

<a href="cv.pdf" download>Get CV</a>

rel โ€“ Link Relationship

Describes the relation.

<a rel="noopener">Secure Link</a>

type โ€“ Content Type

Defines file MIME type.

<link rel="stylesheet" type="text/css" href="style.css">

๐Ÿ“Š Table Attributes

colspan โ€“ Merge Columns

<td colspan="2">Spanned</td>

rowspan โ€“ Merge Rows

<td rowspan="2">Grouped</td>

scope โ€“ Header Scope

<th scope="row">Name</th>

โš™๏ธ Script Attributes

async โ€“ Load Asynchronously

<script src="main.js" async></script>

defer โ€“ Load After Parsing

<script defer src="main.js"></script>

type โ€“ Script Type

<script type="module"></script>

๐Ÿ“ˆ SEO & Meta Attributes

charset โ€“ Character Encoding

<meta charset="UTF-8">

viewport โ€“ Mobile Scaling

<meta name="viewport" content="width=device-width, initial-scale=1.0">

name + content โ€“ Meta Info

<meta name="description" content="Best HTML attribute guide">

โ™ฟ ARIA Accessibility Attributes

role โ€“ Semantic Role

<nav role="navigation"></nav>

aria-label โ€“ Label for Assistive Tech

<button aria-label="Close">X</button>

aria-hidden โ€“ Hide from Screen Readers

<div aria-hidden="true"></div>

โŒ Deprecated Attributes (Do Not Use)

<table border="1"> <!-- Use CSS -->
<td align="center"> <!-- Use text-align CSS -->

๐Ÿ” HTML in Action

<form action="/send" method="POST">
<input type="email" placeholder="Email" required autocomplete="on">
<button type="submit" title="Submit Form">Submit</button>
</form>

<img src="logo.png" alt="Brand Logo" width="100">
<a href="guide.pdf" download target="_blank" rel="noopener">Download Guide</a>

โœ… Conclusion

Understanding HTML attributes helps you build responsive, functional, and accessible websites. Use them wisely to level up your web development skills.


โ“FAQs

Q1: Can data-* attributes be used in JavaScript?
Yes. Access them using element.dataset.

Q2: Is readonly same as disabled?
No. readonly allows data submission. disabled doesn’t.

Q3: Why use rel="noopener"?
It prevents security risks when opening links in new tabs.

Q4: Are inline styles better than CSS files?
No. Use inline styles only for quick edits.

Q5: Do ARIA attributes affect SEO?
Not directly, but they boost accessibility and user experience.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc

HTML Attributes

Or copy link

CONTENTS
Scroll to Top