Hello everyone!
what is different bettween ID(#name) and class(.name) in CSS?
but i think it is the same!


Pleae tell me!
<URL SNIPPED>

Dani AI

Generated

Good concise answers from , and — they covered the basic intent. Here are the practical details that matter when you build real pages: how selector specificity affects which rule wins, when an ID really belongs, and simple troubleshooting steps to avoid fragile CSS.

Specificity is what makes an ID-based rule harder to override than a class or a tag. For example, the ID rule below will win over the class and element rules when all three target the same element:

/* element selector */
button { color: black; }

/* class selector */
.button-cta { color: blue; }

/* id selector */
#signupBtn { color: red; }

Best practice: prefer classes for styling (they are reusable and easy to override). Reserve IDs for things that must be unique: fragment targets, for/aria relationships, or a single JavaScript hook. Avoid relying on ID selectors as the main styling method because they increase specificity and make later overrides painful. For stable JS hooks, consider data attributes instead of styling IDs.

Troubleshooting tips: if a style does not apply, inspect the element in the browser dev tools to see which rule wins and why. Avoid !important as a first fix; instead reorganize selectors or use a more targeted class. For authoritative references, see MDN on CSS specificity and the HTML id attribute: CSS specificity and id attribute.

Recommended Answers

All 4 Replies

ID can be used only once in a page but class can be repeated any number of times.

You would use ID for something that will be used only once per page, like wrapper , header or footer .

Class can be used as many times as you desire, and would be used for something like post_title , post_content or post_metadata .

(blogishly speaking)

Both answers are completely correct. Rule of thumb though, IDs are unique, classes aren't, hence only using them once in a page.

Thank everyone I see!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.