css【详解】—— 圣杯布局 vs 双飞翼布局 (含手写清除浮动 clearfix)

两者功能效果相同,实现方式不同

效果预览

  • 两侧宽度固定,中间宽度自适应(三栏布局)
  • 中间部分优先渲染
  • 允许三列中的任意一列成为最高列

圣杯布局

通过左右栏填充容器的左右 padding 实现,更多细节详见注释。

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>css 圣杯布局</title>
    <style>
      body {
        /* 清除浏览器默认样式 */
        margin: 0;
        /* 设置最小宽度 */
        min-width: 550px;
      }

      .header {
        background-color: gray;
        height: 40px;
      }

      .container {
        /* 圣杯布局 -- 通过 padding 实现 */
        padding-left: 200px;
        padding-right: 150px;
      }

      .center {
        /* center宽度自适应 */
        width: 100%;
        float: left;
        background-color: yellow;
        height: 100px;
      }

      .left {
        width: 200px;
        float: left;
        margin-left: -100%;
        position: relative;
        right: 200px;
        background-color: blue;
        height: 100px;
      }

      .right {
        width: 150px;
        float: left;
        margin-right: -150px;
        background-color: red;
        height: 100px;
      }

      .footer {
        /* 清除浮动 */
        clear: both;
        background-color: gray;
        height: 40px;
      }
    </style>
  </head>
  <body>
    <div class="header">header</div>
    <div class="container">
      <!-- center 置于 left 和 right 的上方,用于优先渲染页面主体内容 -->
      <div class="center">center</div>
      <div class="left">left</div>
      <div class="right">right</div>
    </div>
    <div class="footer">footer</div>
  </body>
</html>

双飞翼布局

通过左右栏填充主体内容的左右 margin 实现,更多细节详见注释。

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>css 双飞翼布局</title>
    <style>
      body {
        /* 清除浏览器默认样式 */
        margin: 0;
        /* 设置最小宽度 */
        min-width: 550px;
      }

      .header {
        background-color: gray;
        height: 40px;
      }

      .container {
        /* 自适应宽度 */
        width: 100%;
        /* 左浮动 */
        float: left;
      }

      .center {
        /* 双飞翼布局 -- 通过 margin 留白实现 */
        margin-left: 200px;
        margin-right: 150px;
        background-color: yellow;
        height: 100px;
      }

      .left {
        width: 200px;
        /* 左浮动 */
        float: left;
        /* 自身向左移动父元素(此处为body)宽度的 100% */
        margin-left: -100%;
        background-color: blue;
        height: 100px;
      }

      .right {
        width: 150px;
        /* 左浮动 */
        float: left;
        /* 自身向左移动 150px */
        margin-left: -150px;
        background-color: red;
        height: 100px;
      }

      .footer {
        /* 清除浮动 */
        clear: both;
        background-color: gray;
        height: 40px;
      }
    </style>
  </head>
  <body>
    <div class="header">header</div>
    <div class="container">
      <!-- center 置于 left 和 right 的上方,用于优先渲染页面主体内容 -->
      <div class="center">center</div>
    </div>
    <!-- left 置于 container 外面 -->
    <div class="left">left</div>
    <!-- right 置于 container 外面 -->
    <div class="right">right</div>
    <div class="footer">footer</div>
  </body>
</html>

手写清除浮动 clearfix

css 复制代码
/* 手写 clearfix */
.clearfix:after {
  content: "";
  display: table;
  clear: both;
}
.clearfix {
  *zoom: 1; /* 兼容 IE 低版本 */
}

用在类似圣杯布局的容器上,footer 不再需要 clear: both;

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>css 圣杯布局</title>
    <style>
      body {
        /* 清除浏览器默认样式 */
        margin: 0;
        /* 设置最小宽度 */
        min-width: 550px;
      }

      .header {
        background-color: gray;
        height: 40px;
      }

      .container {
        /* 圣杯布局 -- 通过 padding 实现 */
        padding-left: 200px;
        padding-right: 150px;
      }

      .center {
        /* center宽度自适应 */
        width: 100%;
        float: left;
        background-color: yellow;
        height: 100px;
      }

      .left {
        width: 200px;
        float: left;
        margin-left: -100%;
        position: relative;
        right: 200px;
        background-color: blue;
        height: 100px;
      }

      .right {
        width: 150px;
        float: left;
        margin-right: -150px;
        background-color: red;
        height: 100px;
      }

      .footer {
        /* 清除浮动 */
        /* clear: both; */
        background-color: gray;
        height: 40px;
      }

      /* 手写 clearfix */
      .clearfix:after {
        content: "";
        display: table;
        clear: both;
      }
      .clearfix {
        *zoom: 1; /* 兼容 IE 低版本 */
      }
    </style>
  </head>
  <body>
    <div class="header">header</div>
    <div class="container clearfix">
      <!-- center 置于 left 和 right 的上方,用于优先渲染页面主体内容 -->
      <div class="center">center</div>
      <div class="left">left</div>
      <div class="right">right</div>
    </div>
    <div class="footer">footer</div>
  </body>
</html>
相关推荐
LCG元1 小时前
Vue.js组件开发-如何实现异步组件
前端·javascript·vue.js
Lorcian1 小时前
web前端12--表单和表格
前端·css·笔记·html5·visual studio code
问道飞鱼2 小时前
【前端知识】常用CSS样式举例
前端·css
wl85112 小时前
vue入门到实战 三
前端·javascript·vue.js
ljz20162 小时前
本地搭建deepseek-r1
前端·javascript·vue.js
爱是小小的癌2 小时前
Java-数据结构-优先级队列(堆)
java·前端·数据结构
傻小胖3 小时前
vue3中Teleport的用法以及使用场景
前端·javascript·vue.js
wl85113 小时前
Vue 入门到实战 七
前端·javascript·vue.js
Enti7c4 小时前
用 HTML、CSS 和 JavaScript 实现抽奖转盘效果
前端·css
LCG元4 小时前
Vue.js组件开发-使用Vue3如何实现上传word作为打印模版
前端·vue.js·word