文章标题

两栏布局

负margin:

效果:

输入图片说明

html

<body>
<div class="left">
    <div>this is left sidebar content</div>
</div>
<div class="right">
    <div>this is right sidebar content</div>
</div>
</body>

css


<style type="text/css">
    .left{
        width:200px;
        float:left;
        background-color:pink;
    }
    .right{
        width:100%;
        float:left;
        margin-left:-200px;
    }
    .right div{
        margin-left:200px;
        background-color:blue;
    }
</style>

此方法也是负margin的一种应用,原理不再赘述,详细参见:[负margin对浮动元素的影响

绝对定位

效果:

输入图片说明

html

<body>
<div class="left">
    <div>this is left sidebar content</div>
</div>
<div class="right">
    <div>this is right sidebar content</div>
</div>
</body>

css

<style type="text/css">
    body{
        margin:0;
    }
    .left{
        width:200px;
        background-color:pink;
        position:absolute;left:0;top:0;
    }
    .right{
        width:100%;
        background-color:blue;
        margin-left:200px;
    }
</style>

浮动定位

效果:

输入图片说明

html


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

css

<style type="text/css">
    body{
        margin:0;
    }
    .left{
        width:200px;
        background-color:blue;
        float:left;
    }
    .right{
        width:100%;
        margin-left:200px;
        background-color:pink;
    }
</style>
  • 注意:绝对定位和浮动定位原理相同,区别在于:浮动定位可以清除浮动,使其占据位置,而绝对定位依旧是脱离文档流的。

flexbox

效果:

输入图片说明

html

<body>
<div class="left">左边栏</div>
<div class="right">右边栏</div>
</body>

css


<style type="text/css">
    body{
        display:flex;
        align-items:stretch;
    }
    .left{
        background-color:pink;
        flex:0 0 200px;
    }
    .right{
        background-color:blue;
        flex:1 1 auto;
    }
</style>

Flex是flex-grow,flex-shrink,flex-basis的缩写

  1. 假设以上三个属性同样取默认值,则flex的默认值为0 1 auto

  2. flex:none,则计算值为0 0 auto

  3. flex:auto,计算值为1 1 auto

  4. flex取值为一个非负数字,则flex-grow,flex-shrink取1,flex-basis取0%

  5. flex取值为一个长度或百分比,则视为flex-basis值,flex-grow取1,flex-shrink取1

  6. flex取值为两个非负数字,则分别视为flex-grow和flex-shrink的值,flex-basis取0%

  7. flex取值为一个非负数字和一个长度或百分比,则分别视为flex-grow和flex-basis的值,flex-shrink取1

三栏布局

负margin

效果:

输入图片说明

html

<body>
<div class="center">
    <div class="center_main">center</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</body>

css

<style type="text/css">
    body{
        margin:0;
    }
    .center{
        width:100%;
        float:left;
    }
    .center_main{
        background-color:pink;
        margin:0 220px;
    }
    .left{
        width:200px;
        background-color:blue;
        float:left;
        margin-left:-100%;
    }
    .right{
        width:200px;
        background-color:blue;
        float:left;
        margin-left:-200px;
    }
</style>
  • 注意:center的元素必须在html中写在第一个
  • 基本原理:center,left,right均设置浮动,center_main设置margin-right,margin-left,为left和right留出距离,left,right设置margin-left,值分别为100%,-(right元素的宽度),设置margin-left的原因参见:[负margin对浮动元素的影响]

绝对定位

效果:

输入图片说明

html

<body>
<div class="center">
    <div class="center_main">center</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</body>

css

<style type="text/css">
    body{
        margin:0;
    }
    .center{
        width:100%;
    }
    .center_main{
        margin:0 220px;
        background-color:pink;
    }
    .left{
        width:200px;
        background-color:blue;
        position:absolute;left:0;top:0;
    }
    .right{
        width:200px;
        background-color:blue;
        position:absolute;right:0;top:0;
    }
</style>

浮动定位

效果:

输入图片说明

html

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

css

<style type="text/css">
    html,body{
        margin:0;
        padding:0;
    }
    .left,.right{
        width:200px;
        background-color:blue;
    }
    .left{
        float:left;
    }
    .right{
        float:right;
    }
    .center{
        background-color:pink;
        margin:0 220px;
    }
</style>

flexbox

效果:

输入图片说明

html

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

css

<style type="text/css">
    body{
        display:flex;
        align-items:stretch;
    }
    .left,.right{
        background-color:blue;
        flex:0 0 100px;
    }
    .center{
        background-color:pink;
        flex:1 1 auto;
    }
</style>
  • flex:0 0 100px;是指设置left,right元素宽度为100px,不随着父元素的宽改变

  • flex:1 1 auto;是指center元素可放大,也可缩小,其尺寸可随着父元素的变化而变化,满足自适应要求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值