• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

三栏布局左右固定,间自适应的几种方式

武飞扬头像
浅墨、离殇
帮助1

1、flex布局(强烈推荐)

实现方式:左右两栏设置宽度,中间栏设置 flex:1,占满余下部分

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <title>flex布局</title>
  6.  
    <style>
  7.  
    .main{
  8.  
    height: 60px;
  9.  
    display: flex;
  10.  
    }
  11.  
     
  12.  
    .left,
  13.  
    .right{
  14.  
    height: 100%;
  15.  
    width: 200px;
  16.  
    background-color: #ccc;
  17.  
    }
  18.  
     
  19.  
    .content{
  20.  
    flex: 1;
  21.  
    background-color: #eee;
  22.  
    }
  23.  
    </style>
  24.  
    </head>
  25.  
     
  26.  
    <body>
  27.  
    <div class="main">
  28.  
    <div class="left"></div>
  29.  
    <div class="content"></div>
  30.  
    <div class="right"></div>
  31.  
    </div>
  32.  
    </body>
  33.  
     
  34.  
    </html>
学新通

2、grid布局

实现方式:左右两栏设置宽度,中间栏宽度auto

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <title>grid布局</title>
  6.  
    <style>
  7.  
    body {
  8.  
    display: grid;
  9.  
    grid-template-columns: 200px auto 200px;
  10.  
    grid-template-rows: 60px;
  11.  
    }
  12.  
     
  13.  
    .left,
  14.  
    .right {
  15.  
    background-color: #ccc;
  16.  
    }
  17.  
     
  18.  
    .content {
  19.  
    background-color: #eee;
  20.  
    }
  21.  
    </style>
  22.  
    </head>
  23.  
     
  24.  
    <body>
  25.  
    <div class="left"></div>
  26.  
    <div class="content"></div>
  27.  
    <div class="right"></div>
  28.  
    </body>
  29.  
     
  30.  
    </html>
学新通

3、magin负值法

实现方式:左右两栏均左浮动,中间栏外层盒子设置浮动,中间栏设置左右两栏宽度的margin值,左栏设置margin -100%,右栏设置 margin值为负的盒子宽度。

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <title>margin负值</title>
  6.  
     
  7.  
    <style>
  8.  
    .left,
  9.  
    .right {
  10.  
    float: left;
  11.  
    width: 200px;
  12.  
    height: 60px;
  13.  
    background-color: #eee;
  14.  
    }
  15.  
     
  16.  
    .left {
  17.  
    margin-left: -100%;
  18.  
    }
  19.  
     
  20.  
    .right {
  21.  
    margin-left: -200px;
  22.  
    }
  23.  
     
  24.  
    .main {
  25.  
    width: 100%;
  26.  
    float: left;
  27.  
    height: 60px;
  28.  
    }
  29.  
     
  30.  
    .content {
  31.  
    height: 60px;
  32.  
    margin: 0 200px;
  33.  
    background-color: #ccc;
  34.  
    }
  35.  
    </style>
  36.  
    </head>
  37.  
     
  38.  
    <body>
  39.  
    <div class="main">
  40.  
    <div class="content"></div>
  41.  
    </div>
  42.  
    <div class="left"></div>
  43.  
    <div class="right"></div>
  44.  
    </body>
  45.  
     
  46.  
    </html>
学新通

4、自身浮动法

实现方式:左栏左浮动,右栏右浮动,中间栏放最后不浮动,设置左右margin值

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <title>自身浮动法</title>
  6.  
    <style>
  7.  
    .left,
  8.  
    .right {
  9.  
    height: 60px;
  10.  
    width: 200px;
  11.  
    background-color: #eee;
  12.  
    }
  13.  
     
  14.  
    .left {
  15.  
    float: left;
  16.  
    }
  17.  
     
  18.  
    .right {
  19.  
    float: right;
  20.  
    }
  21.  
     
  22.  
    .content{
  23.  
    height: 60px;
  24.  
    background-color: #ccc;
  25.  
    margin: 0 200px;
  26.  
    }
  27.  
    </style>
  28.  
    </head>
  29.  
     
  30.  
    <body>
  31.  
    <div class="left"></div>
  32.  
    <div class="right"></div>
  33.  
    <div class="content"></div>
  34.  
    </body>
  35.  
     
  36.  
    </html>
学新通

5、绝对定位

实现方式:左右两栏绝对定位,分别定位到盒子的两侧,中间栏采用margin值撑开盒子

注意:采用定位时,浏览器默认的padding或margin值会影响布局,需要初始化样式 margin:0;padding:0;

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
     
  4.  
    <head>
  5.  
    <title>绝对定位法</title>
  6.  
    <style>
  7.  
    * {
  8.  
    margin: 0;
  9.  
    padding: 0;
  10.  
    }
  11.  
     
  12.  
    .left,
  13.  
    .right {
  14.  
    position: absolute;
  15.  
    height: 60px;
  16.  
    width: 200px;
  17.  
    background-color: #ccc;
  18.  
    top: 0;
  19.  
    }
  20.  
     
  21.  
    .left {
  22.  
    left: 0;
  23.  
    }
  24.  
     
  25.  
    .right {
  26.  
    right: 0;
  27.  
    }
  28.  
     
  29.  
    .content {
  30.  
    height: 60px;
  31.  
    margin: 0 200px;
  32.  
    background-color: #eee;
  33.  
    }
  34.  
    </style>
  35.  
    </head>
  36.  
     
  37.  
    <body>
  38.  
    <div class="left"></div>
  39.  
    <div class="content"></div>
  40.  
    <div class="right"></div>
  41.  
    </body>
  42.  
     
  43.  
    </html>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgeiehk
系列文章
更多 icon
同类精品
更多 icon
继续加载