What are HTML Attributes? #
HTML attributes are special words or codes added inside the opening tag of an HTML element to provide extra information or to modify how the element behaves or appears on a webpage.
For example, while the <a>
tag makes a hyperlink, an attribute like href
inside it tells the browser where the link should go.
Attributes add meaning or instructions to an element, making websites more dynamic and functional.
How HTML Attributes Work: Syntax and Structure #
Attributes are always placed within the start tag of an element and follow this pattern:
<tagname attribute_name="attribute_value">Content</tagname>
- attribute_name is the name that defines what aspect of the element you are specifying.
- attribute_value is the specific information or setting you assign to that attribute, enclosed in quotation marks.
Example: #
<a href="https://www.widelamp.com">Visit Widelamp</a>
<a>
is the tag (hyperlink element).href
is the attribute name, which means the link destination."https://www.widelamp.com"
is the attribute value (the URL to visit).Visit Widelamp
is the clickable text visible on the page.
Commonly Used HTML Attributes with Examples #
Here are some useful attributes you will often use with HTML elements:
- href: Specifies the URL for hyperlinks (
<a>
tag).
<a href="https://www.example.com">Example Website</a>
src: Specifies the source file for images or media (<img>
, <video>
, <audio>
tags).
<img src="logo.png" alt="Company Logo">
alt: Provides alternative text describing an image, useful for accessibility.
id: Assigns a unique identifier to an element, useful for CSS styling and JavaScript targeting.
<p id="intro">Welcome to our website!</p>
class: Used to group elements into categories for CSS styling.
<div class="container">Content here</div>
title: Shows a tooltip text when hovering over the element.
<button title="Click me to submit">Submit</button>
style: Adds inline CSS styles directly to the element.
<h1 style="color:blue;">Blue Heading</h1>
Different Types of Attributes: Name/Value and Boolean #
- Name/Value attributes: Most attributes require a value, like
href="URL"
orid="uniqueID"
. - Boolean attributes: Some attributes represent a true/false state and do not need a value. If present, the attribute is “true”.
Examples include:
disabled
on a button to make it unclickable:
<button disabled>Cannot click me</button>
checked
on a checkbox to pre-select it:
<input type="checkbox" checked>
How Attributes Affect HTML Elements #
Attributes instruct the browser on how to handle or display the element. They can:
- Link to external webpages or resources
- Define image sources and descriptions
- Assign styles or classes for design
- Identify elements uniquely for scripts or styles
- Control interactive features like buttons or forms
For example, without the src
attribute, an <img>
tag cannot show any image because it doesn’t know where to find it.
Summary and Next Steps #
HTML attributes add essential details to elements, helping build functional and well-structured webpages. Remember:
- They go inside opening tags.
- They usually come as name/value pairs.
- Some attributes exist as standalone boolean flags.
- Different elements support different attributes.
- Common attributes include
href
,src
,alt
,id
,class
, andtitle
.
To practice, try adding attributes to your HTML tags and observe how they change the page behavior or look.