Languages

What is the difference between id and class in CSS?

3 points
Asked by:
AnnLen
1771

I can assign styles using id and class.

Which one should I use and why?

I can achieve the same result using class and id.

Example using class:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
    <style>
        .test {
            color: red;
        }
    </style>
</head>
<body>
    <div class="test">Hello</div>
</body>
</html>

Example using id:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
    <style>
        #test {
            color: red;
        }
    </style>
</head>
<body>
    <div id="test">Hello</div>
</body>
</html>

In both examples text changes color from black (default color) to red color.

0 answers
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.