HTML attributes offer further details about HTML elements.
- Every element in HTML can have attributes.
- Additional information about elements is provided by attributes.
- Always specify attributes in the start tag.
- The standard format for attributes is name=”value”.
The href Attribute
A hyperlink is defined using the
<a> tag. The URL of the page to
which the link points is specified by the href attribute:
The src Attribute
An image can be included in an HTML page using the
<img> tag. The path to the image
that will be shown is specified by the src attribute:
The URL can be specified in the src property in one of two ways:
1. An absolute URL directs users to an external image that is stored on a
different website. src=” https://www.technicalmrstar.com/
images/demo_image.jpg” is an example.
Note: Copyright may apply to external
photos. You might be breaking copyright laws if you use it without
authorization. Additionally, you have no control over external photos; they
could be changed or withdrawn at any time.
2. Relative URL – Directs the user to an image hosted on the website. Here,
the domain name is absent from the URL. The URL will be relative to the
current page if it starts out without a slash. For instance,
src=”demo_image.jpg”. The URL will be relative to the domain if it starts
with a slash. Src=”/images/demo_image.jpg” is an example.
Use relative URLs almost always for optimum results. If you switch domains,
they won’t stop working.
The width and height Attributes
The width and height attributes, which describe the width and height of the
image (in pixels), should also be included in the
The alt Attribute
If an image cannot be displayed for some reason, the mandatory alt property
for the <img> tag gives an
alternative text for the picture. If the user utilises a screen reader or
has a slow connection, an error in the src attribute, or both, could be to
Check out what happens if we attempt to display a fake image.
The attribute of style
Using the style attribute, you may give an element additional style like
color, font, size, and others.
The lang Attribute
To specify the language of the Web page, the lang attribute must always be
included inside the <html> tag.
Search engines and browsers are intended to benefit from this.
The example that follows uses English as the language:
The language code in the lang attribute may optionally be supplemented with
a country code. Thus, the HTML page’s initial two characters specify the
language, while its final two characters specify the nation.
The English language and the United States are specified in the example
The title Attribute
The title attribute specifies some extra details about an element.
A tooltip will appear when your cursor is over the element, displaying the
value of the title attribute: