HTML Tutorial
HTML XHTML
HTML Media
HTML Advanced
In this tutorial, we'll learn about HTML attributes and how to use them with different HTML elements. HTML attributes provide additional information about elements and modify their behavior or appearance.
An attribute is added to an HTML element within the opening tag. It consists of a name and a value, separated by an equal sign (=). The value should be enclosed in double or single quotes.
Here's a simple example of an HTML attribute:
<a href="https://www.example.com">Visit Example.com</a>
In this example, the href
attribute is used to specify the URL the user will be directed to when clicking on the link.
There are several attributes that can be used with multiple HTML elements:
id
: Assigns a unique identifier to an element. The value must be unique within the HTML document. It's often used for styling and referencing elements using JavaScript or CSS.class
: Assigns a class name to an element. It can be used for styling multiple elements using the same CSS rules.style
: Defines inline CSS styles for an element.title
: Specifies a tooltip for an element. The tooltip text is displayed when the user hovers over the element.Here's an example that demonstrates the usage of these attributes:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>HTML Attributes Tutorial</title> <style> .blue-text { color: blue; } </style> </head> <body> <p id="intro" class="blue-text" style="font-weight: bold;" title="This is an introductory paragraph.">Welcome to the HTML attributes tutorial!</p> </body> </html>
In this example, the id
, class
, style
, and title
attributes are used to modify the appearance and behavior of a paragraph element.
Some attributes are specific to certain HTML elements. Here are a few examples:
<a>
element:
href
: Specifies the URL of the linked document.target
: Specifies where to open the linked document (e.g., in a new window or tab).<img>
element:
src
: Specifies the URL of the image.alt
: Provides an alternate text for the image, displayed when the image cannot be loaded or for accessibility purposes.<input>
element:
type
: Specifies the type of input element (e.g., text, password, checkbox, etc.).name
: Specifies the name of the input element, used for referencing form data after submission.value
: Specifies the initial value of the input element.placeholder
: Provides a hint to the user about what data should be entered in the input field.HTML data attributes:
Data attributes allow developers to store extra information in HTML elements. They start with data-
followed by a custom name.
<div data-info="some data">Content</div>
HTML event attributes:
Event attributes are used to define JavaScript events for elements. Examples include onclick
, onmouseover
, etc.
<button onclick="myFunction()">Click me</button>
Using attributes in HTML elements:
Attributes are added to HTML elements within their opening tags. For example, setting the class
attribute on a <div>
:
<div class="my-class">Content</div>
HTML form attributes:
Form attributes are used to define properties of HTML forms. Examples include action
, method
, and enctype
.
<form action="/submit" method="post"> <!-- form elements go here --> </form>
HTML input attributes:
Input attributes define properties for input elements. Examples include type
, placeholder
, and value
.
<input type="text" placeholder="Enter your name" value="John Doe">
HTML image attributes:
Image attributes define properties for <img>
elements. Examples include src
, alt
, and width
.
<img src="image.jpg" alt="A beautiful image" width="300">
HTML link attributes:
Link attributes define properties for <a>
(anchor) elements. Examples include href
, target
, and rel
.
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit Example</a>
HTML meta tag attributes:
Meta tag attributes define properties for <meta>
elements. Examples include charset
, name
, and content
.
<meta charset="UTF-8">
HTML table attributes:
Table attributes define properties for <table>
elements. Examples include border
, width
, and cellpadding
.
<table border="1" width="100%" cellpadding="10"> <!-- table content goes here --> </table>
HTML div attributes:
<div>
attributes include global attributes like class
and style
. They are often used as container elements.
<div class="container" style="background-color: #f0f0f0;"> <!-- content goes here --> </div>
HTML anchor tag attributes:
Anchor tag attributes include href
, target
, and download
.
<a href="document.pdf" target="_blank" download="MyDocument">Download Document</a>
HTML list attributes:
List attributes include type
, start
, and reversed
for <ol>
(ordered list) elements.
<ol type="1" start="3"> <li>Item 1</li> <li>Item 2</li> </ol>