What Is ID and CLASS in CSS? Difference Between Id and Class in CSS
Welcome to the Codewithrandom blog. In this blog, we learn What is Id and Class selectors in Html and Css. it’s the most used word in frontend web development.
ID and Class are basically used for targeting HTML Elements and they have their unique use class always used for Multiple Html elements for the same design UI and ID means unique only for 1 Element. So let’s understand all these differences in this blog post. Hope you enjoy our blog so let’s start with the id selector.
Css ID Selector
In HTML, every element on your web page is assigned to be a unique Id attribute. This can be a text that has to be unique (only one item can have this label). Itâs that good to assign labels that describe the element of it. Letâs take a quick example a <ul> tag is there that is used to mark up a navigation menu and that might have id= ânavigationâ or else id = âmenuâ
Youâd assign that id attribute to an HTML element:
1.    If you want to style that specific element differently than other elements of the same type.
2.    Or You want to be able to link to a particular element within a web page. In fact, youâd already added id = âid=” main” to one of the div elements out here.
In this unit, weâre just going to work with ID Selectors. We want to be able to style the specific elements using CSS. For example, letâs say you have a paragraph that serves as a page. Youâd want it to look like all the other paragraphs on the page. You want it to stand out so people are to notice it right? How does the ID work like this:
 Simple HTML Code
<p id="main">Learn with codewithrandom.</p>
Output
![]() |
Output |
So as you can see here main is an ID selector, and thatâs what weâre going to learn how to use it and how to style it letâs see how.
To add style to an element with an id we use a symbol # in your CSS. For example, hereâs how the p id selector has been used in CSS.
50+ HTML, CSS & JavaScript Projects With Source Code
Simple ID Selector in Css Code
p#main { color: red; font-size: 30px; background: #fff; border: 1px solid black; }
Output
Note here that weâve used specifying type of element in CSS and that is optional where the element is having an ID. In the above example, you can see weâve used p#main as an ID selector.
Itâs generally good practice though to include and as It helps you to remember which elements have certain IDs, Sometimes you want to know that just by looking at the CSS file, without having to refer back to its original HTML file.
5+ HTML CSS Projects With Source Code
Now, Letâs see what is class and I hope youâve cleared your concepts of CSS ID selectors:
Css Class Selector
ADVERTISEMENT
What are Class Selectors?
Sometimes you may see that there might be a group of elements that you want to style. For example, letâs say youâre creating a basic web page. So letâs say youâve created a <div> class called = âreviewâ to each. And then you might have created another div class called âsummaryâ. Something like this:
ADVERTISEMENT
Simple HTML code
ADVERTISEMENT
<div class="review" <p>Codewithrandom is the best</p> <p class="summary">Codewithrandom is best for learning tech related blogs, and project and that also with source code. Share it with others and follow him on Instagram for more such contents.</p>
Output
ADVERTISEMENT
To add this style to your element that is part of the class, then we use (.) in CSS. When weâre working with CSS class-based selectors. Hereâs what it looks like:
Simple Class Selector in Css Code
div.review { background-color: #fff; border: 1px solid #ccc; font-size: 25px; } p.summary { font-size: 45px; font-style: italic; padding: 10px; }
Output
This is how your class-based selectors work when youâve created a div class or a paragraph class youâve to also design that and to design that in CSS, we use . to work with it as you can see in the above example that weâve used class selectors and given the font-styles and padding, borders and all then only your CSS Class-based selector is gonna work.
10+ Javascript Projects For Beginners With Source Code
ID selectors are based on their specific ID like letâs say youâve created a paragraph ID and youâve to design that specific paragraph and for that, we use CSS id selectors and then we style that by using # symbol and then we give font-color, background-color, to it.
I hope you found this article helpful, if yes like my content and follow codewithrandom on Instagram, and share this article with anybody who may find this content helpful. Thanks and Stay tuned for another tech-related article.
Darshan
Team codewithrandom