“In this article, we’ll dive into HTML attributes and how they work using practical examples. Think of HTML elements as building blocks for web content. Now, these elements can have something extra called ‘attributes’ that tell us more about them.
Attributes are like tags on your luggage when you travel. They come in pairs: a ‘name’ and a ‘value.’ The ‘name’ is like the label on your luggage tag, and the ‘value’ is like the information on the tag, such as your name or destination.
Here are some important things to know about attributes:
– They always come in pairs, like a name and a value, and look like this: attribute_name=’value’.
– You attach them to the opening tag of an HTML element. It’s like sticking a luggage tag to your suitcase before your trip.
– Attribute values should always be enclosed in quotes. It’s like making sure the information on your luggage tag is inside the tag.
– Most of the time, we use double quotes (‘”‘) for enclosing attribute values, but single quotes (“‘”) are also okay.
– Sometimes, when the value itself has quotes, you might need to use the opposite kind of quote. It’s like using single quotes if your name is ‘John “ShotGun” Nelson.’
Here’s an example to make it even clearer:
<img src=”picture.jpg” alt=”A beautiful sunset”>
In this case, ‘src’ is the attribute name, and ‘picture.jpg’ is the attribute value. It’s like saying, ‘This image element has a source, and the source is ‘picture.jpg’.’
Remember, attributes are like labels that provide extra information about HTML elements. They help us customize and describe our web content.”
<element attribute_name=”attribute_value”></element>
Supported Attributes: It is a global attribute that is supported by all the tags.
Please refer to the HTML Attributes Complete Reference article for all the attributes in detail.
Below are some of the most commonly used Attributes in HTML.
HTML src Attribute: If we want to insert an image into a webpage, then we need to use the <img> tag and the src attribute. We will need to specify the address of the image as the attribute’s value inside the double quote.
Example: This example explains the HTML src Attributes to specify the source address of the file.
<html>
<head>
<title>src Attribute</title>
</head>
<body>
<img src=
“https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png”>
</body>
</html>
HTML alt Attribute: This is an alternate tag that is used to show or display something if the primary attribute i.e., the <img> tag, fails to display the value assigned to it. This can also be used to describe the image to a developer who is actually sitting at the coding end.
Example: This example explains the HTML alt Attributes to specify the name of the file when the image is not loaded properly.
<html>
<head>
<title>alt Attribute</title>
</head>
<body>
<!–If the image is not found or the img field
is left blank the alt value gets displayed–>
<img src=
“https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png”
alt=”The Logo”><br>
<img src=”” alt=”Since the src value is blank,the alt value is displayed”>
</body>
</html>
HTML width and height Attribute: This attribute is used to adjust the width and height of an image.
Example: This example explains the HTML width & height Attributes to specify the different sizes of the images.
<html>
<head>
<title>Width and Height</title>
</head>
<body>
<img src=
“https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png”
width=”300px” height=”100px” >
</body>
</html>
“HTML id Attribute: Think of it as giving a name to something on a webpage. Sometimes, you have several things that look the same, like buttons or images. To tell them apart and work with them individually, we use ‘id’ attributes. These ‘id’s are like name tags for elements, making them unique. We usually use these ‘id’s later in CSS to style or JavaScript to interact with them.
Example: This shows how to use HTML id attributes to give a unique name to a specific element on a webpage.”
<!DOCTYPE html>
<html>
<head>
<style>
#geeks {
color: green;
}
</style>
</head>
<body>
<h1 id=”geeks”>Welcome to GeeksforGeeks</h1> </body>
</html>
HTML title Attribute: The title attribute is used to explain an element on hovering the mouse over it. The behavior differs with various elements but generally, the value is displayed while loading or hovering the mouse pointer over it.
Example: This example explains the HTML title Attributes to specify the metadata for the element on hovering the mouse over it.
<html>
<head>
<title>title Attribute</title>
</head>
<body>
<h3 title=”Hello GeeksforGeeks”>Hover to see the effect</h3>
</body>
</html>
HTML href Attribute: This attribute is used to specify a link to any address. This attribute is used along with the <a> tag. The link put inside the href attribute gets linked to the text displayed inside the<a> tag. On clicking on the text we will be redirected to the link. By default, the link gets opened in the same tag but by using the target attribute and setting its value to “_blank”, we will be redirected to another tab or another window based on the browser’s configuration.
Example: This example explains the HTML href Attributes specify the link address of the file.
<html>
<head>
<title>link Attribute</title>
</head>
<body>
<a href=”https://www.geeksforgeeks.org/”>
Click to open in the same tab
</a><br>
<a href=”https://www.geeksforgeeks.org/” target=”_blank”>
Click to open in a different tab
</a>
</body>
</html>
HTML style Attribute: This attribute is used to provide various CSS effects to the HTML elements such as increasing font-size, changing font-family, coloring, etc.
Example: This example explains the HTML style Attributes to specify the style properties for the HTML element.
<html>
<head>
<title>style Attribute</title>
</head>
<body>
<h2 style=”font-family:Chaparral Pro Light;”>Hello GeeksforGeeks.</h2>
<h3 style=”font-size:20px;”>Hello GeeksforGeeks.</h3>
<h2 style=”color:#8CCEF9;”>Hello GeeksforGeeks.</h2>
<h2 style=”text-align:center;”>Hello GeeksforGeeks.</h2>
</body>
</html>
Not a member yet? Register now
Are you a member? Login now