Search code examples
htmlcsshtml4

why use id's & class's in html why not just one?


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


Solution

  • 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.