HTML attributes
HTML attributes provide additional information about HTML elements.
- All HTML elements can have Attributes.
- Attributes provide additional information about the elements.
- Attributes are always included in the Start tag.
-
Attributes usually come in pairs of
name/value
such as:attributename="value"
Here are some attributes, understand them:
The style attribute
The style
attribute is used to add style ie. additional information to an element, such as color, font size, etc.
The lang Attribute
lang
attribute you should always include the lang
attribute within the <html>
tag to declare the language of the web pages. It aims to help search engines and browsers understand the language of the page.
The following example illustrates English as a page language:
<html lang="en">
<body>
...
</body>
</html>
The "href" attribute
The <a>
tag defines a hyperlink. href
attribute specifies the URL of the page to which the link goes:
The class attribute
The class
attribute allows space to add one or more class names to an element, referring to the class in the style sheet.
The following example adds two classes using the class attribute p-urdu
and text-center
:
The title Attribute
The title
attribute describes some additional information about an element.
When you hover the mouse over the element, the title
attribute will appear as the value
tooltip:
Hover the mouse over the following text, the tooltip will appear:
We went to the market.
The "src" attribute
One of the HTML attributes is the src
attribute that is inserted within the <img>
tag using the <img>
tag. HTML is used to embed an image in a page. The src
attribute shows the path of the displayed image:
The "width" and "height" attributes
The <img>
tags also contain the width
attribute and the height
attribute, which define the width and height of the image in pixels:
The "alt" attribute
The alt
attribute required for the <img>
tag defines an alternate text for an image, if for some reason the image cannot be displayed. This could be due to an error in the behavior, or an error in the src
attribute, or if the user uses a screen reader, then an alternative text appears on the screen.
You will learn more about images in our HTML Images chapter.
Here are a few attributes to clarify HTML attributes, you will learn a lot about HTML attributes and their features in the next tutorial.