css div id used only once per page

后端 未结 7 541
醉梦人生
醉梦人生 2021-01-14 03:27

In using CSS is it best practice to use a div id only once per page. I know each id has to be unique but could be used multiple times. A discussion with my development tea

相关标签:
7条回答
  • 2021-01-14 03:31

    First, tags usually refer to elements such as div, a, img, body, ... Attributes are values given inside the tags such as id, class, href, ...

    The id attribute can be specified once for each tag, with the constrain that no two tags share the same id value.

    Both your examples are valid.

    0 讨论(0)
  • 2021-01-14 03:33

    id should be unique

    see HTML spec

    0 讨论(0)
  • 2021-01-14 03:41

    If it is related to the CSS only then we can also use Class instead of Id for DIV tag

    0 讨论(0)
  • 2021-01-14 03:45

    You can have many elements with id(s) if that's what you're asking. Just view the source code of this page for instance and you'll see many id(s).

    0 讨论(0)
  • 2021-01-14 03:50

    It is physically possible to have duplicate id's per page, but the reason you only want to use one id per page with CSS is because of CSS selectors. Doing a CSS select by id is expected to only return a single DOM item.

    0 讨论(0)
  • 2021-01-14 03:51

    You can certainly use the id="" attribute as many times as you need, but the contents of the attribute should be unique. Not having a unique value is a HTML error.

    If you need multiple items to have the same attribute, then you can set them as a class.

    More info is at the W3C - Element identifiers: the id and class attributes (the HTML 4.01 Specification).

    0 讨论(0)
提交回复
热议问题