Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials HTML HTML Attributes
HTML Beginner FREE

HTML Attributes

Lesson 2 of 16 Beginner Interactive

Attributes provide additional information about HTML elements. They are always placed in the opening tag.

Common Attributes

  • href — specifies the URL for links
  • src — specifies the source for images
  • alt — provides alternative text for images
  • class — assigns a CSS class
  • id — assigns a unique identifier
  • style — adds inline CSS styles

Syntax

HTML
<tagname attribute="value">Content</tagname>
Common HTML Attributes
HTML
<!DOCTYPE html>
<html>
<body>
    <div id="main-content">
        <h1 id="title">Page Title</h1>
    </div>

    <p class="intro highlight">Two classes on this paragraph.</p>

    <p style="color: blue; font-size: 20px;">
        Inline styled paragraph.
    </p>

    <a href="https://sukhnexus.com" target="_blank">
        Visit SukhNexus
    </a>

    <img src="logo.png" alt="Logo" width="100" height="50">
</body>
</html>

Practice

1
Exercise

Create a link with href="https://example.com" and the text "Click Here".

Answer
<a href="https://example.com">Click Here</a>
2
Exercise

Add an alt attribute to an image tag.

Answer
<img src="photo.jpg" alt="Description of the photo">

Quick Quiz

1

Which attribute specifies the URL of a link?

The href attribute specifies the URL of a hyperlink.

Interview Questions

The id attribute must be unique on a page (used once), while a class can be used on multiple elements. IDs have higher CSS specificity than classes.