📒
notebook
  • Notebook
  • DevOps
    • Git
      • 常见 Git 操作
      • 使用 SSH 连接 Git 远程仓库
      • 使用 GPG Keys 签名 Git 提交
      • Git on macOS
    • Docker
      • macOS 安装配置 Docker
      • CentOS7 安装配置 Docker CE
  • Linux
    • CentOS
      • CentOS7 安装配置 vsftpd
      • CentOS7 设置虚拟内存
      • CentOS7 使用 acme.sh 自动申请免费 SSL 证书
      • CentOS7 修改 SSH 端口号
      • CentOS7 主机初始设置
      • 阿里云 CentOS 主机常见设置
      • CentOS7 安装配置 SS
    • nginx
      • nginx 配置 301 永久重定向
      • nginx 使用 SSL证书配置 HTTPS
      • CentOS7 安装配置 nginx
      • nginx 配置 gzip 压缩
      • nginx 代理静态网页
  • Server
    • Ghost
      • macOS 安装配置 Ghost
      • CentOS7 安装配置 Ghost
    • npm & Yarn
      • Yarn 常用命令
      • CentOS7 安装卸载升级 Yarn
      • npm & Yarn 常见错误处理
      • macOS 安装卸载升级 Yarn
    • Node.js
      • Awesome Node.js
      • CentOS7 安装卸载升级 Node.js
      • macOS 安装卸载升级 Node.js
  • Web
    • Ionic
      • 创建 Ionic & Angular 项目
      • 使用 Ionic & Cordova 构建 Android 应用
      • macOS 搭建 Ionic & Cordova 开发环境
    • CSS
      • CSS 排版技巧
      • Awesome CSS
      • CSS 三栏自适应布局
    • Angular
      • Awesome Angular
      • 创建 Angular 项目
    • HTML
      • HTML head 常用标签
      • HTML 常用 DTD 声明
      • 常用网页语义结构
    • Web 技术标准
    • JavaScript
      • Awesome JavaScript
      • JavaScript 的 eval() 函数详解
  • Mobile
    • H5
      • iOS Safari Web App 配置
  • Development Environment
    • Development Utilities
      • Awesome Windows
      • macOS 安装配置 Homebrew
      • Awesome macOS
      • macOS 安装配置 iTerm2
    • FEED
      • 常用 Gulp 插件
  • Network
    • 常见公共 DNS
  • Technology Stacks for Web Front-End Development
Powered by GitBook
On this page
  • 布局方案对比
  • 布局方案明细
  • 圣杯布局
  • 双飞翼布局
  • 混合浮动布局
  • 弹性布局
  • 参考文献

Was this helpful?

  1. Web
  2. CSS

CSS 三栏自适应布局

布局方案对比

布局名称

兼容性

清除浮动

渲染顺序

圣杯布局

IE6

需要

中间栏最先

双飞翼布局

IE6

需要

中间栏最先

混合浮动布局

IE6

需要

中间栏最后

弹性布局

IE11

不需要

可自定义顺序

Tips: 以上布局方案均为解决 “中间栏宽度自适应,两边栏定宽,三栏均可撑起父容器高度” 的布局问题,可推广至两栏自适应布局。

布局方案明细

圣杯布局

HTML:

<header class="header">Header</header>
<main class="clearfix main">
  <article class="middle">Middle</article>
  <aside class="left">Left</aside>
  <aside class="right">Right</aside>
</main>
<footer class="footer">Footer</footer>

CSS:

body {
  margin: 0;
}
.clearfix::after {
  content: "";
  display: block;
  visibility: hidden;
  height: 0;
  line-height: 0;
  clear: both;
}
.header,
.main,
.footer {
  background-color: grey;
}
.left {
  min-height: 100px;
  background-color: yellow;
  opacity: .6;
}
.middle {
  min-height: 100px;
  background-color: green;
  opacity: .6;
}
.right {
  min-height: 100px;
  background-color: blue;
  opacity: .6;
}

/**
 * Layout
 */
.main {
  padding-right: 150px; /* 等于右栏宽度 */
  padding-left: 200px; /* 等于左栏宽度 */
}
.middle {
  float: left;
  width: 100%;
}
.left {
  float: left;
  position: relative;
  left: -200px; /* 等于自身宽度 */
  margin-left: -100%;
  width: 200px;
}
.right {
  float: left;
  margin-right: -150px; /* 等于自身宽度 */
  width: 150px;
}

