In the previous lesson we knew what is an element such as <h1> , <p> etc. Now we’ll know what is an attribute and how and where can we use it in html document.
HTML attributes usually appear as name-value pairs, separated by =, are written within the start tag of an element, after the tag’s name.
In the above picture it displays the syntax of html attribute. Attributes are always expressed inside the element's start tag. You can see that attributes have
In the place of attribute ( what is shown in the above picture) you have to write the name of an attribute what you would like to assign for an element.
In the place of value ( what is shown in the above picture) you have to set the value of that attribute.
value can be written within single or double quotes.
<p title="i am a tooltip" > I'm a paragraph </p>
Here we use title attribute for <p> element and the value of the title element is "i am a tooltip". when users move the cursor over the text ( I'm a paragraph ) it shows the value of that title.
Every attribute have its own functionality.
The Empty attributes can be specified with a name and an empty-string value or you can just write the attribute name where the value is implicitly the empty string.
Example of Empty attribute syntax-
<input disabled>
or
<input disabled="">
An attribute value can be specified without the quotes.These are called Unquoted attribute-value.
Example of Unquoted attribute syntax-
<input value=yes>
"
,'
,=
,<
,>
or `
characters as attribute value.
An single-quoted attribute value is one where the specified value is surrounded by single quotation marks (').
Example of Single-quoted attribute value syntax-
<p title='i am a tooltip' > I'm a paragraph </p>
In the above example we used single quote for title attribute.
'
character as attribute value then you can't use this syntax.Then use double-quoted attribute-value syntax.
A double-quoted attribute value is one where the specified value is surrounded by double quotation marks (").
Example of Double-quoted attribute value syntax-
<p title="i am a tooltip" > I'm a paragraph </p>
In the above example we used double quote for title attribute.
"
character as attribute value then you can't use this syntax.Then use Single-quoted attribute-value syntax.
There are some attributes in HTML called Boolean attributes.The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
<input type="checkbox" checked>
or
<input type="submit" value="Submit" disabled>
There are lots of reasons for using an attribute -
Some elements must have certain attributes. For example src attribute for <img> element and href attribute for <a> element.
<img src="https://www.codeinbook.com/images/cartoon_image.gif" >
You have to provide the location of that image by using src attribute. You can’t display that image without using src attribute.
<a href="https://www.google.com/"> Google </a>
The href attribute is used to provide a link to any address. It turns a normal text to a link called hypertext. When user click on that text it goes to that address.
you can have one or more attributes within one element. For example-
<a href="https://www.google.com/" target="_blank" > Google </a>
Here we use href and target two attributes for <a> element. The target attribute helps to open the link within a new tab instead of this one.