0

i understand what there is a difference between id's and class's - ids and ment to be given to a unique item, and class's and like tags, they can be given to multiple items..

but why do you need to have both, in the past i used to write everything using #id (this was when i was new to writing html and didnt know any different) and the code still worked.. would not have been 100% valid but still worked.

i know now using html5 you can define areas differently, but for this im not talking about using html5

2
  • 1
    You don't need classes in HTML at all, they serve no purpose. But they come in handy together with other technologies, such as JavaScript (DOM manipulation) or CSS (styling). On the other side, you could ask this question in a different context: Here on SO, each question has an ID (unique) but each question also has tags (multiple questions per tag). Do you see why? It provides a way of grouping which cannot be achieved through IDs alone. Commented Aug 9, 2012 at 13:49
  • Read this -> css-tricks.com/the-difference-between-id-and-class Commented Aug 9, 2012 at 13:50

4 Answers 4

4

Imagine if you had a load of buttons that looked the same on the page. Would you want to repeat yourself numerous times in your CSS? Or use a class and write it once.

This is applicable in any case of the same styling on different elements.

Sign up to request clarification or add additional context in comments.

Comments

2

Adding to Sam's answer. Ids are helpful both for styling and javascript. If you want only a particular style applied or want to increase the specificity of your css id come handy.

Comments

1

#id is used to describe the document structure or important containers/elements and they must be unique entries.

.class is used to describe elements which are can be many elements in this documents.

If you try to validate the document and have many id's this will be error and if you want your document to be valid you must rename the id's of use classes.

Classes are useful because they can be many classes with one name on your document and you will not be force to copy/paste style from one element to another and change only the tag to describe the new element.

Comments

0

concise answer:

id - Shows on your page once.

class -Shows on your page more than once.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.