Attribute
Tags give a web browser only basic information about types of elements. Attributes are used to add more specific and varied instructions. Attributes are written inside of the start tag. For example, you can specify a URL for the link using the href
attribute in the <a>
tag like this: <a href="index.html">
.
There are two types of attributes:
- Attributes that are specific to certain tags: e.g.,
href
for<a>
,src
for<img>
- Attributes that can be used in any tag (also known asglobal attributes): e.g.,
style
,class
andid
Attributes that are specific to certain tags
Some tags always require attributes. For example, the <a>
tag needs the href
attribute. Without the href
attribute, you cannot make a link to another web page. Also, the <img>
tag needs the src
attribute. Without the src
attribute, you cannot display your intended image.
There are optional attributes that can be used to add more instructions. For example, the target
attribute is used for the <a>
tag to define how to open the link. When you specify the attribute as target="_blank"
, you can open the linked page in a new tab in the browser.
We haven't explained these tags and attributes yet, but will do so in the next chapter.
Global attribute
A global attribute is an attribute that can be used in any tag. The most commonly used global attributes are style
, class
, and id
, which are mainly used for CSS. How to use these attributes will be explained later.