HTML Attributes
HTML attributes provide additional information about HTML elements.
What are Attributes?
- 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 href Attribute
The <a> tag defines a hyperlink. The href attribute specifies the URL of the page the link goes to:
<a href="https://netgramlearn.com">Visit NetGram Learn</a>
The src Attribute
The <img> tag is used to embed an image. The src attribute specifies the path to the image to be displayed:
<img src="cat.jpg" alt="A cute cat">
The alt Attribute
The alt attribute specifies an alternate text for an image, if the image cannot be displayed:
<img src="logo.png" alt="Company Logo" width="300" height="100">
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; font-size:20px;">This is a red paragraph.</p>
The id Attribute
The id attribute specifies a unique id for an HTML element. The value must be unique within the HTML document:
<h1 id="main-heading">My Heading</h1>
<p id="intro-paragraph">Introduction text.</p>
The class Attribute
The class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class:
<p class="highlight">A highlighted paragraph.</p>
<p class="highlight">Another highlighted paragraph.</p>
Data Attributes
Custom data-* attributes allow storing extra information:
<button data-user-id="42" data-action="delete">Delete</button>
๐ก Tip: Always use lowercase attribute names and quote attribute values for consistency and compatibility.