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

我的梦境 提交于 2019-12-01 13:58:45

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/27951814/html5-bootstrap-class-container-can-it-be-applied-to-body-or-only-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!