WikiGalaxy

Personalize

HTML Attributes

Basic Attributes

What are HTML Attributes?

HTML attributes provide additional information about HTML elements. They are always specified in the start tag and usually come in name/value pairs like: name="value".

Common HTML Attributes

Some common attributes include id, class, style, title, and href. Each attribute serves a unique purpose and can influence the behavior or appearance of an element.


<!DOCTYPE html>
<html>
<body>

<a href="https://www.example.com" target="_blank">Visit Example</a>

</body>
</html>
        

Purpose of Attributes

Attributes allow you to customize elements, link CSS styles, and provide metadata. They are crucial for defining the structure and behavior of web pages.

Console Output:

Visit Example

Using the 'id' Attribute

Unique Identification

The id attribute uniquely identifies an HTML element within a page. It is used for CSS styling and JavaScript manipulation.

Example Usage

Assigning a unique id to an element allows it to be targeted specifically by CSS or JavaScript functions.


<style>
#unique-element {
    color: blue;
}
</style>

<p id="unique-element">This is a paragraph with a unique id.</p>
        

Console Output:

This is a paragraph with a unique id.

The 'class' Attribute

Grouping Elements

The class attribute is used to define a group of elements that can be styled and manipulated together using CSS and JavaScript.

Multiple Classes

An element can have multiple classes, allowing for complex styling and interactions.


<style>
.text-highlight {
    background-color: yellow;
}
.bold-text {
    font-weight: bold;
}
</style>

<p class="text-highlight bold-text">This paragraph has multiple classes.</p>
        

Console Output:

This paragraph has multiple classes.

The 'style' Attribute

Inline Styling

The style attribute allows for inline CSS styling directly within an HTML element, providing a quick way to apply styles without external stylesheets.

Example Usage

Inline styles can override styles defined in external stylesheets.


<p style="color: red; font-size: 20px;">This paragraph is styled inline.</p>
        

Console Output:

This paragraph is styled inline.

The 'title' Attribute

Providing Additional Information

The title attribute provides additional information about an element, often displayed as a tooltip when the mouse hovers over the element.

Accessibility Enhancement

Using the title attribute can enhance accessibility by providing context to screen readers.


<p title="This is a tooltip">Hover over this paragraph to see the tooltip.</p>
        

Console Output:

Hover over this paragraph to see the tooltip.

The 'href' Attribute

Linking Resources

The href attribute specifies the URL of the page the link goes to. It is used within <a> (anchor) tags.

Example Usage

Using the href attribute, you can create links to other web pages, documents, or resources.


<a href="https://www.example.com">Go to Example</a>
        

Console Output:

Go to Example

The 'alt' Attribute

Alternative Text for Images

The alt attribute provides alternative text for an image if it cannot be displayed. It is crucial for accessibility and SEO.

Example Usage

Using descriptive alt text helps screen readers convey the content of an image to visually impaired users.


<img src="image.jpg" alt="A beautiful sunrise over the mountains">
        

Console Output:

A beautiful sunrise over the mountains

The 'src' Attribute

Specifying Source Files

The src attribute specifies the source file for media elements like images, audio, and video. It defines the path to the resource.

Example Usage

Properly using the src attribute ensures that media files are correctly loaded and displayed on web pages.


<img src="path/to/image.jpg" alt="Descriptive text for image">
        

Console Output:

Descriptive text for image

Basic Attributes

  Attributes like href, src, alt, style, and title are foundational.

The href Attribute

<!DOCTYPE html>
<html>
<head>
    <title>Hyperlink Example</title>
</head>
<body>
    <a href="https://www.example.com">Visit Example</a>
</body>
</html>

Explanation:

  • href specifies the link's destination.
  • Links can use absolute URLs (https://) or relative URLs (/about.html).
  • The src Attribute

    The src attribute specifies the source file for an image, video, or script.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Image Example</title>
    </head>
    <body>
        <img src="image.jpg" alt="Beautiful Landscape">
    </body>
    </html>
    

    Explanation:

  • src specifies the image's file path.
  • alt provides alternative text if the image cannot load.
  • Absolute vs. Relative URLs

    Absolute URL: Complete path including domain name. Example: https://example.com/page.html

    Relative URL:Path relative to the current document. Example: page.html

    <!DOCTYPE html>
    <html>
    <head>
        <title>URL Example</title>
    </head>
    <body>
        <a href="https://www.example.com">Absolute URL</a>
        <br>
        <a href="relative-page.html">Relative URL</a>
    </body>
    </html>
    

    The alt Attribute

    The alt attribute defines alternative text for images. It's essential for accessibility.

    <img src="forest.jpg" alt="A lush green forest">
    

    Explanation:

    • Displays text if the image fails to load.

    The style Attribute

    The style attribute adds inline CSS to elements.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Styled Text</title>
    </head>
    <body>
        <p style="color:blue; font-size:20px;">This is styled text</p>
    </body>
    </html>
    

     Advanced Attributes

    The lang Attribute

    Defines the language of the document.

    <html lang="en">
    

    Output:

    WikiGalaxy

    The title Attribute

    Adds a tooltip that appears when hovering over the element.

    <a href="https://www.example.com" title="Visit Example">Hover Over Me</a>
    

    data-* Attributes

    Custom data attributes for developers.

    <div data-user-id="12345">User Information</div>
    

    aria-* Attributes

    Improve accessibility for screen readers.

    <button aria-label="Close">X</button>
    
    logo of wikigalaxy

    Newsletter

    Subscribe to our newsletter for weekly updates and promotions.

    Privacy Policy

     • 

    Terms of Service

    Copyright © WikiGalaxy 2025