css多栏布局(双栏布局、三栏布局、圣杯布局、双飞翼布局)

5 篇文章 0 订阅
1 篇文章 0 订阅
1.两栏布局

两栏布局的核心是左栏固定宽度,右栏宽度自适应

html结构为

  <div class="outer">
        <div class="left"></div>
        <div class="right"></div>
    </div>

方式一

左栏浮动+固定宽度

右栏宽度auto+margin偏移

.outer{
    height: 500px;
    background-color: black;

}
.left{
    height: 100%;

    float: left;
    width: 200px;
    background-color: red;
    

}
.right{

    height: 100%;

    margin-left: 200px;
    width: auto;
    background-color: blue;
}

方式二

左栏浮动+固定宽度

右栏设置overflow:hidden(目的是产生bfc,不与左栏重叠)

.outer{
    height: 500px;
    background-color: black;

}
.left{
    height: 100%;

    float: left;
    width: 200px;
    background-color: red;
    

}
.right{

    height: 100%;

    overflow: hidden;
   
    background-color: blue;
}

方式三

父级设置flex布局

左栏固定宽度

右栏flex:1

.outer{
    height: 500px;
    background-color: black;
    display: flex;

}
.left{
    


    width: 200px;
    background-color: red;
    

}
.right{

    flex: 1;
    background-color: blue;
}

方式四

父级相对定位

左栏绝对定位

右栏设置margin-left

.outer{
    height: 500px;
    background-color: black;
    position: relative;

}
.left{
    height: 100%;

    position: absolute;
    width: 200px;

    background-color: red;
    

}
.right{

   height: 100%; 

   margin-left: 200px;
   background-color: blue;
}

方式五

父级相对定位

左栏固定宽度

右栏绝对定位+偏移

.outer{
    height: 500px;
    background-color: black;
    position: relative;

}
.left{
    height: 100%;

    
    width: 200px;

    background-color: red;
    

}
.right{

   height: 100%; 

   position: absolute;

    left: 200px;
    bottom: 0;
    top: 0;
    right: 0;
   
   background-color: blue;
}
2.三栏布局

三栏布局的核心是左右两栏固定宽度,中间一栏宽度自适应

dom结构为

  <div class="outer">
        <div class="left"></div>
        <div class="right"></div>
        <div class="center"></div>
    </div>

方式一

左栏左浮动 固定宽度

右栏右浮动 固定宽度

中间一栏设置左右偏移 并且处于html结构的最后

.outer{
    height: 500px;
    background-color: black;

}
.left{
    height: 100%;

    width: 200px;
    float: left;
    background-color: red;

}

.center{
    height: 100%;


    margin-left: 200px;
    margin-right: 200px;
    background-color: green;

}

.right{

    height: 100%;

    width: 200px;

    float: right;
   
    background-color: blue;
}


方式二

左右栏绝对定位

中间一栏设置偏移

.outer{
    height: 500px;
    background-color: black;
    position: relative;

}
.left{
    height: 100%;
    width: 200px;
    position: absolute;
    background-color: red;

}

.center{
    height: 100%;
    margin-left: 200px;
    margin-right: 200px;
    background-color: green;

}

.right{

    height: 100%;
    position: absolute;
    width: 200px;
    right: 0;
    top: 0;
    background-color: blue;
}


方式三

父级设置flex布局

左右栏固定宽度

中间一栏设置flex:1

.outer{
    height: 500px;
    background-color: black;
    display: flex;

}
.left{

    width: 200px;
   
    background-color: red;

}

.center{

    flex: 1;
    background-color: green;

}

.right{

    width: 200px;
   
    background-color: blue;
}


圣杯布局

方法:

1.给三栏都设置左浮动 让中间一栏占宽百分百,使得左右两栏下移

2.给中间一栏设置左右padding,用来给左右两栏设置存放空间

3.设置左栏的margin-left为-100%,这样左栏就会移动到中间一栏的最左边

4.给右栏设置margin-left为负的右栏的宽度,这样右栏就会移动到中间一栏的最右边

5.左右两栏使用相对定位进行偏移,移动到之前给他们准备好的空间中

为什么margin-left为负数会使元素上移?

答:浏览器会判断margin-left与元素宽度,如果margin-left+width<0,浏览器就会认为元素的宽度小于0,

​ 上一层是可以容纳这个元素的,于是就会上移

.outer{
    height: 500px;
    background-color: black;
    padding-left: 200px;
    padding-right: 200px;

}
.left{

    position: relative;
    left: -200px;

    float: left;
    margin-left: -100%;

    width: 200px;
    height: 100%;
   
    background-color: red;

}

.center{

    width: 100%;
    height: 100%;
    float: left;
    background-color: green;

}

.right{

    position: relative;
    left: 200px;

    float: left;
    margin-left: -200px;


    width: 200px;
    height: 100%;
   
    background-color: blue;
}


双飞翼布局

方法:与圣杯布局类似,不同点是双飞翼布局给左右两栏留空间是这样做的:

在中间div中新增一个div(inside),给inside设置margin偏移.这样就给

左右两栏留下了空间

html结构

<div class="outer">
        <div class="center">
            <div class="inside">

            </div>
        </div>
        <div class="left"></div>  
        
        <div class="right"></div>
    </div>
.outer{
    height: 500px;
    background-color: black;
    

}
.left{

    float: left;
    margin-left: -100%;

    width: 200px;
    height: 100%;
   
    background-color: red;
    

}

.center{

    float: left;
    height: 100%;
    width: 100%;


}
.inside{
    margin-left: 200px;
    margin-right: 200px;
    height: 100%;
    background-color: grey;
}

.right{

   
    float: left;
    margin-left: -200px;


    width: 200px;
    height: 100%;
   
    background-color: blue;
}

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS布局双飞翼布局都是三布局,其中主要内容区域在最中间,左右两侧是侧边。它们的目的是为了解决传统三布局固定中间宽度,侧边无法等高的问题。 CSS布局的实现: HTML结构如下: ``` <div class="container"> <div class="main">主要内容</div> <div class="left">左侧边</div> <div class="right">右侧边</div> </div> ``` CSS样式如下: ``` .container { display: table; width: 100%; table-layout: fixed; border-spacing: 0; } .main { display: table-cell; width: 100%; background-color: #fff; } .left { display: table-cell; width: 200px; background-color: #ccc; position: relative; left: -200px; margin-left: -100%; } .right { display: table-cell; width: 200px; background-color: #ccc; position: relative; right: -200px; margin-right: -100%; } ``` 实现原理:通过将容器设置为table,主要内容区域和侧边都设置为table-cell,实现三等高。通过设置负的margin和left/right来实现侧边的位置偏移。 双飞翼布局的实现: HTML结构如下: ``` <div class="container"> <div class="main">主要内容</div> </div> <div class="left">左侧边</div> <div class="right">右侧边</div> ``` CSS样式如下: ``` .container { padding-left: 200px; padding-right: 200px; } .main { float: left; width: 100%; background-color: #fff; } .left { float: left; width: 200px; margin-left: -100%; background-color: #ccc; } .right { float: left; width: 200px; margin-left: -200px; background-color: #ccc; } ``` 实现原理:通过设置主要内容区域的宽度为100%,再通过padding将左右两侧的宽度撑开。通过设置负的margin和left来实现侧边的位置偏移。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值