CSS如何实现页面两列布局与三列布局

来源:爱站网时间:2020-03-05编辑:网友分享
在css中布局是非常重要的,但是有很多小伙伴们都不知道CSS如何实现页面两列布局与三列布局,那么接下来的内容中我们就和爱站小编一起去看看CSS实现页面两列布局与三列布局的方法吧。

css中布局是非常重要的,但是有很多小伙伴们都不知道CSS如何实现页面两列布局与三列布局,那么接下来的内容中我们就和爱站小编一起去看看CSS实现页面两列布局与三列布局的方法吧。

1. 使用BFC的原理实现BFC的规则之一,就是BFC区域,不会与float box重叠,因此我们可以利用这一点来实现3列布局。
html代码如下

 

 

XML/HTML Code复制内容到剪贴板
  1. <div class="left">div>  
  2. <div class="right">div>  
  3. <div class="main">div>  

css代码如下

 

 

CSS Code复制内容到剪贴板
  1. .left {   
  2.   floatleft;   
  3.   margin-right10px;   
  4.   width100px;   
  5.   height100px;   
  6.   background-color: orange;   
  7. }   
  8. .rightright {   
  9.   floatrightright;   
  10.   margin-left10px;   
  11.   width100px;   
  12.   height100px;   
  13.   background-color: orange;   
  14. }   
  15. .main {   
  16.   height100px;   
  17.   background-colorgreen;   
  18.   overflowhidden;   
  19. }  

2.双飞翼布局
这种布局方案最早由淘宝提出,主要为了主列能够被最先加载。
实现原理:
(1)让主列外面添加一个wrap,主列wrap,以及2子列都左浮动。
(2)设置主列wrap宽度为100%,将子列的margin-left设置为负值,让子列能够排列在左右两侧。
(3)这是主列的margin-left与margin-right比左右两列的宽度大一点,则可以设置出来主列与子列之间的间隙。
html代码如下

 

 

XML/HTML Code复制内容到剪贴板
  1. <div class="wrap">  
  2.   <div class="main-content">  
  3.     <div class="main">div>  
  4.   div>  
  5.   <div class="left">div>  
  6.   <div class="right">div>  
  7. div>  

css代码如下

 

 

CSS Code复制内容到剪贴板
  1. .wrap {   
  2.   width: 100%;   
  3. }   
  4. .wrap::after {   
  5.   displayblock;   
  6.   content'';   
  7.   font-size: 0;   
  8.   height: 0;   
  9.   clearboth;   
  10.   zoom: 1;   
  11. }   
  12. .main-content {   
  13.   floatleft;   
  14.   width: 100%;   
  15. }   
  16. .main {   
  17.   height100px;   
  18.   background-colorgreen;   
  19.   margin-left110px;   
  20.   margin-right110px;   
  21. }   
  22. .left {   
  23.   floatleft;   
  24.   width100px;   
  25.   height100px;   
  26.   background-color: orange;   
  27.   margin-left: -100%;   
  28. }   
  29. .rightright {   
  30.   floatleft;   
  31.   width100px;   
  32.   height100px;   
  33.   background-color: orange;   
  34.   margin-left: -100px;   
  35. }  

3.圣杯布局
圣杯布局在结构上要简单一点,也能够让主列优先加载。
实现原理:
(1)添加一个包裹框,设置padding-leftpadding-right值,比子列宽度大一个空隙的宽度。
(2)主列,子列都设置为float: left 左子列margin-left设置为-100%,并且设置为position:relative; left: -110px将左子列放置到左侧。右子列同理。
(3)主列只需设置宽度为100%即可。包裹框的宽度不要设置为100%,自适应即可。
html代码如下

 

 

XML/HTML Code复制内容到剪贴板
  1. <div class="wrapper">  
  2.   <div class="main">div>  
  3.   <div class="left">div>  
  4.   <div class="right">div>  
  5. div>  

css代码如下

 

 

