css经典布局——圣杯布局

圣杯布局和双飞翼布局一直是前端面试的高频考点,圣杯布局的出现是来自由MatthewLevine在2006年写的一篇文章《InSearchoftheHolyGrail》。比起双飞翼布局,它的起源不是源于对页面的形象表达。在西方,圣杯是表达“渴求之物”的意思。而双飞翼布局则是源于淘宝的UED,可以说是灵感来自于页面渲染。效果图原本录制了…

大家好,又见面了,我是你们的朋友全栈君。

         圣杯布局双飞翼布局一直是前端面试的高频考点,圣杯布局的出现是来自由 Matthew Levine 在 2006 年写的一篇文章 《In Search of the Holy Grail》。 比起双飞翼布局,它的起源不是源于对页面的形象表达。在西方,圣杯是表达“渴求之物”的意思。而双飞翼布局则是源于淘宝的UED,可以说是灵感来自于页面渲染。

效果图

        原本录制了一个小视频,奈何不能上传到博客中,视频中通过缩放页面可以发现随着页面的宽度的变化,这三栏布局是中间盒子优先渲染,两边的盒子框子宽度固定不变,即使页面宽度变小,也不影响我们的浏览。注意:为了安全起见,最好还是给body加一个最小宽度!

css经典布局——圣杯布局

 圣杯布局要求

  • header和footer各自占领屏幕所有宽度,高度固定。
  • 中间的container是一个三栏布局。
  • 三栏布局两侧宽度固定不变,中间部分自动填充整个区域。
  • 中间部分的高度是三栏中最高的区域的高度。

圣杯布局的三种实现

【1】浮动

  • 先定义好header和footer的样式,使之横向撑满。
  • 在container中的三列设为浮动和相对定位(后面会用到),center要放在最前面,footer清除浮动。
  • 三列的左右两列分别定宽200px和150px,中间部分center设置100%撑满
  • 这样因为浮动的关系,center会占据整个container,左右两块区域被挤下去了
  • 接下来设置left的 margin-left: -100%;,让left回到上一行最左侧
  • 但这会把center给遮住了,所以这时给外层的container设置 padding-left: 200px;padding-right: 150px;,给left和right空出位置
  • 这时left并没有在最左侧,因为之前已经设置过相对定位,所以通过 left: -200px; 把left拉回最左侧
  • 同样的,对于right区域,设置 margin-left: -150px; 把right拉回第一行
  • 这时右侧空出了150px的空间,所以最后设置 right: -150px;把right区域拉到最右侧就行了。
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
</head>
<style>
  body {
    min-width: 550px;  /* 2x leftContent width + rightContent width */
    font-weight: bold;
    font-size: 20px;
  }

  #header, #footer {
    background: rgba(29, 27, 27, 0.726);
    text-align: center;
    height: 60px;
    line-height: 60px;
  }
  #footer {
    clear: both;
  }

  #container {
    padding-left: 200px;   /* leftContent width */
    padding-right: 150px;  /* rightContent width */
    overflow: hidden;
  }

  #container .column {
    position: relative;
    float: left;
    text-align: center;
    height: 300px;
    line-height: 300px;
  }

  #center {
    width: 100%;
    background: rgb(206, 201, 201);
  }

  #left {
    width: 200px;           /* leftContent width */
    right: 200px;           /* leftContent width */
    margin-left: -100%;
    background: rgba(95, 179, 235, 0.972);
  }

  #right {
    width: 150px;           /* rightContent width */
    margin-left: -150px;   /* rightContent width */
    right: -150px;
    background: rgb(231, 105, 2);
  }

</style>

<body>
  <div id="header">#header</div>
  <div id="container">
    <div id="center" class="column">#center</div>
    <div id="left" class="column">#left</div>
    <div id="right" class="column">#right</div>
  </div>
  <div id="footer">#footer</div>


</body>

</html>

【2】flex弹性盒子

  • header和footer设置样式,横向撑满。
  • container中的left、center、right依次排布即可
  • 给container设置弹性布局 display: flex;
  • left和right区域定宽,center设置 flex: 1; 即可
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
</head>
<style>
  body {
    min-width: 550px;  
    font-weight: bold;
    font-size: 20px;
  }
  #header, #footer {
    background: rgba(29, 27, 27, 0.726);
    text-align: center;
    height: 60px;
    line-height: 60px;
  }
  #container {
   display: flex;
  }
  #container .column {
    text-align: center;
    height: 300px;
    line-height: 300px;
  }
  #center {
    flex: 1;
    background: rgb(206, 201, 201);
  }
  #left {
    width: 200px;        
    background: rgba(95, 179, 235, 0.972);
  }
  #right {
    width: 150px;           
    background: rgb(231, 105, 2);
  }
</style>

<body>
  <div id="header">#header</div>
  <div id="container">
    <div id="left" class="column">#left</div>
    <div id="center" class="column">#center</div>
    <div id="right" class="column">#right</div>
  </div>
  <div id="footer">#footer</div>
