HTML+CSS3的position和float、inline-block实现一样的布局

Web布局有时候可以使用不同CSS/CSS3的属性来实现相同的布局效果的。

1.position和float、inline-block,而float会少一些CSS的代码,简单一些:

1).position实现布局

<style type="text/css">
.container {
  position: relative;
  border: solid #6AC5AC 3px;
}
nav {
  position: absolute;
  left: 0px;
  width: 200px;
  border: solid #ff3399 3px;
  box-sizing: border-box;
}
nav .label, nav .endlabel {
    background: #ff3399;
}
section {
  /* position is static by default */
  position: relative;
  margin-left: 200px;
  border: solid #ffcc66 3px;
  box-sizing: border-box;
}
section .label, section .endlabel {
    background: #ffcc66;
}
body {
  margin-bottom: 120px;
}
.label {
    top: 0;
    left: 0;
    padding: 0 3px 3px 0;
}
.endlabel {
    bottom: 0;
    right: 0;
    padding: 0 3px 3px 0;
}
.label, .endlabel {
    position: absolute;
    background-color: #6AC5AC;
    color: #414142;
    line-height: 1em;
}
footer {
  position: fixed;
  bottom: 0;
  left: 0;
  height: 70px;
  background-color: white;
  border: solid #96C02E 3px;
  width: 100%;
  box-sizing: border-box;
}
footer .label, footer .endlabel {
    background: #96C02E;
}
</style>
<div class="container elem">
    <span class="label">&lt;div class="container"&gt;</span>

    <nav class="elem elem-red">
      <span class="label">&lt;nav&gt;</span>
      <ul>
        <li>
          <a href="position-example.html">Home</a>
        </li>
        <li>
          <a href="position-example.html">Taco Menu</a>
        </li>
        <li>
          <a href="position-example.html">Draft List</a>
        </li>
        <li>
          <a href="position-example.html">Hours</a>
        </li>
        <li>
          <a href="position-example.html">Directions</a>
        </li>
        <li>
          <a href="position-example.html">Contact</a>
        </li>
      </ul>
      <span class="endlabel">&lt;/nav&gt;</span>
    </nav>

    <section class="elem elem-green">
      <span class="label">&lt;section&gt;</span>
      <p>
         <code>section</code> 的 <code>margin-left</code> 样式确保了有足够的空间容纳 <code>nav</code> 元素。
      </p>
      <span class="endlabel">&lt;/section&gt;</span>
    </section>

    <section class="elem elem-green ipsum">
      <span class="label">&lt;section&gt;</span>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.
      </p>
      <span class="endlabel">&lt;/section&gt;</span>
    </section>

    <section class="elem elem-green">
      <span class="label">&lt;section&gt;</span>
      <p>
        注意观察当你调整浏览器窗口时发生了什么。效果很赞!
      </p>
      <span class="endlabel">&lt;/section&gt;</span>
    </section>

    <footer class="elem elem-orange">
      <span class="label">&lt;footer&gt;</span>
      <p>
        如果你使用了一个固定定位的页眉或页脚,确保有足够的空间来显示它们!我在 <code>body</code> 上面加了 <code>margin-bottom</code> 。
      </p>
      <span class="endlabel">&lt;/footer&gt;</span>
    </footer>

</div>

2).float实现布局

<style type="text/css">
.container {
  position: relative;
  border: solid #6AC5AC 3px;
}
.containerLabel {
    left: 0px;
    position: absolute;
    background: #6AC5AC;
}
nav {
  float: left;
  width: 25%;
  border: solid #ff3399 3px;
  box-sizing: border-box;
  overflow: hidden;
}
nav .label, nav .endlabel {
    background: #ff3399;
}
section {
  overflow: auto;
  margin-left: 25%;
  border: solid #ffcc66 3px;
  box-sizing: border-box;
}
section .label, section .endlabel {
    background: #ffcc66;
}
nav .endlabel, section .endlabel {
    float: right;
}
footer {
  position: fixed;
  bottom: 0;
  left: 0;
  height: 70px;
  background-color: white;
  border: solid #96C02E 3px;
  width: 100%;
  box-sizing: border-box;
}
footer .label, footer .endlabel {
    background: #96C02E;
}
</style>
<div class="container elem">
    <span class="label">&lt;div class="container"&gt;</span>

    <nav class="elem elem-red">
      <span class="label">&lt;nav&gt;</span>
      <ul>
        <li>
          <a href="position-example.html">Home</a>
        </li>
        <li>
          <a href="position-example.html">Taco Menu</a>
        </li>
        <li>
          <a href="position-example.html">Draft List</a>
        </li>
        <li>
          <a href="position-example.html">Hours</a>
        </li>
        <li>
          <a href="position-example.html">Directions</a>
        </li>
        <li>
          <a href="position-example.html">Contact</a>
        </li>
      </ul>
      <span class="endlabel">&lt;/nav&gt;</span>
    </nav>

    <section class="elem elem-green">
      <span class="label">&lt;section&gt;</span>
      <p>
         <code>section</code> 的 <code>margin-left</code> 样式确保了有足够的空间容纳 <code>nav</code> 元素。
      </p>
      <span class="endlabel">&lt;/section&gt;</span>
    </section>

    <section class="elem elem-green ipsum">
      <span class="label">&lt;section&gt;</span>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.
      </p>
      <span class="endlabel">&lt;/section&gt;</span>
    </section>

    <section class="elem elem-green">
      <span class="label">&lt;section&gt;</span>
      <p>
        注意观察当你调整浏览器窗口时发生了什么。效果很赞!
      </p>
      <span class="endlabel">&lt;/section&gt;</span>
    </section>

    <footer class="elem elem-orange">
      <span class="label">&lt;footer&gt;</span>
      <p>
        如果你使用了一个固定定位的页眉或页脚,确保有足够的空间来显示它们!我在 <code>body</code> 上面加了 <code>margin-bottom</code> 。
      </p>
      <span class="endlabel">&lt;/footer&gt;</span>
    </footer>

