zoukankan      html  css  js  c++  java
  • CSS 圣杯/双飞翼布局

    常用布局

    圣杯布局

    html结构
     <body>
        <div class="container clearfix">
          <div class="center"></div>
          <div class="left"></div>
          <div class="right"></div>
        </div>
        <div>
          <div>123</div>
          <div>123</div>
          <div>123</div>
        </div>
      </body>
    
    css:
    container

    把左右两边距离空出来

    .container {
        padding: 0 200px;
    }
    
    center

    设置widh为100%,即充满整个容器

    .center {
         100%;
    }
    
    浮动

    设置left,center,right左浮动。
    清除container浮动,保证后面的div正常布局

    .left, .center, .right {
        float: left;
    }
    .clearfix::after {
        display: block;
        height: 0;
        content: "";
        visibility: hidden;
        clear: both
    }
    .clearfix {
        *zoom: 1;
    }
    
    left

    使用margin-left: -100%;

    .left {
        position: relative;
        margin-left: -100%;
        left: -200px;
    }
    

    使用margin-right: -200px;;

    .right {
        margin-right: -200px;
    }
    

    为什么不用相对定位来移动

    .right {
    position: relative;
    left: -200px;
    }
    

    会在第二行向左移动200px,不能和center同行

    代码
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style type="text/css">
          html,
          body {
            height: 100%;
            overflow: hidden;
            margin: 0;
            padding: 0;
          }
    
          .container {
            padding: 0 200px;
          }
          .left,
          .right {
             200px;
            min-height: 200px;
          }
          .left {
            background: lightgreen;
          }
          .right {
            background: lightgrey;
          }
          .center {
             100%;
            min-height: 400px;
            background: lightsalmon;
          }
    
          .left,
          .center,
          .right {
            float: left;
          }
          .left {
            position: relative;
            margin-left: -100%;
            left: -200px;
          }
          .right {
            margin-right: -200px;
            /* position: relative; */
            /* left: -200px; */
          }
          .clearfix::after {
            display: block;
            height: 0;
            content: "";
            visibility: hidden;
            clear: both;
          }
          .clearfix {
            *zoom: 1;
          }
        </style>
      </head>
      <body>
        <div class="container clearfix">
          <div class="center"></div>
          <div class="left"></div>
          <div class="right"></div>
        </div>
        <div>
          <div>123</div>
          <div>123</div>
          <div>123</div>
        </div>
      </body>
    </html>
    
    

    双飞翼布局

    HTML结构

     <body>
        <div class="container">
          <div class="cemter"></div>
        </div>
        <div class="left"></div>
        <div class="right"></div>
      </body>
    

    container

    .container {
         100%;
        background: lightseagreen;
        height: 700px;
    }
    

    center

    .center {
        min-height: 400px;
        background: lightpink;
        margin: 0 200px;
    }
    

    浮动

    .container, .left, .right {
        float: left;
    }
    .clearfix::after {
        display: block;
        height: 0;
        content: "";
        visibility: hidden;
        clear: both;
    }
    .clearfix {
        *zoom: 1;
    }
    

    left

    .left {
            margin-left: -100%;
             200px;
            min-height: 400px;
            background: lightskyblue;
          }
    

    right

    .right {
            margin-left: -200px;
             200px;
            min-height: 400px;
            background: lightgoldenrodyellow;
          }
    
    代码
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style type="text/css">
          html,
          body {
             100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
          }
          .container {
             100%;
            background: lightseagreen;
            height: 700px;
          }
          .center {
            min-height: 400px;
            background: lightpink;
            margin: 0 200px;
          }
          .container,
          .left,
          .right {
            float: left;
          }
          .left {
            margin-left: -100%;
             200px;
            min-height: 400px;
            background: lightskyblue;
          }
          .right {
            margin-left: -200px;
             200px;
            min-height: 400px;
            background: lightgoldenrodyellow;
          }
          .clearfix::after {
            display: block;
            height: 0;
            content: "";
            visibility: hidden;
            clear: both;
          }
          .clearfix {
            *zoom: 1;
          }
        </style>
      </head>
      <body>
        <div class="container">
          <div class="center"></div>
        </div>
        <div class="left"></div>
        <div class="right"></div>
      </body>
    </html>
    

    图解:

  • 相关阅读:
    mybatis框架查询用户表中的记录数
    文件的上传和下载
    怎样在一条sql语句中将第一列和第二列加和的值作为第三列的值
    [OS] 进程的虚地址空间
    [网络] TCP/IP协议族各层的协议汇总
    [面试] C++ 虚函数表解析
    [OS] 堆栈、堆、数据段、代码段
    [算法] 并查集概念及其实现
    [OS] 我与大牛的对话!
    [C] int *p[4]与int (*q)[4]的区别
  • 原文地址:https://www.cnblogs.com/xiaoxu-xmy/p/13695754.html
Copyright © 2011-2022 走看看