Attributes provide additional information or functionality to an HTML element. They modify how the element behaves or appears. Attributes are always included in the opening tag and are written as name–value pairs, with the value enclosed in quotes.
Common uses include linking to other pages, setting image sources, defining styles, identifying elements with IDs or classes, and controlling accessibility with ARIA attributes.
Example:
<a href="https://example.com">Visit</a>In this example:
a is the HTML element (anchor tag)href is the attribute name"https://example.com" is the attribute valueBest Practices:
src for images, href for links)More examples:
<img src="photo.jpg" alt="A scenic view">
<input type="text" placeholder="Enter your name">
<div id="main-content" class="container"></div>The format is:
<tagname attribute="value">content</tagname>id: Unique identifierclass: Used for grouping and stylingstyle: Inline CSS (not recommended for large projects)title: Tooltip shown on hover<p id="intro" class="text" title="Intro text">Hello</p>Some attributes are specific to certain elements:
<a> (links)href: Target URLtarget="_blank": Open in new tab<img> (images)src: Image file pathalt: Alternative text for screen readerswidth, height: Dimensions<input> (form fields)type: Text, password, checkbox, etc.value: Default field contentplaceholder: Hint shown in fieldBoolean attributes do not require a value—just their presence in the tag is enough to enable them. You can optionally assign the attribute name as the value (e.g., checked="checked"), but in HTML5, simply including the attribute is standard.
<input type="checkbox" checked>checked – Pre-selects a checkbox or radio button when the page loads.<input type="checkbox" checked>disabled – Prevents the user from interacting with the element (e.g., clicking a button or typing in a field).<button disabled>Submit</button>readonly – Makes an input field non-editable, but still allows the user to select and copy the text.<input type="text" value="Read-only text" readonly>required – Marks a form input as mandatory, preventing form submission until a value is provided.<input type="email" required>alt on images)class for styling, not styleAdd an image with src and alt, and create a link that opens in a new tab:
<img src="logo.png" alt="Company Logo" width="200">
<a href="https://www.example.com" target="_blank">Visit our site</a>Ask the AI if you need help understanding or want to dive deeper in any topic