</div>

3).inline-block实现布局

<style type="text/css">
.container {
  position: relative;
  border: solid #6AC5AC 3px;
}
nav {
    float:left;
    position: relative;
    display: inline-block;
    vertical-align: top;
    width: 25%;
    border: solid #ff3399 3px;
    box-sizing: border-box;
}
nav .label, nav .endlabel {
    color: #ffffff;
    background: #ff3399;
}
.column {
    position: relative;
    display: inline-block;
    vertical-align: top;
    width: 75%;
    border: solid #ff3399 3px;
    box-sizing: border-box;
}
section {
    /* position is static by default */
    position: relative;
    /*margin-left: 200px;*/
    border: solid #ffcc66 3px;
    box-sizing: border-box;
}
section .label, section .endlabel {
    background: #ffcc66;
}
footer {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 70px;
    background-color: white;
    border: solid #96C02E 3px;
    width: 100%;
    box-sizing: border-box;
}
footer .label, footer .endlabel {
    background: #96C02E;
}
body {
    margin-bottom: 120px;
}
.label {
    top: 0;
    left: 0;
    padding: 0 3px 3px 0;
}
.endlabel {
    bottom: 0;
    right: 0;
    padding: 0 3px 3px 0;
}
.label, .endlabel {
    position: absolute;
    background-color: #6AC5AC;
    color: #414142;
    line-height: 1em;
}
</style>
<div class="container elem">
    <span class="label">&lt;div class="container"&gt;</span>

    <nav class="elem elem-red">
      <span class="label">&lt;nav&gt;</span>
      <ul>
        <li>
          <a href="position-example.html">Home</a>
        </li>
        <li>
          <a href="position-example.html">Taco Menu</a>
        </li>
        <li>
          <a href="position-example.html">Draft List</a>
        </li>
        <li>
          <a href="position-example.html">Hours</a>
        </li>
        <li>
          <a href="position-example.html">Directions</a>
        </li>
        <li>
          <a href="position-example.html">Contact</a>
        </li>
      </ul>
      <span class="endlabel">&lt;/nav&gt;</span>
    </nav>

    <div class="elem elem-red column">
        <!--<span class="colLabel">&lt;div class="column"&gt;</span>-->
        <section class="elem elem-green">
          <span class="label">&lt;section&gt;</span>
          <p>
             <code>section</code> 的 <code>margin-left</code> 样式确保了有足够的空间容纳 <code>nav</code> 元素。
          </p>
          <span class="endlabel">&lt;/section&gt;</span>
        </section>

        <section class="elem elem-green ipsum">
          <span class="label">&lt;section&gt;</span>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.
          </p>
          <span class="endlabel">&lt;/section&gt;</span>
        </section>

        <section class="elem elem-green">
          <span class="label">&lt;section&gt;</span>
          <p>
            注意观察当你调整浏览器窗口时发生了什么。效果很赞!
          </p>
          <span class="endlabel">&lt;/section&gt;</span>
        </section>
    </div>

    <footer class="elem elem-orange">
      <span class="label">&lt;footer&gt;</span>
      <p>
        如果你使用了一个固定定位的页眉或页脚,确保有足够的空间来显示它们!我在 <code>body</code> 上面加了 <code>margin-bottom</code> 。
      </p>
      <span class="endlabel">&lt;/footer&gt;</span>
    </footer>

</div>

效果如下图:

htmlcss3-responsive-design1

htmlcss3-responsive-design2

以上例子请参考本站页面:

Media响应式Float设计

 

 

One Comment

Leave a comment