Skip to main content

HTML Attributes

HTML attributes provide additional information about HTML elements.

HTML Attributes

  • HTML attributes are special words which provide additional information about the elements or attributes are the modifier of the HTML element.
  • All HTML elements can have attributes
  • Attributes provide additional information about elements
  • Attributes are always specified in the start tag
  • Attributes usually come in name/value pairs like: name="value"
  • The Attributes name and values are case sensitive, and it is recommended by W3C that it should be written in Lowercase only.
  • You can add multiple attributes in one HTML element, but need to give space between two attributes.


<tagname  attribute_name="value">content</tagname>


<!DOCTYPE html>  
<h1> This is Style attribute</h1>
<p style="height: 50px; color: blue">It will add style property in element</p>
<p style="color: red">It will change the color of content</p>


Output result

The title attribute in HTML

The title attribute is used as text tooltip in most of the browsers. It display its text when user move the cursor over a link or any text. You can use it with any text or link to show the description about that link or text. In our example, we are taking this with paragraph tag and heading tag.


Example with <h1> tag

<h1  title="This is heading tag">Example of title attribute</h1>

Example with <p> tag:

<p  title="This is paragraph tag">Move the cursor over the heading and paragraph, and you will see a description as a tooltip</p>

Full code example:

<!DOCTYPE html>  
<h1 title="This is heading tag">Example of title attribute</h1>
<p title="This is paragraph tag">Move the cursor over the heading and paragraph, and you will see a description as a tooltip</p>

Output result

The href Attribute

The <a> tag defines a hyperlink. The href attribute specifies the URL of the page the link goes to:


<a href="">This is a link</a>

Output result

You will learn more about links in our [HTML Links](TODO link) chapter.

The src Attribute

The <img> tag is used to embed an image in an HTML page. The src attribute specifies the path to the image to be displayed:


<img src="my-image.jpg">

There are two ways to specify the URL in the src attribute:

  1. Absolute URL - Links to an external image that is hosted on another website. Example: src="".

  2. Relative URL - Links to an image that is hosted within the website. Here, the URL does not include the domain name. If the URL begins without a slash, it will be relative to the current page. Example: src="an-image.jpg". If the URL begins with a slash, it will be relative to the domain. Example: src="/images/an-image.jpg".

Tip: It is almost always best to use relative URLs. They will not break if you change domain.


You can use single or double quotes for URL in src attribute. In HTML5, you can also omit use of quotes around attribute values, but the use of quotes is recomended.

The width and height Attributes

The <img> tag should also contain the width and height attributes, which specifies the width and height of the image (in pixels):


<img src="an-image.jpg"  width="800"  height="600">

The alt Attribute

The required alt attribute for the <img> tag specifies an alternate text for an image, if the image for some reason cannot be displayed. This can be due to slow connection, or an error in the src attribute, or if the user uses a screen reader.


<img src="an-image.jpg" alt="Alt text of the image" >

You will learn more about images in our [HTML Images](TODO images chapter link) chapter.

The style Attribute

The style attribute is used to add styles to an element, such as color, font, size, and more.


<p style="color:red;">This is a red paragraph.</p>

You will learn more about styles in our [HTML Styles](TODO styles chapter link) chapter.


Use Lowercase Attributes

The HTML standard does not require lowercase attribute names.

The title attribute (and all other attributes) can be written with uppercase or lowercase like title or TITLE.

However, W3C recommends lowercase attributes in HTML, and demands lowercase attributes for stricter document types like XHTML.

Quote Attribute Values

The HTML standard does not require quotes around attribute values.

However, W3C recommends quotes in HTML, and demands quotes for stricter document types like XHTML.


<a href="">Visit our HTML tutorial</a>


<a href=>Visit our HTML tutorial</a>

Sometimes you have to use quotes. This example will not display the title attribute correctly, because it contains a space:

<p title=An Example>

In some situations, when the attribute value itself contains double quotes, it is necessary to use single quotes:

<p title='John "ShotGun" Nelson'>

Or vice versa:

<p title="John 'ShotGun' Nelson">