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.

Syntax

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

Example

<!DOCTYPE html>  
<html>
<head>
</head>
<body>
<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>
</body>
</html>

Output

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.

Examples

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>  
<html>
<head>
</head>
<body>
<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>
</body>
</html>

Output result

The href Attribute

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

Example

<a href="https://www.google.com">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:

Example

<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="https://www.example.com/images/an-image.jpg".

  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.

note

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):

Example

<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.

Example

<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.

Example

<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.

Suggestions

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.

Good:

<a href="https://www.example.com/html/">Visit our HTML tutorial</a>

Bad:

<a href=https://www.example.com/html/>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">