Skip to content

三栏布局 并且优先加载中间部分

知识点

其实考察的就是 HTML 的解析流程

flex 实现

  • flex 的 order 属性可以控制元素的排列顺序,所以实现三栏布局很简单,数值越小元素越靠前,默认为 0
container
left
right
html
<div class="page">
  <div class="container"></div>
  <div class="left"></div>
  <div class="right"></div>
</div>

<style scoped>
  .page {
    display: flex;
  }

  .container {
    flex: 1;
    background-color: blueviolet;
    order: 1;
  }

  .left {
    width: 50px;
    height: 40px;
    background-color: pink;
  }

  .right {
    width: 50px;
    height: 40px;
    background-color: skyblue;
    order: 2;
  }
</style>

圣杯布局

  • 此布局最后还需要给整体的容器设置一个最小宽度,否则宽度过小会出现布局混乱,一般写在 html 或 body 上
  • 最小宽度计算方式一般为 left+right+left,因为在相对定位下,元素先放置在未添加定位时的位置,再在不改变页面布局的前提下调整元素位置(因此会在此元素未添加定位时所在位置留下空白),也就相当于虽然盒子被移走,但此处仍然有个占用宽度为 left 宽度的空白,而我们移动的距离刚好是 left 的宽度,所以此时至少要留下两个 left 的宽度+right 的宽度才能保证盒子的布局不会混乱
header
center
left
right
html
<div class="b-box">
  <div class="b-header">header</div>
  <div class="b-page">
    <div class="b-center b">center</div>
    <div class="b-left b">left</div>
    <div class="b-right b">right</div>
  </div>
  <div class="b-footer">footer</div>
</div>

<style scoped>
  .b-box {
    min-width: 400px;
  }

  .b-header {
    width: 100%;
    height: 20px;
    background-color: #ccc;
  }

  .b {
    float: left;
  }

  .b-page {
    padding-left: 150px;
    padding-right: 100px;
  }

  .b-center {
    width: 100%;
    background-color: pink;
  }

  .b-left {
    position: relative;
    right: 150px;
    width: 150px;
    /*
	 此处 margin-left: -100%;的原因是:
	 margin被设置为百分比时其大小是相对于最近的块级容器的宽度 
	 本例中也就是b-page的宽度,而标准盒模型下,width只包括元素内容,
	 不包含padding与border,所以设置为-100%,其实也就相当于移动b-center的宽度,
	 因为b-center的宽度为100%
	*/
    margin-left: -100%;
    background-color: skyblue;
  }

  .b-right {
    width: 100px;
    background-color: green;
    /* 
    这种写法是因为当他margin-right: -100px;时,在外界看来他没有宽度了,所以就上去了
    */
    margin-right: -100px;
    /* 
    下边这种写法不用在min-width中加上此处相对定位后空白right的宽度
    这是因为相对定位向左移动后margin-left又将元素移动回原地了,又覆盖在了原本right上
    但是对于center来说,此时right和上边一样也是没有宽度了,所以他就自然而然移动上去了
    在视觉上宽度依然存在,此处比较抽象
    */
    /* position: relative;
    left: 100px;
    margin-left: -100px; */
  }

  .b-footer {
    width: 100%;
    height: 20px;
    background-color: #ccc;
    clear: both;
  }
</style>

双飞翼布局

  • 此布局也需要中间留有宽度,否则再宽度过小时,会挤压中间的内容,被右侧覆盖
  • 宽度大小自己决定,合理即可
center
left
html
<div id="box">
  <div id="header">header</div>
  <div id="container" class="column">
    <div id="center">center</div>
  </div>
  <div id="left" class="column">left</div>
  <div id="right" class="column">right</div>
  <div id="footer">footer</div>
</div>

<style scoped>
  #box {
    min-width: 400px;
  }

  #header,
  #footer {
    width: 100%;
    background-color: #ccc;
  }

  .column {
    float: left;
  }

  #container {
    width: 100%;
  }

  #center {
    margin-left: 150px;
    margin-right: 100px;
    background-color: pink;
  }

  #left {
    width: 150px;
    background-color: green;
    /*
	 此处 margin-left: -100%;的原因和上方圣杯中类似,也是参照最近的块级容器的宽度
	*/
    margin-left: -100%;
  }

  #right {
    width: 100px;
    background-color: orange;
    margin-left: -100px;
  }

  #footer {
    clear: both;
  }
</style>

Keep Reading, Keep Writing, Keep Coding