- HTML Tutorial
- HTML - Home
- HTML - Introduction
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - Attributes
HTML attributes are used to provide additional information about the HTML elements, attributes are the reserved keywords. We have seen few HTML tags and their usage like <h1>, <h2>, <p>, <br>, <hr> and other tags. We used them so far in their simplest form, but most of the HTML tags can also have attributes, which are extra bits of information.
An attribute is used to define the characteristics of an HTML element and is placed inside the element's opening tag. All attributes are made up key & value paire.
<tag attribute="Value">...<tag>
As we can see attributes are used in the tag with name & value paire but there are some attributes that does not required any value like disabled, required, etc.
- Name: It holds the property we want to set. For example, the paragraph <p> element in the example carries an attribute whose name is align, which you can use to indicate the alignment of paragraph on the page.
- Value: It holds the value what we want the value of the property to be set and always put within quotations. The below example shows three possible values of align attribute: left, center and right.
Attribute names and attribute values are case-insensitive. However, the World Wide Web Consortium (W3C) recommends lowercase attributes/attribute values in their HTML recommendation.
HTML Attribute
There are some rules and characteristics of attributes like how we should use an attribute on an HTML element or tag.
- HTML provides additional information about the elements
- Attributes should always mention in the starting tag.
- All HTML elements can have attributes except a few like <head>, <title>, etc.
- W3C recommend to use attributes in lowercase and keep the value in quote.
HTML Attribute Example
In the following example we will use attributes to define the alignment of <p> element using HTML align attribute.
<!DOCTYPE html> <html> <head> <title>HTML Attribute Example</title> </head> <body> <p align="left">Left Aligned</p> <p align="center">Center Aligned</p> <p align="right">Right Aligned</p> </body> </html>
As you have seen the above code where we used an attribute called align and use three different value of align attribute to specify the alignment of the <p> element.
HTML Global Attributes
Global attributes are common to all HTML elements and can be used universally. Some of the most important global attributes include:
HTML id Attribute
The id attribute of an HTML tag can be used to uniquely identify any element within an HTML page. If you have two elements of the same name within a Web page (or style sheet), you can use the id attribute to distinguish between elements that have the same name.
We are using the id attribute to distinguish between two paragraph elements and styling it in CSS with id name.
<!DOCTYPE html> <html> <head> <title>HTML id Attribute</title> <style> #html{ color: red; } #html{ color:green; } </style> </head> <body> <p id="html"> This para explains what is HTML </p> <p id="css"> This para explains what is CSS </p> </body> </html>
HTML title Attribute
The title attribute gives a suggested title for the element. The syntax for the title attribute is similar as explained for id attribute.
The behavior of this attribute will depend upon the element that carries it, although it is often displayed as a tooltip when cursor comes over the element or while the element is loading.
<!DOCTYPE html> <html> <head> <title>HTML title Attribute</title> </head> <body> <h3 title="Hello HTML!"> Hover mouse here to see my title </h3> </body> </html>
HTML class Attribute
The class attribute is used to associate an element with a style sheet, and specifies the class of element. You will learn more about the use of the class attribute when you will learn Cascading Style Sheet (CSS).
The value of the attribute is a space-separated list of class names. For example
<!DOCTYPE html> <html> <head> <title>HTML class Attribute</title> <style> .borderStyle{ border: solid black 5px; } .colorStyle{ color:red; } </style> </head> <body> <p class="borderStyle colorStyle" > Welcome to Tutorialspoint... </p> </body> </html>
HTML style Attribute
The style attribute allows you to specify Cascading Style Sheet (CSS) rules within the element.
<!DOCTYPE html> <html> <head> <title>HTML style Attribute</title> </head> <body> <p style="font-family:arial; color:#FF0000;"> Welcome to Tutorialspoint... </p> </body> </html>
Internationalization Attributes
There are three internationalization attributes, which are available for most (although not all) HTML Elements.
HTML dir Attribute
The dir attribute allows you to indicate to the browser about the direction in which the text should flow. The dir attribute can take one of two values, as you can see in the table that follows.
Value | Meaning |
---|---|
ltr | Left to right (the default value). |
rtl | Right to left (for languages such as Hebrew or Arabic that are read right to left). |
<!DOCTYPE html> <html dir="rtl"> <head> <title>HTML dir Attribute</title> </head> <body> This is how IE 5 renders right-to-left directed text. </body> </html>
HTML lang Attribute
The lang attribute allows you to indicate the main language used in a document, but this attribute was kept in HTML only for backwards compatibility with earlier versions of HTML. This attribute has been replaced by the xml:lang attribute in new XHTML documents.
The values of the lang attribute are ISO-639 standard two-character language codes. Check HTML Language Codes: ISO 639 for a complete list of language codes.
<!DOCTYPE html> <html lang="en"> <head> <title>HTML lang Attribute</title> </head> <body> This page is the English Language </body> </html>
How to use HTML Attributes?
There are certain practices you should follow to use attributes on any element, please check below mentioned ways to do so.
<!-- Good Practice --> <a href="https://www.tutorialspoint.com/html/html_overview.htm"> HTML Introduction <a> <!-- Bad Practice --> <a href=https://www.tutorialspoint.com/html/html_overview.htm> HTML Introduction <a>
<!-- Can use single and double Quotes --> <p title="We are known for 'Simple Easy Learning'"> Tutorialspoint </p> <p title='We are known for "Simple Easy Learning"'> Tutorialspoint </p>
Generic Attributes
Here's a table of some other attributes that are readily usable with many of the HTML tags.
Attribute | Options | Function |
---|---|---|
right, left, center | Horizontally aligns tags | |
numeric, hexadecimal, RGB values | Places a background color behind an element | |
User Defined | Names an element for use with Cascading Style Sheets. | |
User Defined | Classifies an element for use with Cascading Style Sheets. | |
Numeric Value | Specifies the width of tables, images, or table cells. | |
Numeric Value | Specifies the height of tables, images, or table cells. | |
User Defined | A text to display in a tool tip. |
We will see related examples as we will proceed to study other HTML tags. For a complete list of HTML Tags and related attributes please check reference to HTML Tags List & HTML Attributes.