What are HTML Attributes?
HTML attributes provide additional information about elements, such as size, color, behavior, etc. They come in two main types:
- Global Attributes: Apply to all HTML elements (e.g., id, class)
- Specific Attributes: Apply to specific elements (e.g., src for images, alt for links)
How to Use Attributes
Attributes are written inside the element's opening tag, in the following format:
<element attribute="value">
Key Concepts
- id: Identifies an element uniquely on the page, used for styling and scripting.
- class: Assigns a class name to an element, used for styling a group of elements.
- src: Specifies the source of an image or file.
- alt: Provides alternative text for images, aiding accessibility for visually impaired users.
Example
<img src="my-image.jpg" alt="A picture of a cat">
In this example:
<img>
is the image element.
src="my-image.jpg"
specifies the image file to be displayed.
alt="A picture of a cat"
provides a description of the image for accessibility.
Importance of Attributes
Attributes are essential for:
- Styling: Adding visual effects to elements.
- Functionality: Controlling the behavior of elements (e.g., opening links in a new tab).
- Accessibility: Making content accessible to all users.
- Organization: Grouping similar elements for easier management.