Languages

What is the difference between id and class in CSS?

3 points
Asked by:
Jacob
342

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
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join