treehouse : what would you like to learn today?
Web Design Web Development iOS Development

required: div class or id ?

  • hey , just wondering, im making just a quick runndown static website and using html5. but remembered that in one of my previous posts that the HTML 5 wasn't cross browser proper for me. i dont know it was mental!

    but here's the code. so far
    <!DOCTYPE html>
    <html>
    <head>
    <title>THIS IS MY WEBPAGE TITLE</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
    </head>

    <body>

    <div id="wrapper">

    <header>
    <h1>This is the website title Inside the header</h1>
    <p>This is the some quote or sloagen</p>
    </header>

    <nav>
    <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Media</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
    </ul>
    </nav>

    <div id="main-content">
    <p>This may change per page.</p>
    <p>This may change per page.</p>
    <p>This may change per page.</p>
    <p>This may change per page.</p>
    </div>

    <footer>
    <div>This is your footer copywrite section.</div>
    </footer>

    </div>

    </body>
    </html>



    so my real question was, should i be naming every div? (giving it an "ID" or "Class"?? if so ? howcome
    or why not?
    and is it good practice?
  • XHTML
    <link rel="stylesheet" href="style.css" type="text/css" />



    HTML5
    <link rel="stylesheet" href="style.css" type="text/css">



    Also, HTML5 has
    <content>
    so you dont have to use your #main-content div.


    I usually only give main sections an ID and use classes for little things within the ID.
  • oh sweet!!!! thank you!!
  • So, heres what the difference between classes and IDs are.

    IDs are used for elements that don't repeat (ones that you only have one of) such as #footer
    Classes are used for repeating things such as an LI element inside of a UL.

    Your code wont validate if you have more than one of the same ID on the same page.

    Hope that helps
  • no no i mean

    do you have do give a div an ID or class?
    like, is it also best practice to leave a few blank? or no
  • You don't HAVE to - depends what you want to do really.
  • You don't have to give it anything apart from the name
    however I fell it helps to know what the point of the div is for or how it is to be styled and so give it a meaningful id or class