ID VS CLASS
id and class in HTML
?
<div class=”item”>Content</div>
<div id=”item”>Content</div>
In HTM, both classes and IDs are attributes used to identify and target specific elements for styling
and Javascript manipulation.
?
idnbsp;attribute nbsp;
IDs must be unique within an HTML document. Each element can have only one unique ID.
Using the same ID for multiple elements is considered invalid HTML and can lead to unexpected behaviour.
classnbsp;attribute nbsp;
Classes, on the other hand, do not need to be unique and can be used to target multiple elements on the page. It is a common practice to use classes to group elements that share similar styles or behavior.
Note:
IDs are handy for targeting a single, specific element in Javascript or CSS. For example, you might use an ID to target a navigation bar or a unique section of a page. Styling or manipulating an element by its ID will take precedence over styles or manipulations applied using classes.
In the example, The text would be red because the id selector takes precedence over the class selector.
?
<p id=”foo” class=”bar”>Hello!</p>
?
<style>
#foo { color: red };
.bar { color: yellow };
</style>