Is using the same id multiple times on a page bad practice even when I am not parsing through the page?

大兔子大兔子 提交于 2019-12-01 16:13:48

You only know what the current requirements are.

It is best to keep to standards and to valid code/markup as you don't know what will happen in the future.

In this application, you may very well end up having to use jQuery and then you will be in trouble.

The solution for styling multiple elements the same way is to use CSS classes - there is absolutely no reason not to do so, as it is just as simple as using ids.

http://www.w3.org/TR/WCAG20-TECHS/H93.html

http://www.w3.org/TR/html401/struct/global.html#adef-id

There are other things that read your HTML document, not just browsers.

According to the HTML specification, the id attribute MUST be unique on a page (it's not a criteria web designers/developers just invented)

Absolutely. The specs for HTML, CSS, and JavaScript are an agreement as to how a browser will behave when confronted with certain code. Browsers don't make many guarantees when you do anything that's off-spec, and they make even fewer that they would all agree on.

As Oded mentioned in his comment on the question, browsers build a DOM tree from your code, style it, and render it. So, depending on how a given browser decided to do that, they may run into problems with multiple elements with the same id.

So, why risk it? I can't think of any instance where a class (or nothing at all) couldn't be used in place of an id.

yes it is bad practice. An id should be a unique reference to that element. Use class instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!