HTML attributes are special features used to provide additional information about HTML elements. They modify the behavior of the element or define its characteristics.
Example
1.Thesrcattribute specifies the source or location of the image. Similarly, thehrefattribute in the tag defines the destination of the link.
<img src="profile.jpg" alt="Profile">
<a href="https://www.dev.to">Click here to visit Dev Community</a>
2.The
<input>tag, used for creating forms. Itstypeattribute defines what kind of input it is, like text, password, or checkbox. Theplaceholderattribute shows a hint inside the field before the user types anything.
<input type="text" placeholder="full name">
<input type="password" placeholder="password">
<input type="checkbox" id="terms">
<label for="terms">I agree to the terms and conditions</label>
4.The
<form>tag itself, used to collect user data. It has anactionattribute that points to where the data should be sent, and amethodattribute to decide how the data is sent, usuallyGETorPOST.
<form action="/submit-login" method="POST">
<input type="text" placeholder="Username">
<input type="password" placeholder="Password">
<button type="submit">Login</button>
</form>
3.Global attributes like
classandidcan be added to almost any tag.classis used to style multiple elements with CSS, whileidis used to uniquely identify a single element for scripting or linking.
<h1 id="main-title">Welcome to My Site</h1>
<p class="highlight-text">This is an important paragraph.</p>
<p class="highlight-text">This is another important paragraph.</p>
<div id="sidebar" class="layout-box highlight-text">
Sidebar content goes here.
</div>
Top comments (1)
Good explanation of HTML attributes. They are an important concept for beginners because attributes provide extra information about HTML elements and control their behavior.
One thing worth adding is that attributes can be divided into global attributes and element-specific attributes. For example, attributes like
id,class, andtitlecan be used on many HTML elements, whilehrefis mainly used with links andsrcis commonly used with images and scripts.Also, it's a good practice to use attributes that improve accessibility. For example, adding meaningful
alttext to images helps users who rely on screen readers.Overall, this is a clear introduction to a basic but essential part of writing structured HTML. Keep practicing with different elements to understand where each attribute fits best.