E-Content of
INTERNET TECHNOLOGY AND WEB DESIGN
Chapter : 9.3 HTML Attributes
Topic : 9.3.1 HTML Attributes
HTML Attributes
• An attribute is used to define the characteristics of an HTML element and is
placed inside the element's opening tag.
FIG 9.4: Attributes of HTML Program
• Attributes names and attribute values are case sensitive. However, the World
Wide Web consortium (W3C) recommends lowercase attributes/attribute values
in their HTML4 recommendation.
• All attributes are made up of two parts. They are,
Name
Value
• Name Attribute
The Name is the property you want to set. For example, the paragraph <p>
element in the example carries an attribute whose name is align, in which
you can use to indicate the alignment of paragraph on the page.
• Value Attribute
The Value is what you want the value of the property to be set and always
put within quotations.
The below example shows three possible values of align attribute. There are
such as,
Left
Page | 23
E-Content of
INTERNET TECHNOLOGY AND WEB DESIGN
Right
Center
• Example program
• Output
Core Attributes
• The four core attributes that can be used on the majority of HTML elements
(although not all) are,
id
title
class
style
• The id attribute
The id attribute of an HTML tag can be used to uniquely identify any element
within an HTML page. There are two primary reasons that you might want to
use an id attribute on an element.
If an element carries an id attribute as a unique identifier it is possible to
identify just that element and its content.
Page | 24
E-Content of
INTERNET TECHNOLOGY AND WEB DESIGN
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.
Example
• The 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.
Example Program
Output
• The 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 may also be a space separated list of class names.
Example program
Page | 25
E-Content of
INTERNET TECHNOLOGY AND WEB DESIGN
• The style attribute
The style attribute allows you to specify cascading style sheet (CSS) rules
within the element.
Example program
Output
At this point of time, we are not learning CSS, so just let's proceed without
bothering much about CSS.
Here you need to understand what are HTML attributes and how they can be
used while formatting content.
Internationalization Attributes
• There are three internationalization attributes, which are available for most
(although not all) XHTML elements.
dir
lang
xml:lang
• The dir attribute
The dir attribute allows you to indicate to the browser 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.
Page | 26
E-Content of
INTERNET TECHNOLOGY AND WEB DESIGN
Value Meaning
ltr Left to right (default value)
rtl Right to left (for languages such as Hebrew or Arabic that are
read right to left.
FIG 9.5: The dir attribute value
Example Program
Output
When dir attribute is used within the <html> tag, it determines how text will be
presented within the entire document.
When used within another tag, it controls the text's direction for just the
content of that tag.
• The 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.
Example Program
Page | 27
E-Content of
INTERNET TECHNOLOGY AND WEB DESIGN
• The xml:lang Attribute
The xml:lang attribute is the XHTML replacement for the lang attribute.
The value of the xml:lang attribute should be an ISO-639 country code as
mentioned in previous section.
Generic Attributes
• Here's a table of some other attributes that are readily usable with many of the
HTML tags.
Page | 28