HTML Attributes with Examples for Beginners

html attributes

HTML attributes add extra detail to elements so the browser knows how to handle them. Here you will see examples and will cover the full guide.

What are HTML Attributes?

An attribute adds detail to an element. It defines extra information about that element. It can also change the way the element behaves on the page.

The syntax looks like this:

<tagname attribute="value">content</tagname>

You write the attribute inside the opening tag. The attribute always has a name and a value.

Here are the commonly used HTML attributes:

  • id sets a unique name for one element.
  • class groups many elements under one style.
  • href points to a web address in a link.
  • src points to the file source of an image or media.

Global vs. Specific Attributes in HTML with Examples

A global attribute applies to every element in HTML. A specific attribute works only on some elements.

Here is a table to show the main difference:

Attribute TypeUseExample
GlobalWorks on all elementsid, class, title
SpecificWorks only on some elementshref (for <a>), src (for <img>)

Global attributes give more control across all elements. Specific attributes add detail only to certain tags.

Examples of HTML Attributes

Use of id:

<p id="intro">This is a paragraph with an id attribute.</p>

This example sets a unique name for the paragraph. The id attribute lets you target that element with CSS or JavaScript. It makes the element stand alone with a clear reference.

Use of class:

<p class="note">This is one paragraph with a class.</p>
<p class="note">This is another paragraph with the same class.</p>

This example groups two paragraphs under one class. The class attribute lets you style or script many elements at once. You can use the same class name across the page.

Use of href:

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

This example creates a link to an external page. The href attribute holds the web address. The anchor tag then uses it to send the user to that location.

Use of src:

<img src="photo.jpg" alt="Sample photo">

This example loads an image on the page. The src attribute points to the file path. The browser uses that path to fetch and display the image.

Wrapping Up

In this article you learned what attributes are and how to use them. You also saw the difference between global and specific attributes.

Here is a quick recap:

  • Attributes add detail and control to elements.
  • Syntax follows the pattern name="value".
  • Global attributes work on every element.
  • Specific attributes work on certain tags only.
  • You saw examples of id, class, href, and src.

FAQs

What are HTML attributes with examples?

HTML attributes provide extra information about elements.
<img src="image.jpg" alt="Example">
  • src defines the image path
  • alt provides alternative text

What is the difference between id and class in HTML?

<div id="header">Header</div>
<div class="menu">Menu</div>
  • id is unique for one element
  • class can be reused on multiple elements

How do you use href attribute in HTML links?

<a href="https://example.com">Visit Site</a>
  1. href specifies the destination URL
  2. It is required in <a> tags to make clickable links

What are global attributes in HTML?

Global attributes can be used with all HTML elements.
<p id="para1" class="text" style="color:red;">Hello</p>
  • id, class, and style are global attributes
  • They improve styling and scripting

Similar Reads

What is HTML for Beginners and How it Works for Beginners

HTML builds the base for every web page. It sets structure and meaning so the browser knows what to show.…

The Img Tag in HTML: How to Display Images

You need images to show products or ideas on your site. The HTML img tag places those images on the…

HTML source Tag: How to Embed Media with Examples

The source tag in HTML helps load different media formats and screen sizes. You can use it inside many other…

HTML Form Tag: How Create User Input Forms

The HTML form tag helps you collect user input on a website. You use the HTML form tag to create…

HTML noscript Tag: How to Display Fallback When JS Is Disabled

The HTML noscript tag tells the browser what to show when JavaScript does not run. This tag targets users who…

HTML output Tag: How to Display Calculation Results

The <output> tag in HTML shows results inside a form. It helps display values from user actions or scripts. You…

HTML head Tag: How to Include Metadata and Links in head

The HTML head tag contains information and loads tools for the web page. What Is the <head> Tag in HTML?…

HTML vs XHTML: Key Differences Every Developer Must Know

HTML and XHTML describe web pages. You must know the differences to write code that works across browsers. This article…

HTML Aside Tag: How to Create a Side Content

You use the <aside> tag to mark extra content in HTML. It does not replace the main text and shows…

The canvas Tag in HTML: How to Draw Graphics with Examples

The HTML canvas tag draws shapes and images. It also helps you draw text with code. It gives control over…

Previous Article

JavaScript Object References and Copying for Beginners

Next Article

PHP array_fill_keys: Create Arrays with Specific Keys

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.