Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Attributes, Classes, Ids, and Styles | Understanding Basic Elements
HTML for Beginners

book
Attributes, Classes, Ids, and Styles

In our last lesson, we learned that the <a> tag uses an attribute called href. Attributes are used to provide additional instructions to a specific tag. We can assign values to most of the attributes. So they often behave like variables.

Attributes are permanently attached to a particular element. The purpose of an attribute is different from one to another. There are mainly two types of attributes: global attributes and element-based attributes. The most popular global attributes are id, class, and style.

Id attribute

It is used to recognize an element uniquely. This is helpful when writing CSS or Javascript codes targeting that specific element later.

html

index.html

copy
<p id="greet">Welcome to our website</p>

They don't change anything in the presentation of the code. However, if you want to change the color of this specific paragraph, you can write CSS code using the id element, as shown below.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html>
<!-- Go to the index css file -->
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p id="greet">Welcome to our website</p>
</body>
</html>

Class attribute

The class attribute is used to categorize a set of elements. You can give the same class for several elements. So you can write CSS and Javascript codes targeting that set of elements.

html

index.html

copy
<p class="welcome">This sentence belongs to the welcome class</p>
<p class="welcome">This sentence also belongs to the welcome class</p>
<p>This sentence does not belongs to the welcome class</p>
<p class="welcome">This sentence also belongs to welcome class</p>

Here first, second and fourth paragraphs belong to the "welcome" class. You can write CSS code to change those three sentences leaving the third one unchanged.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html>
<!-- Go to the index css file -->
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p class="welcome">This sentence belongs to the welcome class</p>
<p class="welcome">This sentence also belongs to the welcome class</p>
<p>This sentence does not belongs to the welcome class</p>
<p class="welcome">This sentence also belongs to welcome class</p>
</body>
</html>

Styles

A style attribute is used to write inline CSS for an HTML element. The format for style attribute is:

html
<tag style="css-property: value">content</tag>
html

index.html

copy
<!DOCTYPE html>
<html>
<head> </head>
<body>
<p style="color: red">This paragraph will be red</p>
</body>
</html>

You can use hundreds of CSS properties with the style attribute.

There are other global attributes. But they are not commonly used. Apart from global attributes, there are a lot of attributes that can be used only with certain elements. You will learn a lot about them in future chapters.

1. Which attribute is used to uniquely recognize an element?

2. How to change the color of a heading?

question mark

Which attribute is used to uniquely recognize an element?

Select the correct answer

question mark

How to change the color of a heading?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
We use cookies to make your experience better!
some-alt