Every HTML element can have some extra details about them. These additional pieces of information are called attributes. They are the modifier of the HTML elements.
Check the example shown below. The example have elements with attributes.
In the above example, some HTML elements' start tags have extra information. These additional pieces of information are attributes. For example:
Let's discuss these attributes in more detail.
Inside the <html> tag you should always provide a lang attribute.
It helps the browser in setting the language of the HTML document. With this, the browser can display the contents' language accurately on the screen.
It also helps search engines in indexing the page.
The value of the lang attribute can also contain the country code along with the language code.
The first two characters will be language code followed by a hyphen and the last two characters will be specified with the country code.
To set the language as English and the country as the United States, you need to set lang="en-US".
See the example below for more clear understanding.
When we need to give some specific styles to HTML elements, we apply style attributes to them. With style attribute, one can modify an element's default view in the browser.
The style attribute can change the font, color and size of an element.
As in the below example, we have changed the font color of the heading to red.
We will learn this concept in more detail in the HTML Styles chapter.
If we need to give some extra information or a hint about an element, we can use the title attribute in the start tag of that element.
When we mouse over such an element, the value of the attribute is shown as a tooltip.
Check out the below example. For better understanding, click on 'Try on Editor' to view the result.
The href attribute is applied to element <a>. Element <a> defines a hyperlink in an HTML document.
The href value holds the URL of another HTML document.
In the example above, when we click, 'Visit Result University', a new web page (HTML document) opens.
Read Chapter: HTML Links for detailed discussion.
The src attribute contains the location (URL) of the external resource.
When the src attribute is specified in the <img> tag, the browser loads the image from the URL and embeds it in the document.
The value of the src i.e. the URL can be specified in two ways:
1. Absolute URL:
An absolute URL is a full URL that specifies the complete address of the resource location.
So an absolute URL contains the name of the protocol (HTTP/HTTPS), domain (resultuniversity.com) and path (/html/aloevera.jpg).
If an image is hosted on another website, we will provide an absolute URL in the value of the src.
2. Relative URL:
If the resource (URL) is located within the website, we can provide only the path as the value in the src attribute. Such value is termed relative URL.
If a relative URL starts with a slash, it is relative to the domain.
If a relative URL starts without a slash, it is relative to the current page.
The alt attribute as the name suggests provides an alternate text to the image.
If the image loads properly, the value provided in the alt attribute i.e. the text is kept hidden from the visible screen. But for some reason, if the image fails to load properly the value of the alt attribute will be shown instead of the image.
See what happens if the image fails to load. An image can fail to load due to various reasons like a slow internet connection, or a wrong URL specified.
Width and height attributes set the width and height of an element to a specific value.
Let's take an example of an image whose width is set to 300 pixels and whose height is set to 250 pixels.
As HTML is not a case-sensitive language, you are free to use uppercase letters for attributes name.
But W3C recommends the use of lowercase letters for attribute names.
In our HTML tutorial, we will use lowercase letters for attribute names.
Though HTML doesn't strictly ask for quotes around the values of the attributes, it is always a good practice to quote around the attribute's value to eliminate unexpected results.
The value of the title attribute contains space and hence the text of the tooltip is only 'Desktop'.
So if the value contains space, you must use quotes.
W3C also recommend having quotes around the values
Both single and double quotes around the attributes' value are allowed. But commonly double quotes are preferred. Single quotes are used when the value itself contains double quotes or vice versa.
|1. HTML Tutorial|
|2. Introduction to HTML|
|3. Basic HTML Page|
|4. HTML Editors|
|5. Web Browser|
|6. HTML History|
|7. Basic HTML Tags|
|8. HTML Elements|
|9. HTML Attributes|
|10. HTML Headings|
|11. HTML Paragraphs|
|12. HTML Text Formatting|
|13. HTML Styles|
|14. HTML Comment|
|15. HTML Quotation|
|16. HTML Colors|
|17. HTML RGB Colors|
|18. HTML HEX Colors|
|19. HTML HSL Color|
|20. HTML CSS|
|21. HTML Links|
|22. HTML Link Colors|
|23. Link within Page|
|24. HTML Images|
|25. HTML Background Image|
|26. HTML Image Map|
|27. HTML Picture Element|
|28. HTML Favicon|
|29. HTML Table|
|30. HTML Table Border|
|31. HTML Table Size|