Attributes
Published Aug 4, 2021Updated Mar 17, 2023
Contribute to Docs
Attributes can be added to the opening tag of an HTML element to change its default behavior or provide additional data about it.
Attributes consist of a name and a value in the format name="value"
:
<tag name="value"></tag>
So for example, the <img>
image element has a mandatory src
attribute that contains the image file path/URL:
<img src="codecademy_logo.png" />
src
is the attribute name.codecademy_logo.png
is the attribute value.
HTML elements can have any number of attributes.
The example below displays an image element with three attributes:
<img src="nyan_cat.gif" width="500" height="600" />
These declare its source, width, and height.
Note: Attribute names are not case-sensitive, but it is good practice to write HTML markup in lowercase.
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.
- 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 more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.