调整三栏间距:

/* 三栏间距 20px */
.main {
  padding-right: 170px; /* 等于右栏宽度加间距 */
  padding-left: 220px; /* 等于左栏宽度加间距 */
}
.left {
  left: -220px; /* 等于自身宽度加间距 */
}
.right {
  position: relative;
  right: -20px; /* 间距 */
}

Tips: 此布局中间栏宽度不得小于左栏宽度。

双飞翼布局

HTML:

<header class="header">Header</header>
<main class="clearfix main">
  <article class="middle-wrapper">
    <div class="middle">Middle</div>
  </article>
  <aside class="left">Left</aside>
  <aside class="right">Right</aside>
</main>
<footer class="footer">Footer</footer>

CSS:

body {
  margin: 0;
}
.clearfix::after {
  content: "";
  display: block;
  visibility: hidden;
  height: 0;
  line-height: 0;
  clear: both;
}
.header,
.main,
.footer {
  background-color: grey;
}
.left {
  min-height: 100px;
  background-color: yellow;
  opacity: .6;
}
.middle {
  min-height: 100px;
  background-color: green;
  opacity: .6;
}
.right {
  min-height: 100px;
  background-color: blue;
  opacity: .6;
}

/**
 * Layout
 */
.middle-wrapper {
  float: left;
  width: 100%;
}
.middle-wrapper > .middle {
  margin-right: 150px; /* 等于右栏宽度 */
  margin-left: 200px; /* 等于左栏宽度 */
}
.left {
  float: left;
  margin-left: -100%;
  width: 200px;
}
.right {
  float: left;
  margin-left: -150px; /* 等于自身宽度 */
  width: 150px;
}

调整三栏间距:

/* 三栏间距 20px */
.middle-wrapper > .middle {
  margin: 0 170px 0 220px;
}

混合浮动布局

HTML:

<header class="header">Header</header>
<main class="clearfix main">
  <aside class="left">Left</aside>
  <aside class="right">Right</aside>
  <article class="middle">Middle</article>
</main>
<footer class="footer">Footer</footer>

CSS:

body {
  margin: 0;
}
.clearfix::after {
  content: "";
  display: block;
  visibility: hidden;
  height: 0;
  line-height: 0;
  clear: both;
}
.header,
.main,
.footer {
  background-color: grey;
}
.left {
  min-height: 100px;
  background-color: yellow;
  opacity: .6;
}
.middle {
  min-height: 100px;
  background-color: green;
  opacity: .6;
}
.right {
  min-height: 100px;
  background-color: blue;
  opacity: .6;
}

/**
 * Layout
 */
.left {
  float: left;
  width: 200px;
}
.right {
  float: right;
  width: 150px;
}
.middle {
  margin-right: 150px; /* 等于右栏宽度 */
  margin-left: 200px; /* 等于左栏宽度 */
}

调整三栏间距:

/* 三栏间距 20px */
.middle {
  margin-right: 170px; /* 等于右栏宽度加间距 */
  margin-left: 220px; /* 等于左栏宽度加间距 */
}

弹性布局

HTML:

<header class="header">Header</header>
<main class="main">
  <article class="middle">Middle</article>
  <aside class="left">Left</aside>
  <aside class="right">Right</aside>
</main>
<footer class="footer">Footer</footer>

CSS:

body {
  margin: 0;
}
.header,
.main,
.footer {
  background-color: grey;
}
.left {
  min-height: 100px;
  background-color: yellow;
  opacity: .6;
}
.middle {
  min-height: 100px;
  background-color: green;
  opacity: .6;
}
.right {
  min-height: 100px;
  background-color: blue;
  opacity: .6;
}

/**
 * Layout
 */
.main {
  display: flex;
}
.middle {
  order: 2;
  flex: 1;
}
.left {
  order: 1;
  width: 200px;
}
.right {
  order: 3;
  width: 150px;
}

调整三栏间距:

/* 三栏间距 20px */
.middle {
  margin-right: 20px;
  margin-left: 20px;
}

参考文献

PreviousAwesome CSSNextAngular

Last updated 4 years ago

Was this helpful?

CodePen:

CodePen:

CodePen:

CodePen:

CSS 圣杯布局
CSS 双飞翼布局
CSS 混合浮动布局
CSS 弹性布局
In Search of the Holy Grail
使用 CSS 弹性盒子