</body>

</html>

【3】grid布局

css经典布局——圣杯布局

如上图所示,我们把body划分成三行四列的网格,其中有5条列网格线

  • 给body元素添加display: grid;属性变成一个grid(网格)
  • 给header元素设置grid-row: 1; 和 grid-column: 1/5; 意思是占据第一行网格的从第一条列网格线开始到第五条列网格线结束
  • 给footer元素设置grid-row: 1; 和 grid-column: 1/5; 意思是占据第三行网格的从第一条列网格线开始到第五条列网格线结束
  • 给left元素设置grid-row: 2; 和 grid-column: 1/2; 意思是占据第二行网格的从第一条列网格线开始到第二条列网格线结束
  • 给center元素设置grid-row: 2; 和 grid-column: 2/4; 意思是占据第二行网格的从第二条列网格线开始到第四条列网格线结束
  • 给right元素设置grid-row: 2; 和 grid-column: 4/5; 意思是占据第二行网格的从第四条列网格线开始到第五条列网格线结束
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
</head>
<style>
  body {
    min-width: 550px;
    font-weight: bold;
    font-size: 20px;
    display: grid;
  }
  #header,
  #footer {
    background: rgba(29, 27, 27, 0.726);
    text-align: center;
    height: 60px;
    line-height: 60px;
  }
  #header {
    grid-row: 1;
    grid-column: 1/5;
  }
  #footer {
    grid-row: 3;
    grid-column: 1/5;
  }
  .column {
    text-align: center;
    height: 300px;
    line-height: 300px;
  }
  #left {
    grid-row: 2;
    grid-column: 1/2;
    background: rgba(95, 179, 235, 0.972);
  }
  #center {
    grid-row: 2;
    grid-column: 2/4;
    background: rgb(206, 201, 201);
  }
  #right {
    grid-row: 2;
    grid-column: 4/5;
    background: rgb(231, 105, 2);
  }
</style>

<body>
  <div id="header">#header</div>
  <div id="left" class="column">#left</div>
  <div id="center" class="column">#center</div>
  <div id="right" class="column">#right</div>
  <div id="footer">#footer</div>
</body>

</html>

 

文章每周持续更新,可以微信搜索「 前端大集锦 」第一时间阅读,回复【视频】【书籍】领取200G视频资料和30本PDF书籍资料

css经典布局——圣杯布局

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/124882.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • php 第三方登录总结OAuth协议

    php 第三方登录总结OAuth协议

    2021年10月25日
    57
  • 将一个接口响应时间从2s优化到 200ms以内的一个案例

    将一个接口响应时间从2s优化到 200ms以内的一个案例一 背景在开发联调阶段发现一个接口的响应时间特别长 经常超时 囧 本文讲讲是如何定位到性能瓶颈以及修改的思路 将该接口从 2s 左右优化到 200ms 以内 二 步骤 2 1 定位定位性能瓶颈有两个思路 一个是通过工具去监控 一个是通过经验去猜想 2 1 1 工具监控就工具而言 推荐使用 arthas 用到的是 trace 命令具体安装步骤很简单 大家自行研究 我的使用步骤是

    2025年7月7日
    1
  • 纸上得来终觉浅,绝知此事要躬行

    纸上得来终觉浅,绝知此事要躬行

    2021年10月2日
    47
  • element-ui中表格获取当前行的索引index[通俗易懂]

    element-ui中表格获取当前行的索引index[通俗易懂]前言弄文件上传时,需要对上传列表的文件进行一定的操作,例如暂停/取消等等,因为我是使用element-ui中表格展示上传文件列表的,这时的操作却需要使用到当前行的索引下,如何获取索引就是我接下来要做的工作了:获取当前行的索引index使用scope.$index,scope.row即可实现获取索引<el-table-columnlabel=”排序”min-width=”100″><templateslot-scope=”scope”>{{sco

    2022年9月3日
    2
  • C#酒店管理系统_酒店管理系统免费

    C#酒店管理系统_酒店管理系统免费1.酒店管理系统概要c#实现的酒店管理系统,里面包含了数据库文件,简易酒店管理系统源码,采用WinFrom程序设计开发的酒店管理系统;应用到标准的三层技术,多个视图工具控件;功能介绍用户可根据自己的需求入住登记不同类型的房间,同时登记个人基本信息,管理员可通过对不同类型房间的管理及房间信息管理设置不同的类型房间进行增删改查,并对入住客户的信息及点退房信息查询,并改变房间的入住与退房或空房间的状态信息2.数据库设计由于数据库较多,所以暂时不放出来,下面我们看运行截图3.运行截图

    2022年9月24日
    0
  • qmap使用

    qmap使用#include<QCoreApplication>#include<QMap>//#include<QVector>#include<QDebug>typedefQMap<QString,int>CMyQMap;intmain(intargc,char*argv[]){QCoreApplica…

    2022年5月7日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号