Table of Content
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:
idsets a unique name for one element.classgroups many elements under one style.hrefpoints to a web address in a link.srcpoints 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 Type | Use | Example |
|---|---|---|
| Global | Works on all elements | id, class, title |
| Specific | Works only on some elements | href (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, andsrc.
FAQs
What are HTML attributes with examples?
<img src="image.jpg" alt="Example">
srcdefines the image pathaltprovides alternative text
What is the difference between id and class in HTML?
<div id="header">Header</div>
<div class="menu">Menu</div>
idis unique for one elementclasscan be reused on multiple elements
How do you use href attribute in HTML links?
<a href="https://example.com">Visit Site</a>
hrefspecifies the destination URL- It is required in
<a>tags to make clickable links
What are global attributes in HTML?
<p id="para1" class="text" style="color:red;">Hello</p>
id,class, andstyleare global attributes- They improve styling and scripting
Similar Reads
The audio tag adds sound to web pages without plugins. It gives you control to play, pause, loop, or mute…
The HTML <cite> tag is a small but important tool for marking up the title of a creative work. You…
The HTML <strong> tag shows strong importance. This tag gives semantic meaning to text. Screen readers and search engines recognize…
The <details> tag hides content and shows it when needed. It helps organize long pages. The tag shows extra info…
HTML builds the base for every web page. It sets structure and meaning so the browser knows what to show.…
The <wbr> tag in HTML helps the browser decide where it can break a word. It does not force a…
HTML global attributes work on most elements. They do not depend on the tag name. You can use them with…
The header tag defines the top part of a page or a section in HTML. It usually holds a heading…
The iframe tag embeds another HTML page inside the current page. It loads content from a separate source. Understand the…
The HTML samp tag shows the result of a computer program or command. It helps readers know which text came…