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.
There are some rules and characteristics of attributes like how we should use an attribute on an HTML element or tag.
In the following example we will use attributes to define the alignment of <p> element using HTML align attribute.
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.
Global attributes are common to all HTML elements and can be used universally. Some of the most important global attributes include:
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.
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.
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 byteshastra.com
</p>
</body>
</html>
The style attribute allows you to specify Cascading Style Sheet (CSS) rules within the element.
There are three internationalization attributes, which are available for most (although not all) HTML Elements.
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). |
<
Example:
<!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>
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.
Example
<!DOCTYPE html>
<html lang=”en”>
<head>
<title>HTML lang Attribute</title>
</head>
<body>
This page is the English Language
</body>
</html>
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.byteshastra.com/demo.htm”>
HTML Introduction
<a>
<!– Bad Practice –>
<a href=https://www.byteshastra.com/demo.htm>
HTML Introduction
<a>
<!– Can use single and double Quotes –>
<p title=”We are known for ‘Simple Easy Learning'”>
byteshastra
</p>
<p title=’We are known for “Simple Easy Learning”‘>
byteshastra
</p>
Here’s a table of some other attributes that are readily usable with many of the HTML tags.
Attribute | Options | Function |
---|---|---|
align |
right, left, center | Horizontally aligns tags |
bgcolor |
numeric, hexadecimal, RGB values | Places a background color behind an element |
id |
User Defined | Names an element for use with Cascading Style Sheets. |
class |
User Defined | Classifies an element for use with Cascading Style Sheets. |
width |
Numeric Value | Specifies the width of tables, images, or table cells. |
height |
Numeric Value | Specifies the height of tables, images, or table cells. |
title |
User Defined | A text to display in a tool tip. |