css三栏布局

中间自适应,两边固定

[图片上传失败...(image-ab3235-1517459642371)]

1.用margin和float实现

css:

<style>
    * {
        margin: 0;
        padding: 0;
        text-align: center;
    }
    .left {
        float: left;
        width: 100px;
        background: yellow;
    }
    .right {
        float: right;
        width: 100px;
        background: green;
    }
    .center {
        margin: 0 100px;
        background: #ccc;
    }
</style>

html

<div class="container">
    <div class="left">left</div>
    <div class="right">right</div>
    <div class="center">center</div>
</div>
2.用float,margin负值实现

css

<style>
    * {
        margin: 0;
        padding: 0;
        text-align: center;
    }
    .left, .right, .center {
        float: left;
    }
    .left {
        width: 100px;
        margin-left: -100%;
        background: yellow;
    }
    .right {
        width: 100px;
        margin-left: -100px;
        background: green;
    }
    .center {
        width: 100%;
        background: #ccc;
    }
</style>

html

<div class="container">
    <div class="center">center</div>
    <div class="left">left</div>
    <div class="right">right</div>
</div>
3.用flex实现

css

<style>
    * {
        margin: 0;
        padding: 0;
        text-align: center;
    }
    .container {
        display: flex;
    }
    .left {
        width: 100px;
        background: yellow;
    }
    .right {
        width: 100px;
        background: green;
    }
    .center {
        flex: 1;
        background: #ccc;
    }
</style>

html

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

中间固定,两边自适应

[图片上传失败...(image-ebd879-1517459642371)]

1. 用margin和float实现

css

<style>
    * {
        margin: 0;
        padding: 0;
        text-align: center;
    }
    .container {
        padding: 0 100px;
    }
    .left {
        float: left;
        width: 50%;
        margin-left: -100px;
        background: yellow;
    }
    .right {
        float: right;
        width: 50%;
        margin-right: -100px;
        background: green;
    }
    .center {
        float: left;
        width: 200px;
        background: #ccc;
    }
</style>

html

<div class="container">
    <div class="left">left</div>
    <div class="center">center</div>
    <div class="right">right</div>
</div>
2. 用flex实现

css

<style>
    * {
        margin: 0;
        padding: 0;
        text-align: center;
    }
    .container {
        display: flex;
    }
    .left {
        flex: 1;
        background: yellow;
    }
    .right {
        flex: 1;
        background: green;
    }
    .center {
        width: 200px;
        background: #ccc;
    }
</style>

html

<div class="container">
    <div class="left">left</div>
    <div class="center">center</div>
    <div class="right">right</div>
</div>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,696评论 1 92
  • 三栏布局一般指左右两栏固定宽度,中间一栏自适应的布局方法,也是许多网站常用的布局方式。如下图: 该示例在一个htm...
    zkhChris阅读 4,874评论 0 8
  • 所谓三栏布局指的是:两边固定,中间内容自适应。三栏布局在开发中经常见到,效果如图所示: 红色和蓝色部分宽度固定,中...
    betterwlf阅读 3,848评论 0 1
  • 前言 总括: 不管是三栏布局还是两栏布局都是我们在平时项目里经常使用的,也许你不知道什么事三栏布局什么是两栏布局但...
    秦至阅读 4,059评论 2 8
  • 夜晚厌倦了藏匿 向白昼学习曝光 有副躯体 已经在月的注视下自惭形秽 向内敲碎自己吧 月光就是一把皎洁的锤子 那些骄...
    归则墨阅读 1,733评论 0 0

友情链接更多精彩内容