18

I understand the concept of one id in an html page. As a noob, i was just wondering if you can use the same id once for different html pages. Will that be consider sloppy?

For instance, <div id="1"></div> (used only once)in index.html and used <div id="1"></div> again in product.html. Is that consider bad?

I tried to do a search but found no answers. Thank you!

!edit! Thank you for the answer guys. Appreciated!

2
  • 1
    Yes, you can use the same ID on different pages. In fact, it's quite common to do so. You have to be aware of tricky cross-page inclusions, though. (PHP's require(), I'm looking at you!)
    – user529758
    Aug 16, 2013 at 7:42
  • 2
    If the pages r truly alone, its ok, but once you use PHP or something to combine them (eg. include), you may get two of the same ID on one page, and that's bad.
    – MightyPork
    Aug 16, 2013 at 7:45

2 Answers 2

40

An id should only be used once on a single document. It is used for elements that only should appear once on the page anyway (think of a "top navigation bar"). Classes are used for elements that can appear more than once (think of a "particularly styled table", a "repeatable block of information" or things that share particular charasteristics such as "on this browser width this block spans 6 columns" in for example bootstrap). It is perfectly normal to use the same id on different pages. Usually you'll make a skeleton/template for your layout, where each element will be styled the same on each page that uses this template. It is then helpful to have the same id for the same element across different pages. (or: It would be considered sloppy to change the layout of the page on every page, using different id's for each element, as it would be hard or impossible to maintain your pages.)

2
  • 1
    Sorry that i dont have enough reputation to upvote. But thanks for the explanation. I just want to make sure i'm doing everything right the first time. Aug 16, 2013 at 7:57
  • 1
    @stanley1943 I think now you have, so go on and upvote, if you haven't done already! :)
    – gsamaras
    Aug 15, 2016 at 21:38
4

It's fine to do that, but you would want to name your ids something better than "1". Something more descriptive, like <div id="main-part"></div> would be better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.