ID vs CLASS in CSS. Please explain in detail with example

后端 未结 8 1852
不思量自难忘°
不思量自难忘° 2021-01-29 11:46

I have heard that ID is unique and can only be used once in a page, but its working fine when used over multiple times on a page. Please let me know the purpose of ID and hows i

8条回答
  •  甜味超标
    2021-01-29 11:52

    Using the same ID for multiple items causes your HTML to be invalid.

    The simple answer is just that. If you're using an ID (a unique identifier) multiple times you're invalidating your HTML.

    Just as chewing gum can be used to stick a painting to a wall, the same ID can be used multiple times because HTML doesn't handle errors in a draconian fashion. It still doesn't make it correct or even a good use of the attribute.

    Classes are designed so that you can relate multiple elements on a page. This is what it's designed for.

提交回复
热议问题