Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Davide Pugliese
Davide Pugliese
4,091 Points

<div> When is it needed? - Coding best practices

Hello, I am taking the time to test and make practice with what I reviewed to improve my skills. One thing that I noticed immediately sitting now and starting to code is Nick Pettit clean way of coding. He just does not add code unless is really necessary (and it's a good thing considering that our code is going to be reviewed also by Google Page Speed for SEO purposes later on or at least I always check that as well).

 <header>
      <a href="index.html" id="logo">
        <h1>Nick Pettit</h1>
        <h2>Designer</h2>
      </a>
      <nav>
        <ul>
          <li><a href="index.html" class="selected">Portfolio</a></li>
          <li><a href="about.html">About</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>

Many people I know of (which it does not mean they are doing it right) would probably use div like so:

<div  id="logo">
       <a href="index.html">
          <h1>Nick Pettit</h1>
          <h2>Designer</h2>
       </a>
</div>
<div id="navigation">
          <nav>
            <ul>
              <li><a href="index.html" class="selected">Portfolio</a></li>
              <li><a href="about.html">About</a></li>
              <li><a href="contact.html">Contact</a></li>
            </ul>
        </nav>
</div>

HTML 5 now provides also the section element. Therefore I ask myself, with HTML 5, when a div is really needed.

Thank you,

Davide

1 Answer

I usually use divs when it doesn't make sense to use anything else. As a container, for instance.

<header id="header" class="wrapper">
  <div class="container">
    <!-- more code here -->
  </div>
</header>

Nothing else really seems to fit in that particular job, so I just use a div. :) In this area, nothing you do or I do or anyone else does is inherently wrong, but I do think they're missing out on a valuable thing.

As an aside, while we're talking about semantic HTML, I have written a big post on good practices. It's a little long, though.