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

后端 未结 8 1671
不思量自难忘°
不思量自难忘° 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 12:11

    I have heard that ID is unique and can only be used once in a page

    That is an HTML rule and has nothing to do with CSS.

    but its working fine when used over multiple times on a page.

    Browsers perform error recovery. Don't depend on it as not all browsers will recover from all errors in the same way. Write valid markup.

    Please let me know the purpose of ID and hows its exactly different from classes?

    In HTML terms — an id is unique per document and can be a link target. A class can be reused.

    In CSS terms — An id selector has a higher specificity than a class selector.

提交回复
热议问题