Attributes

Published Aug 4, 2021Updated Sep 16, 2023
Contribute to Docs

HTML attributes are data that can be added to the opening tag of an HTML element. These attributes modify the default behavior of the element or provide additional information about it.

Attributes consist of a name and are usually set equal to a value. Values are enclosed in double or single quotation marks.

Syntax

The syntax for adding attributes to an HTML element is as follows:

<tagname attribute="value"></tagname>

Note: Attribute names are not case-sensitive, but it is good practice to write HTML markup in lowercase.

Examples

Image Attributes

Using an <img> element, which is commonly used to display images, reveals several attributes, such as src, width, and height. These attributes exert significant influence over the manner in which the image appears on the web page. The dimensions of the particular element, once specified, enable precise control over the end result.

<img src="source_of_image.png" width="500" height="600" />

GIF showcasing effect of height attribute on an img element

disabled Attribute

The disabled attribute is applied to form elements like buttons and inputs to make them nonfunctional:

<button disabled>Submit</button>

This is the output of the above code snippet: Disable attribute on a <button> element

Attributes

class
An HTML attribute used to select one or more elements for the application of styling or logic.
dir
Sets the direction of the text of an HTML element.
id
Used to specify a unique id for an HTML element, which cannot be shared across multiple elements within the same HTML document.
src
Specifies the location of a digital source.
style
Specifies how an element will be rendered in a web browser.
title
Contains and can display the specified information about the element it belongs to.

All contributors

Looking to contribute?

Learn HTML on Codecademy