HTML Attributes - Understanding the Basics of HTML Attributes

27 May 2023 Balmiki Mandal 0 Html

Demystifying HTML Attributes: Essential Basics Unveiled

HTML attributes provide additional information about HTML elements and are always included in the opening tag. They are used to modify the default behavior or provide extra details about an element.

Here are some basics of HTML attributes:

Syntax:

The syntax for an HTML attribute is as follows:

html
<element attribute="value">Content</element>
  • Element: The HTML element to which the attribute belongs.
  • Attribute: The attribute name.
  • Value: The value assigned to the attribute. It is always enclosed in double or single quotes.

Common Attributes:

  1. id Attribute:

    • Provides a unique identifier for an element.
    html
    <div id="uniqueID">Content</div>
  2. class Attribute:

    • Assigns one or more class names to an element, allowing you to apply styles or JavaScript behaviors.
    html
    <p class="important-text">This is important!</p>
  3. style Attribute:

    • Defines inline CSS styles for an element.
    html
    <span style="color: red; font-size: 16px;">Styled Text</span>
  4. src Attribute:

    • Specifies the source URL for elements like images, iframes, or scripts.
    html
    <img src="image.jpg" alt="Description">
  5. alt Attribute:

    • Provides alternative text for images, useful for accessibility.
    html
    <img src="image.jpg" alt="Description">
  6. href Attribute:

    • Defines the hyperlink target for <a> (anchor) elements.
    html
    <a href="https://example.com">Visit Example</a>
  7. target Attribute:

    • Specifies where to open the linked document (e.g., in a new tab or window).
    html
    <a href="https://example.com" target="_blank">Open in New Tab</a>
  8. width and height Attributes:

    • Specify the width and height of certain elements like images or tables.
    html
    <img src="image.jpg" alt="Description" width="300" height="200">

Boolean Attributes:

Some attributes don't require a value. If present, they are considered true; if absent, they are false. Common examples include checked, disabled, and readonly.

html
<input type="checkbox" checked>
<button disabled>Disabled Button</button>
<input type="text" readonly>

Custom Attributes:

You can create custom attributes, but it's generally recommended to use data attributes (prefixed with data-) for custom data.

html
<div data-custom="value">Custom Content</div>

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.