HTML Attributes
Written By: Avinash Malhotra
Updated on
Attributes
HTML elements can have attributes in the opening or start tag. Attributes provide additional information about that element. HTML attributes are added to the opening tag and can have a value. Some popular global attributes are class, id, title, style, etc.
HTML attributes are always defined in the start tag. Attributes are written in name/value pairs like: <tag attribute="value">. An attribute can be used only once in the same element.
For attributes like class, multiple values can be assigned separated by spaces, but the class attribute itself appears only once per tag.
Previously, many presentational attributes were used for styling, like align, valign, bgcolor, etc. However, in HTML5, these attributes have been removed. CSS should be used instead for styling.
HTML Attributes Type
HTML Attributes are categorized on the basics of their functionality and the way they are written in HTML.
| Attribute Type | Meaning |
|---|---|
| Global Attribute | Attributes meant for all HTML elements. E.g.: class, id |
| Boolean Attribute | Attribute with attribute name only, no value. E.g.: disabled, hidden, reversed etc. |
| Presentational Attributes | Attributes used to style HTML Element. For example : style, size, color, border, cellspacing, cellpadding etc. |
| Event Attributes | Event attributes are used to add javascript event on html element, like onclick, onmouseover, onmouseout, etc. |
Except for style and size, all presentational attributes have been removed in HTML5 in favor of CSS.
HTML Attributes List
Here is a list of HTML attributes with their name, description, and how to use them. These are popular HTML attributes.
HTML Attributes
| Attribute | Element | Description |
|---|---|---|
| class | Global Attribute | used to group elements for css |
| id | Global Attribute | Assign a unique id for the element |
| name | Global Attribute | Assign name of an element |
| style | Global Attribute | CSS attribute for tags (Specifies an inline style for an element) |
| title | Global Attribute | Specifies extra information about an element (displayed as a tooltip) |
| lang | Global Attribute | Language of text |
| dir | Global Attribute | direction of text, ltr or rtl only |
| data- | Global Attribute | data attributes are used in html5 to define custom attribute |
| type | <input>, <audio>, <video>,
<embed>, <iframe>, <source>, <script>,
<track>, <ol> |
Defines type of element. |
| src | <img>, <script>, <input>,
<audio>, <video>, <embed>,
<iframe>, <source>, <track> |
source of media element |
| href | <a>, <link>, <base> |
hypertext reference, path of hyperlink ( <a>) and link tag |
| tabindex | <a>, <input>, <select>, <button>,
<select>, <textarea> |
to override default tab order and follow the specific one. |
| width | <img>, <embed>, <video>, <iframe> |
Set width of element |
| height | <img>, <embed>, <video>, <iframe> |
Set height of element |
| value | <input>, <option>, |
Set default value of element visible at pageload |
| size | <input>, <select> |
Width of element in px. For input type text, size means no of characters |
Attributes removes in HTML5
| bgcolor | Background Color of the document
(Removed in HTML5) |
<body bgcolor="aqua"> |
| background | Background Image of the document
(Removed in HTML5) |
<body background="img/bg.png"> |
| border | To set border of table element.
(Removed in HTML5) |
<table border="1"> </table> |
| align | Align left, right, center
(Removed in HTML5) |
<p align="center"> |
| valign | Align top, middle, bottom
(Removed in HTML5) |
<p valign="middle"> |
| cellspacing | change space (margin) between table cells
(Removed in HTML5) |
<table cellspacing="10px"> |
| cellpadding | inner space ( padding) between table cells
(Removed in HTML5) |
<table cellpadding="10px"> |
| bottommargin | Margin from bottom
(Removed in HTML5) |
<body bottommargin="20"> |
| topmargin | Margin from top
(Removed in HTML5) |
<body topmargin="10"> |
Class Attribute
Class attribute is used to define a group of elements having same CSS properties. Class can have single or multiple values with whitespace separation. With CSS, use of class attribute is compulsory. Multiple elements can share the same class.
Class name should be descriptive. For example, for a top-most div, a preferred class name is header. For navigation, use nav; for main content, main; and for the last div, footer.
Class Attribute Example
Font with class red and bold
Font with class red
Font without any class
<style>
p{ font-family:sans-serif}
.red{ color:red}
p.bold{ font-weight:bold}
</style>
<p class="red bold"> Font with class red and bold</p>
<p class="red"> Font with class red only</p>
<p> Font without any class name</p>
ID Attribute
Id attribute is used to call a unique element. In a single webpage, an id name is always used once. An HTML element can have a single ID attribute and single value of id. As per W3C, using the same id on multiple elements is a W3C validation error.
ID Attribute Example
Font with class red
Font with class red
Font with id blue
<style>
.red{ color:red}
#blue{ color:blue}
</style>
<p class="red" > Font with class red</p>
<p class="red" > Font with class red</p>
<p id="blue"> Font with id blue</p>
Title Attribute
HTML Title Attribute is a global attribute used to show a tooltip on mouse hover. Title Attribute is also compulsory for the <abbr> tag to provide the full expansion of abbreviations.
<abbr title="Hypertext Markup Language">HTML</abbr>
Tags where title attribute is recommended
<abbr>- For abbreviations<iframe>- For describing embedded content<input>- For additional help text<img>- For image descriptions (though alt is preferred)
Size Attribute
Size Attribute defines the size for the input tag (number of characters visible) and was used for the font tag (deprecated). Since the font tag is removed in HTML5, the size attribute is now primarily used for input elements to control their width.
<input type="text" size="6" >
<input type="text" size="10" >
<input type="text" size="20" >
<input type="text" size="30" >
Bgcolor
( removed in html5)
Used to give background color to body tag and table tag.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body bgcolor="#ff0033">
// content
</body>
</html>
Background
( removed in html5)
Used to give background image in HTML4/XHTML for body element. In HTML5, background-image is used instead.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body background="img/bg.png">
// content
</body>
</html>
Align attribute
( removed in html5)
Align Attribute was used to horizontally align text or tables. It was also used to align other HTML elements like HTML table.
In HTML5, use text-align property in CSS.
TEXT Aligned Left
TEXT Aligned Center
TEXT Aligned Right
<h1 align="left">TEXT Aligned Left</h1>
<h1 align="center">TEXT Aligned Center</h1>
<h1 align="right">TEXT Aligned Right</h1>
Summary
HTML attributes enhance HTML elements by providing additional information or functionality. Global attributes like class and id are essential for styling and scripting. Remember to use CSS for presentation instead of deprecated attributes, and ensure IDs are unique for valid HTML.
Frequently Asked Questions
Q: Can I use multiple classes on one element?
A: Yes, separate them with spaces, e.g., class="class1 class2".
Q: Why are some attributes removed in HTML5?
A: To separate content from presentation; use CSS for styling.
Q: Is the title attribute required?
A: Not always, but recommended for accessibility, especially on abbr tags.