What is the difference between id and class in CSS and when should I use them

0 votes
#main {
    background: #000;
    border: 1px solid #AAAAAA;
    padding: 10px;
    color: #fff;
    width: 100px;
}
<div id="main">
    Welcome
</div>

Here I gave an id to the div element and it's applying the relevant CSS for it.

OR

.main {
    background: #000;
    border: 1px solid #AAAAAA;
    padding: 10px;
    color: #fff;
    width: 100px;
}
<div class="main">
    Welcome
</div>

Now that the div has a class, it is working for me in the exact way that I intended.

So what exactly is the difference between an Id and a class, and when should I use one over the other? Being new to CSS and web design, I am a little bit perplexed by this.

Aug 18, 2022 in CSS by Edureka
• 13,620 points
420 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In CSS

0 votes
1 answer

when declaring css styles, what is the meaning of .classA.classB (two class names with a dot in the middle and no space)

The first example (space-separated classes) is a ...READ MORE

answered Jun 28, 2022 in CSS by Edureka
• 12,690 points
870 views
0 votes
0 answers

what is the difference between xx-large and larger in CSS?

I came across this query when reading ...READ MORE

Aug 19, 2022 in CSS by Edureka
• 13,620 points
643 views
0 votes
0 answers

what is the difference between xx-large and larger in CSS?

I came across this query when reading ...READ MORE

Aug 22, 2022 in CSS by Edureka
• 13,620 points
493 views
0 votes
1 answer

What is the difference between CSS and SCSS?

CSS, or Cascading Style Sheets, is a ...READ MORE

answered Jun 17, 2022 in CSS by Edureka
• 12,690 points

edited Mar 5 660 views
0 votes
0 answers
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
10,967 views
0 votes
1 answer

Using/Handling colon inside a JSF generated HTML element ID in CSS selector

Yes, you can.  Just Backslash (\) the colon.  Like ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,250 points
2,971 views
0 votes
1 answer

What is a css selector and where is it used?

CSS Selector is a combination of element ...READ MORE

answered Nov 21, 2018 in Data Analytics by Kalgi
• 52,350 points
1,036 views
0 votes
1 answer

What is the difference between span and div?

divs are block element that has no ...READ MORE

answered Jul 2, 2022 in Web Development by Ayobami Haastrup

edited Mar 5 3,924 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP