两栏布局和三栏布局

两栏布局和三栏布局

两栏布局

两栏布局:一侧的宽度固定,另一侧自适应;

这是HTML的基础结构,后续的代码都以此为基础;

html 复制代码
<div class="container">
  <div class="left">left</div>
  <div class="right">right</div>
</div>
  1. 利用浮动 将左侧的盒子向左浮动,右侧的盒子宽度设置100%或者auto,让其撑起整个父元素;
css 复制代码
.left {
    float: left;
    width: 200px;
    height: 200px;
    background-color: red;
  }
  .right {
    width: auto;
    height: 200px;
    background-color: green;
  }
  1. 利用浮动 左侧元素设置固定大小,并左浮动,右侧元素设置overflow: hidden; 这样右边就触发了BFCBFC 的区域不会与浮动元素发生重叠,所以两侧就不会发生重叠。
    想了解BFC请点击这里
css 复制代码
.left {
    float: left;
    width: 200px;
    height: 200px;
    background-color: red;
  }
  .right {
    height: 200px;
    background-color: green;
    overflow: hidden;
  }
  1. flex布局 父元素设置flex布局,将右边的元素设置为flex:1,或者给父元素添加justify-content: space-between;,右侧元素宽度设置100%
css 复制代码
.container {
    display: flex;
    /* justify-content: space-between; */
  }

  .left {
    width: 200px;
    height: 200px;
    background-color: red;
  }

  .right {
    flex: 1; /*如果那两行注释取消,需要将该行注释,并且加上width:100%;*/
    /* width: 100%; */
    height: 200px;
    background-color: green;

  }
  1. 利用定位方法 将父级元素设置为相对定位。固定的一侧元素设置为absolute定位,另一侧宽度100%,但是要给右侧盒子添加margin-left;
css 复制代码
.container {
    position: relative;
  }
  .left {
    position: absolute;
    width: 200px;
    height: 200px;
    background-color: red;
  }
  .right {
    width: 100%;
    height: 200px;
    background-color: green;
    margin-left: 200px;
  }

三栏布局

三栏布局就是圣杯布局/双飞翼布局;左右两侧的宽度是固定的,中间部分是自适应的;

这是HTML基础结构,后续代码也使用该结构;

html 复制代码
<div class="container">
  <div class="left">left</div>
  <div class="center">center</div>
  <div class="right">right</div>
</div>
  1. 利用浮动 左边设置向左浮动,右边设置向右浮动,中间一栏设置左右两个方向的margin值,注意这种方式,中间一栏div必须放到最后;
css 复制代码
.left {
    float: left;
    width: 100px;
    height: 100px;
    background: green;
  }
  .right {
    float: right;
    width: 100px;
    height: 100px;
    background: yellow;
  }
  .center {
    background-color: red;
    margin-left: 100px;
    margin-right: 100px;
    height: 100px;
  }
  1. 利用定位 左右两栏设置绝对定位,中间设置对应方向大小的margin的值
css 复制代码
.container {
    position: relative;
  }
  .left {
    position: absolute;
    width: 100px;
    height: 100px;
    background: green;
  }
  .right {
    position: absolute;
    width: 100px;
    height: 100px;
    right: 0;
    top: 0;
    background: yellow;
  }
  .center {
    background-color: red;
    margin-left: 100px;
    margin-right: 100px;
    height: 100px;
  }
  1. flex布局,左右两栏设置固定大小,给父元素添加display:flex,给中间盒子添加flex:1
css 复制代码
.container {
    display: flex;
  }
  .left {
    width: 100px;
    height: 100px;
    background: green;
  }
  .right {
    width: 100px;
    height: 100px;
    background: yellow;
  }
  .center {
    flex: 1;
    background-color: red;
    height: 100px;
  }

圣杯布局

这是HTML基础布局;

html 复制代码
<div class="header">header</div>
  <div class="outer">
    <div class="center">center</div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
  <div class="footer">footer</div>

圣杯布局思路:

  • center盒子写在最前面,保证center盒子最先渲染;
  • outer盒子指定padding-leftpadding-right值,留出leftright的位置;
  • 三个盒子都设置float:left,这时leftright就会被挤到下一行;
  • left设置margin-left:-100%;相对定位+left:-left的宽度;right设置margin-left=-right的宽度;相对定位+right:-right的宽度, 即可将两个盒子归位。
css 复制代码
 * {
    padding: 0;
    margin: 0;
    text-align: center;
  }
  html {
    height: 100%;
  }
  body {
    display: flex;
    flex-direction: column;
    height: 100%;
    ;
  }
  .header {
    width: 100%;
    height: 50px;
    background-color: dimgray;
  }
  .footer {
    width: 100%;
    height: 50px;
    background-color: dimgray;
  }
  .outer {
    flex: 1;
    padding-left: 100px;
    padding-right: 200px;
  }
  .center {
    float: left;
    background-color: darkslateblue;
    height: 100%;
    width: 100%;
  }
  .left {
    float: left;
    width: 100px;
    margin-left: -100%;
    background-color: burlywood;
    height: 100%;
    position: relative;
    left: -100px;
  }
  .right {
    float: left;
    width: 200px;
    margin-left: -200px;
    height: 100%;
    position: relative;
    right: -200px;
    background-color: cyan;
  }

双飞翼布局

这是HTML结构

html 复制代码
<div class="header">header</div>
  <div class="outer">
    <div class="center">
      <div class="content">content</div>
    </div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
  <div class="footer">footer</div>

双飞翼布局思路:

  • 给三个盒子都设置为左浮动;
  • center的宽度设为100%
  • left设置margin-left:-100%right设置margin-left=-right的宽度即可将两个盒子归位,但是将center的两端挡住了;
  • center盒子中再写一个content盒子,设置margin-leftmargin-right为两侧的宽度,content盒子作为内容。
css 复制代码
* {
    padding: 0;
    margin: 0;
  }
  html {
    width: 100%;
    height: 100%;
    text-align: center;
  }
  body {
    display: flex;
    width: 100%;
    height: 100%;
    flex-direction: column;
  }
  .header {
    background-color: grey;
    height: 50px;
  }
  .footer {
    background-color: grey;
    height: 50px;
  }
  .outer {
    flex: 1;
  }
  .center {
    float: left;
    width: 100%;
    background-color: darkslateblue;
    height: 100%;
  }
  .left {
    float: left;
    margin-left: -100%;
    width: 100px;
    background-color: burlywood;
    height: 100%;
  }
  .right {
    float: left;
    width: 200px;
    background-color: cyan;
    margin-left: -200px;
    height: 100%;
  }
  .content {
    margin-left: 100px;
    margin-right: 200px;
    height: 100%;
  }

圣杯布局和双飞翼布局的区别

圣杯布局是利用padding将中间部分留出,再利用定位、margin的方式将左右盒子归位,因此不需要外层div;

双飞翼布局是先设置中间盒子的宽度为100%,再用margin移动左右盒子覆盖了中间盒子的两侧,再将中间加入一个盒子,留出两侧的margin值,达到三栏布局的效果。

两种布局都需要把center盒子写在left和right前面,为了最先渲染。

相关推荐
尾善爱看海1 小时前
Vue 面试收官篇:SSR、性能优化落地、30 道高频面试题精讲(附标准答案)
前端·javascript·vue.js·面试·vue
驳是1 小时前
一个组件,提升 react-router 开发的幸福感
前端·preact
GreenTea2 小时前
OpenAI Agents API 上手实测:一次调用把整个 agent loop 甩给 OpenAI
前端·后端·算法
星云API技术支持2 小时前
企业微信二次开发:群权限设置、成员管理与群资料维护的接口组合实践
java·前端·企业微信
京东云开发者2 小时前
81.8 秒的视频,我们改了 15 个版本:一次纯 Codex 驱动的 AI-native 视频实践
前端·aigc
Csvn3 小时前
TypeScript 大型项目架构:从单体 tsconfig 到分层可扩展的工程
前端
计算机魔术师4 小时前
Dario Amodei 发文呼吁为前沿 AI 降速并提出三点计划
前端
计算机魔术师4 小时前
OpenAI的AI代理偷偷给RubyGems下毒,我们却毫无察觉
前端
甲维斯5 小时前
0代码,0建模,3句话开发一个3D游戏!
前端·游戏·游戏开发
IT_陈寒5 小时前
Vue的响应式更新把我坑惨了,原来问题出在这
前端·人工智能·后端