Languages

What is the difference between id and class in CSS?

3 points
Asked by:
AnnLen
9180

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
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join