Course List
What are HTML Attributes?
Attributes provide additional information about an HTML element. They are always written inside the opening tag (or the self‑closing tag) and usually come in name‑value pairs.
Example:<a href="https://example.com">Visit Example</a>
hrefis the attribute name."https://example.com"is the attribute value.The attribute tells the browser where the link should go.
Basic Syntax
The general pattern for an attribute is:
<tag attribute="value">content</tag>
The attribute name is written in lowercase (recommended, though HTML is case‑insensitive).
The value is placed inside double quotes (single quotes are also allowed, but double quotes are the convention).
Some attributes can appear without a value – these are called boolean attributes.
Example with multiple attributes
<img src="photo.jpg" alt="A beautiful sunset" width="500" height="300">
srcspecifies the image file.altprovides a text description (important for accessibility).widthandheightdefine the image dimensions.
Common HTML Attributes
| Attribute | Used on elements | Purpose |
|---|---|---|
href | <a>, <link> | Specifies the URL of a link |
src | <img>, <script>, <iframe> | Specifies the source file (image, script, etc.) |
alt | <img>, <area> | Provides alternative text for images |
id | All elements (global) | Uniquely identifies an element on the page |
class | All elements (global) | Assigns one or more class names for CSS or JavaScript |
style | All elements (global) | Adds inline CSS styles |
title | All elements (global) | Provides a tooltip when the mouse hovers over the element |
type | <input>, <button>, <script> | Defines the type of the element (e.g., text, submit, checkbox) |
value | <input>, <button>, <option> | Specifies the initial value of a form control |
disabled | <input>, <button>, <select> | Boolean attribute; disables the element |
Boolean Attributes
A boolean attribute does not require a value. Its presence means true, and its absence means false.
You can write it in three ways (the first is most common in HTML5):
disabled(only the attribute name)disabled=""(empty value)disabled="disabled"
Example:<input type="text" disabled> – the input field is disabled and cannot be edited.
Other boolean attributes: readonly, checked, required, hidden, multiple, selected.
Global Attributes
Global attributes can be used on any HTML element (though some may have no effect on certain elements).
Important global attributes:
id– unique identifier (must be unique per page).class– space‑separated list of class names.style– inline CSS.title– tooltip text.lang– language of the element’s content (e.g.,lang="en").hidden– boolean; hides the element from view.tabindex– sets the tab order for keyboard navigation.contenteditable– boolean; allows the user to edit the element’s content.data-*– custom data attributes (see below).
Custom Data Attributes
You can create your own attributes prefixed with data-. They store private data for use in JavaScript or CSS, without affecting the page’s rendering.
Example:<div data-user-id="12345" data-role="admin">John Doe</div>
In JavaScript, you can access them via element.dataset.userId or element.dataset.role.
Data attributes are especially useful in dynamic web applications.
Rules and Best Practices
Always use lowercase for attribute names – it is a convention (HTML5 is case‑insensitive but lowercase is cleaner).
Quote attribute values – use double quotes (
") for consistency.Do not omit required attributes – e.g., the
altattribute for<img>is required for accessibility. If the image is purely decorative, usealt=""(empty).Use boolean attributes without a value – e.g., write
disabled, notdisabled="false"(which would actually mean true because any value makes it true).Separate multiple attributes with spaces – order does not matter.
Do not add duplicate attributes – only the first one is used by the browser.
Attribute vs. Property
In HTML, attributes are written in the markup. When the browser parses the page, it creates corresponding DOM properties for many attributes. Sometimes attributes and properties have different names or behaviors (e.g., class attribute vs. className property). However, for basic HTML authoring, you only need to know the attribute syntax.
Summary
HTML attributes are added inside the opening tag.
They usually appear as
name="value".Boolean attributes can be written without a value.
Global attributes like
id,class,stylework on all elements.Custom data attributes (
data-*) let you store extra information.Following best practices (lowercase, quotes, meaningful values) keeps your HTML clean and maintainable.
HTML Practice
We will provide some examples and online editors in each chapter to help you better learn HTML!
Example
<!DOCTYPE html>
<html>
<title>Hello World </title>
<body>
<h1>My First Title</h1>
<p>My First Paragraph. </p>
</body>
</html>
