Attributes in html are like settings for HTML tags, telling them how to look or behave. They come as pairs, with a name and a value, always placed in the tag's opening part and enclosed in quotes.
=
), and the value is usually enclosed in quotes.For example:
In this <a>
(anchor) tag, the href
attribute is used to specify the hyperlink destination.
Class
AttributeThe class
attribute is a powerful tool to apply a specific style or behavior to multiple elements. It helps group elements together, enabling you to style them collectively using CSS.
This text is highlighted.
This text is highlighted.
Here, the class
attribute with the value “highlight” is applied to the paragraph (<p>
) element.
Element <p> and <div> are combined and same style added to both the elements
Id
AttributeThe id
attribute provides a unique identifier for an element. This is useful for targeting a specific element with CSS or JavaScript.
Welcome to my Website
The id
attribute with the value “main-heading” is assigned to the heading (<h1>
) element.
Src
AttributeThe src
attribute, used in the <img>
tag, specifies the source (URL or file path) of an image.
Here, the src
attribute points to the image file “image.jpg.”
Href
AttributeIn the <a>
(anchor) tag, the href
attribute defines the hyperlink destination.
The href
attribute contains the URL — https://www.example.com.
In the combined example, we create a simple webpage using these attributes. The <style>
section inside the <head>
tag demonstrates how the highlight
class is styled in CSS.
My Simple Webpage
Welcome to My Simple Webpage
Section 1
This is the content of Section 1.
Section 2
This is the content of Section 2.
Section 3
This is the content of Section 3.
Visit Example.com
<style>
section in the <head>
tag defines a CSS rule for the .highlight
class, making the text red and bold.<h1>
tag has an id
attribute with the value “main-heading.”<ul>
use the class
attribute for styling and the href
attribute for linking to sections within the page.src
attribute to specify the image file.href
attribute to link to “https://www.example.com.”This chapter highlights the crucial role of HTML attributes in shaping web content. Whether you're styling with class, identifying with id, or linking with href and src, attributes bring life to HTML elements. As you progress in web development, mastering attributes empowers you to create dynamic and interactive websites. Remember to experiment, explore, and have fun building with HTML attributes. Happy coding !❤️