codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

3 / 59 topics
1Introduction to HTML2HTML Elements3HTML Attributes4HTML Document Structure5HTML Text Formatting6HTML Links7HTML Images8HTML Lists9HTML Tables10HTML Forms
Tutorials/HTML & CSS/HTML Attributes
🎨HTML & CSS

HTML Attributes

Updated 2026-05-15
10 min read

HTML Attributes

Introduction

Attributes in HTML provide additional information about an element. They are used to configure elements, control their behavior, or specify how they should be displayed. Each attribute is added to the opening tag of an HTML element and consists of a name-value pair separated by an equals sign (=). The value must be enclosed in quotes (either single or double).

Attributes are essential for creating interactive and dynamic web pages. They allow developers to control various aspects of elements, such as their styling, functionality, and accessibility.

Concept

HTML attributes can be broadly categorized into several types:

  1. Global Attributes: These are applicable to all HTML elements.
  2. Event Attributes: These handle user interactions like clicks or key presses.
  3. Specific Attributes: These are unique to certain elements and serve specific purposes.

Global Attributes

Global attributes provide common functionality across different elements. Some of the most commonly used global attributes include:

  • id: Provides a unique identifier for an element, which can be used in CSS or JavaScript.
  • class: Assigns one or more class names to an element, allowing it to be styled using CSS.
  • style: Contains inline CSS styles that apply directly to the element.
  • title: Displays additional information when the user hovers over the element.

Event Attributes

Event attributes are used to define what happens when a specific event occurs. For example:

  • onclick: Executes JavaScript code when an element is clicked.
  • onmouseover: Triggers JavaScript code when the mouse pointer is over an element.
  • onsubmit: Executes JavaScript code when a form is submitted.

Specific Attributes

Specific attributes are unique to certain elements and serve specific purposes. For instance, the <img> tag has the src attribute to specify the image source, while the <a> tag uses the href attribute to define the link destination.

Examples

Let's explore some practical examples to understand how HTML attributes work.

Example 1: Using Global Attributes

<div id="header" class="main-header" style="color: blue;" title="This is a header">
    Welcome to My Website!
</div>

In this example:

  • The id attribute uniquely identifies the <div> element.
  • The class attribute assigns it to the "main-header" class, which can be used for styling.
  • The style attribute applies inline CSS to change the text color to blue.
  • The title attribute provides additional information that appears as a tooltip when the user hovers over the element.

Example 2: Using Event Attributes

<button onclick="alert('Button clicked!')">Click Me</button>

Here, the onclick attribute is used to execute JavaScript code that displays an alert message when the button is clicked.

Example 3: Using Specific Attributes

<img src="logo.png" alt="Company Logo">
<a href="https://www.example.com">Visit Example</a>

In this example:

  • The src attribute in the <img> tag specifies the path to the image file.
  • The alt attribute provides alternative text for the image, which is useful for accessibility and SEO.
  • The href attribute in the <a> tag defines the URL that the link points to.

What's Next?

In the next section, we will delve into CSS Box Sizing. Understanding how elements are sized and positioned on a webpage is crucial for creating responsive designs. We'll explore different box sizing models and how they affect layout calculations.

Stay tuned!


PreviousHTML ElementsNext HTML Document Structure

Recommended Gear

HTML ElementsHTML Document Structure