HTML5 & Bootstrap class="container", can it be applied to body or only div?

10,815

I recommend sticking to the

<body>
   <div class="container">
      <div class="row">
          <div class="col-md-12"> ...

format.

If you intend to work with a lot other developers or with bootstrap templates- you will see that the container classes typically nest row class divs.

Since we are talking about markup there is no right answer, but following this convention is strongly recommended.

  1. For consistency
  2. For easy changes to styling & reusability with other projects- this even opens the door to drop-in replacements of css stylesheets from other projects or bootstrap templates. (I have had some surprisingly good results with this).

However, if you insist on giving non-div tags "container" and "col-X" tags, be consistent. I wouldn't recommend it though and would consider any template that follows its own convention to be an indicator of poor code quality.

Share:
10,815
Padawan
Author by

Padawan

Updated on July 05, 2022

Comments

  • Padawan
    Padawan almost 2 years

    I keep bumping into this issue where everyone keeps: a) wanting to wrap HTML5 semantic tags with divs, and b) wants to apply class selectors to the divs and not the semantic tags. It's as if people are afraid of slapping classes onto semantic tags for some reason.

    For example, I am constantly told that this is "incorrect",

    <body class="container">
        <header class="row">
            <div class="col-md-12"> ...
    

    And something like this is more preferable,

    <body>
       <div class="container">
          <div class="row">
              <div class="col-md-12"> ...
    

    And here, where the first example I have the column class in the h2 tag

        <div class="row">
            <h2 class="col-4 feature">Featured Work</h2>
        </div>
    

    But "the correct" way is to add yet another div tag to apply the class,

        <div class="row">
            <div class="col-4 feature">
                <h2>Featured Work</h2>
            </div>
        </div>
    

    I understand that this might be opinion-based, but I have found that when dealing with HTML5, opinions actually matter since virtually everyone is having issues and there is no other way to hammer out the details without opinions.