整理笔记4: CSS布局重点(中: 常见的float布局)

113 阅读1分钟

CSS布局重点(中: 常见的float布局)

我是knockkey, 这是我坚持更新博客第2天. 更新这篇博客是为了解决: 常见的两种float布局, 虽然float布局已经不经常用了, 但我认为这是必须要学会的基础.

知识点:

  • 如何实现圣杯布局和双飞翼布局
  • 这两种布局的目的:
    • 三栏布局, 中间一栏最先加载和渲染(内容最重要)
    • 两侧内容固定, 中间内容随着宽度自适应
    • 一般用于PC网页
  • 手写clearfix

1. float布局: 圣杯布局

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      min-width: 550px;
    }

    #header {
      text-align: center;
      background-color: #f1f1f1;
    }

    /*  */
    #container {
      padding-left: 200px;
      padding-right: 150px;
    }

    #container .colum {
      float: left;
    }

    #center {
      background-color: #ccc;
      width: 100%;
    }


    #left {
      /*  */
      position: relative;
      background-color: yellow;
      width: 200px;
      /*  */
      margin-left: -100%;
      right: 200px;

    }

    #right {
      background-color: red;
      width: 150px;
      /*  */
      margin-right: -100%;

    }

    #footer {
      /* 清楚浮动 */
      clear: both;
      text-align: center;
      background-color: #f1f1f1;
    }

    /* 手写clearfix */
    /* .clearfix:after {
      content: '';
      display: table;
      clear: both;
    } */

  </style>
</head>

<body>
  <div id="header">this is header</div>
  <div id="container" class="clearfix">
    <div id="center" class="colum">this is center</div>
    <div id="left" class="colum">this is left</div>
    <div id="right" class="colum">this is right</div>
  </div>
  <div id="footer">this is footer</div>
</body>

</html>

2.float布局: 双飞翼布局

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    boyd {
      min-width: 550px;
    }

    .col {
      float: left;
    }

    #main-wrap {
      margin: 0 190px 0 190px;
    }

    #main {
      width: 100%;
      height: 200px;
      background-color: #ccc;
    }

    #left {
      width: 190px;
      height: 200px;
      background-color: #0000FF;
      margin-left: -100%;
    }

    #right {
      width: 190px;
      height: 200px;
      background-color: #FF0000;
      margin-left: -190px;
    }
  </style>
</head>

<body>
  <div id="main" class="col">
    <div id="main-wrap">this is main</div>
  </div>
  <div id="left" class="col">this is left</div>
  <div id="right" class="col">this is right</div>
</body>

</html>