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>
相关推荐
萌萌哒草头将军2 小时前
⚡⚡⚡尤雨溪宣布开发 Vite Devtools,这两个很哇塞 🚀 Vite 的插件,你一定要知道!
前端·vue.js·vite
小彭努力中3 小时前
7.Three.js 中 CubeCamera详解与实战示例
开发语言·前端·javascript·vue.js·ecmascript
浪裡遊4 小时前
跨域问题(Cross-Origin Problem)
linux·前端·vue.js·后端·https·sprint
LinDaiuuj4 小时前
判断符号??,?. ,! ,!! ,|| ,&&,?: 意思以及举例
开发语言·前端·javascript
敲厉害的燕宝4 小时前
Pinia——Vue的Store状态管理库
前端·javascript·vue.js
Aphasia3114 小时前
react必备JavaScript知识点(二)——类
前端·javascript
玖玖passion4 小时前
数组转树:数据结构中的经典问题
前端
呼Lu噜4 小时前
WPF-遵循MVVM框架创建图表的显示【保姆级】
前端·后端·wpf
珠峰下的沙砾4 小时前
Vue3 里 CSS 深度作用选择器 :global
前端·javascript·css
航Hang*4 小时前
WEBSTORM前端 —— 第2章:CSS —— 第3节:背景属性与显示模式
前端·css·css3·html5·webstorm