zoukankan      html  css  js  c++  java
  • 【CSS】三栏布局的经典实现

    要求:自适应宽度,左右两栏固定宽度,中间栏优先加载;

     1 <!DOCTYPE html>
     2 <html>
     3 
     4 <head>
     5     <title>layout</title>
     6     <style type="text/css">
     7         * {
     8             margin: 0;
     9             padding: 0px;
    10         }
    11         
    12         .wrap-2 {
    13             margin-top: 20px;
    14         }
    15         
    16         .header {
    17             background: #E81D1D;
    18             text-align: center;
    19         }
    20         /*对父元素浮动隐藏,然后三栏利用margin-bottom padding-bottom来实现等高*/
    21         
    22         .body {
    23             overflow: hidden;
    24         }
    25         
    26         .main-2 {
    27             float: left;
    28              100%;
    29             margin-bottom: -9999px;
    30             padding-bottom: 9999px;
    31             background: #17A857;
    32         }
    33         
    34         .main-wrap-2 {
    35             padding: 0px 200px 0 200px;
    36         }
    37         /*margin-left是巧用来做合理的页面布局以至于不被挤到下一行*/
    38         
    39         .sub-2 {
    40             float: left;
    41             margin-left: -100%;
    42              200px;
    43             background: #CEAE13;
    44             margin-bottom: -9999px;
    45             padding-bottom: 9999px;
    46         }
    47         
    48         .extra-2 {
    49             float: left;
    50             margin-left: -200px;
    51              200px;
    52             background: #CEAE13;
    53             margin-bottom: -9999px;
    54             padding-bottom: 9999px;
    55         }
    56         
    57         .footer {
    58             background: #D114C0;
    59             text-align: center;
    60         }
    61     </style>
    62 </head>
    63 
    64 <body>
    65     <div class="wrap-2">
    66         <div class="header">Header</div>
    67         <div class="body">
    68             <div class="main-2">
    69                 <div class="main-wrap-2">
    70                     <p>main-wrap</p>
    71                     <p>main-wrap</p>
    72                 </div>
    73             </div>
    74             <div class="sub-2">
    75                 <p>sub</p>
    76                 <p>sub</p>
    77                 <p>sub</p>
    78             </div>
    79             <div class="extra-2">
    80                 <p>extra</p>
    81                 <p>margin-left:350px; background:#CC0;margin-left:350px; background:#CC0;</p>
    82             </div>
    83         </div>
    84         <div class="footer">Footer</div>
    85     </div>
    86 </body>
    87 
    88 </html>

    三点需要理解的

    一、margin-bottom: -9999px;padding-bottom: 9999px;

    原理:

    同列的div设置一个父级 overflow:hidden 超出部分隐藏

    给同列的div设置css margin-bottom:-10000px; padding-bottom:10000px;

    这样就可以实现三列等高!

    二、

    41             margin-left: -100%;
    50             margin-left: -200px;

    原理:中间列浮动并且宽度是100%,则后面的div的浮动就会被挤到下一行,当使用margin左负值,超过自身的宽度时,这个元素在第一行就有位置了。
    而margin的负值达到-100%时,恰能达到窗口最左侧。


    三、中间栏优先加载的意思

    由于浏览器的显示方式是从上到下一行一行解析代码的,所以如果要让中间列优先加载就需要将中间列的内容写在三列当中的最前页。
    就是先写中间列的div的意思。。。
  • 相关阅读:
    Populating Next Right Pointers in Each Node
    Reverse Linked List
    Reverse Linked List II
    Triangle
    Surrounded Regions
    Effective_STL 学习笔记(十九) 了解相等和等价的区别
    Effective_STL 学习笔记(十八) 避免使用 vector<bool>
    Effective_STL 学习笔记(十七) 使用 “交换技巧” 来修整过剩的容量
    Effective_C++ (条款02) 尽量以 const,enum,inline替换 #define
    Effective_C++ (条款01) 视 C++ 为一个语言联邦
  • 原文地址:https://www.cnblogs.com/lijie33402/p/4570941.html
Copyright © 2011-2022 走看看