CSS Code复制内容到剪贴板
  1. .wrapper {   
  2.   padding-left110px;   
  3.   padding-right110px;   
  4.   overflowhidden;   
  5. }   
  6. .main {   
  7.   floatleft;   
  8.   width: 100%;   
  9.   height100px;   
  10.   background-color#ccc;   
  11. }   
  12. .left {   
  13.   floatleft;   
  14.   width100px;   
  15.   height100px;   
  16.   margin-left: -100%;   
  17.   positionrelative;   
  18.   left: -110px;   
  19.   _left: 0;   
  20.   background-color: orange;   
  21. }   
  22. .rightright {   
  23.   floatleft;   
  24.   width100px;   
  25.   height100px;   
  26.   background-color: orange;   
  27.   margin-left: -100px;   
  28.   positionrelative;   
  29.   rightright: -110px;   
  30. }  

下面再给出一个高度占满全屏的例子:

 

 

CSS Code复制内容到剪贴板
  1.      

  2. "_blank" href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml">      xmlns=
  3. "server">      runat=
  4.      "Content-Type" content="text/html; charset=utf-8" />     
  5.          
  6.     >      type=
  7.         body, html {     
  8.             margin0px;     
  9.         }     
  10.         #header {     
  11.             backgroundblue;     
  12.             height100px;     
  13.             width: 100%;     
  14.             positionrelative/*父div的位置设置成相对的*/     
  15.             top: 0;     
  16.         }     
  17.             #header #h_menu {     
  18.                 position:absolute;     
  19.                 bottombottom:0;     
  20.                 background:yellow;     
  21.                 width:100%;     
  22.                 height:50px;     
  23.             }     
  24.         #middle {     
  25.             position:absolute;     
  26.             width:100%;     
  27.             height:auto;     
  28.             top100px;     
  29.             bottombottom:50px;                       
  30.         }     
  31.         .left {     
  32.             width: 15%;    /*这里是百分比或者像素值,对应下面的center就是百分比或者像素值*/     
  33.             backgroundred;     
  34.             floatleft;     
  35.             height:100%;     
  36.         }     
  37.         .rightright {     
  38.             width: 15%;  /*这里是百分比或者像素值,对应下面的center就是百分比或者像素值*/     
  39.             height: 100%;     
  40.             background: pink;     
  41.             floatrightright;     
  42.         }     
  43.         .center {     
  44.             height: 100%;     
  45.             backgroundgreen;     
  46.             /*两种方式均可(一)margin(二)margin-left、margin-right*/     
  47.             /*(一)、用这种方式上面的left和right中的width是百分比或者像素值都可以*/     
  48.             marginauto;         
  49.             /*(二)、这里是百分比或者像素值,对应上面的left、right中的width就是百分比或者像素值*/     
  50.             /*margin-left:15%;        
  51.             margin-right:15%;*/     
  52.         }     
  53.         #footer {     
  54.             backgroundblue;     
  55.             height50px;     
  56.             width: 100%;     
  57.             positionabsolute;     
  58.             bottombottom: 0;     
  59.         }     
  60.          
  61.      
  62.  
  63.     
    "form1" runat="server">     
     id=
  64.         
         
  65.             
    "header">     
     id=
  66.                 上       
  67.                 
    "h_menu">     
     id=
  68.                     上_底     
  69.                 
                  
  •             

     

 

 

  •             
    "middle">     
     id=
  •                 
    "left">     
     class=
  •                     中左     
  •                      
  •                 
    "right">     
     class=
  •                     中右     
  •                      
  •                 
    "center">     
     class=
  •                     中间     
  •                      
  •                  
  •             
    "footer">     
     id=
  •                 下     
  •                  
  •              
  •          
  •      
  •      
  •  

        

看完后你知道CSS如何实现页面两列布局与三列布局了吗?希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

上一篇:Web前端绘制0.5像素的方法

下一篇:CSS3实现清新Loading效果的实例

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载