前端面试 CSS— CSS 如何实现“圣杯”布局?

Posted aiguangyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前端面试 CSS— CSS 如何实现“圣杯”布局?相关的知识,希望对你有一定的参考价值。

 “圣杯” 布局是一种典型的网页布局结构,如下所示:

实现方式有多种,下面一一给出代码实现。

1. Flex布局来实现

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .header,
    .footer {
      height: 40px;
      width: 100%;
      background: red;
    }

    .container {
      display: flex;
    }

    .middle {
      flex: 1;
      background: yellow;
    }

    .left {
      width: 200px;
      background: pink;
    }

    .right {
      background: aqua;
      width: 200px;
    }
  </style>
</head>

<body>
  <div class="header">这里是头部</div>
  <div class="container">
    <div class="left">左边</div>
    <div class="middle">中间部分</div>
    <div class="right">右边</div>
  </div>
  <div class="footer">这里是底部</div>
</body>

</html>

2. Float布局来实现(全部float:left)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .header,
    .footer {
      height: 40px;
      width: 100%;
      background: red;
    }

    .footer {
      clear: both;
    }

    .container {
      padding-left: 200px;
      padding-right: 200px;
    }

    .container div {
      position: relative;
      float: left;
    }

    .left {
      width: 200px;
      background: pink;
      margin-left: -100%;
      left: -200px;
    }
    .middle {
      width: 100%;
      background: yellow;
    }

    .right {
      width: 200px;
      background: aqua;
      margin-left: -200px;
      left: 200px;
    }
  </style>
</head>

<body>
  <div class="header">这里是头部</div>
  <div class="container">
    <div class="middle">中间部分</div>
    <div class="left">左边</div>
    <div class="right">右边</div>
  </div>
  <div class="footer">这里是底部</div>
</body>

</html>

3. Float布局来实现(左边 float: left, 右边 float: right)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .header,
    .footer {
      height: 40px;
      width: 100%;
      background: red;
    }

    .container {
      overflow: hidden;
    }

    .middle {
      background: yellow;
    }

    .left {
      float: left;
      width: 200px;
      background: pink;
    }

    .right {
      float: right;
      width: 200px;
      background: aqua;
    }
  </style>
</head>

<body>
  <div class="header">这里是头部</div>
  <div class="container">
    <div class="left">左边</div>
    <div class="right">右边</div>
    <div class="middle">中间部分</div>
  </div>
  <div class="footer">这里是底部</div>
</body>

</html>

4. 绝对定位来实现

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .header,
    .footer {
      height: 40px;
      width: 100%;
      background: red;
    }

    .container {
      min-height: 22px;
      position: relative;
    }

    .container>div {
      position: absolute;
    }

    .middle {
      left: 200px;
      right: 200px;
      background: yellow;
    }

    .left {
      left: 0;
      width: 200px;
      background: pink;
    }

    .right {
      right: 0;
      width: 200px;
      background: aqua;
    }
  </style>
</head>

<body>
  <div class="header">这里是头部</div>
  <div class="container">
    <div class="left">左边</div>
    <div class="right">右边</div>
    <div class="middle">中间部分</div>
  </div>
  <div class="footer">这里是底部</div>
</body>

</html>

5. Grid布局来实现

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <style>
    body {
      display: grid;
      text-align: center;
    }

    .header {
      background: red;
      grid-row: 1;
      grid-column: 1/5;
    }

    .left {
      grid-row: 2;
      grid-column: 1/2;
      background: orange;
    }

    .middle {
      grid-row: 2;
      grid-column: 2/4;
      background: rebeccapurple
    }

    .right {
      grid-row: 2;
      grid-column: 4/5;
      background: cadetblue;
    }


    .footer {
      background: gold;
      grid-row: 3;
      grid-column: 1/5;
    }
  </style>
</head>

<body>
  <div class="header">这里是头部</div>
  <div class="left">左边</div>
  <div class="middle">中间部分</div>
  <div class="right">右边</div>
  <div class="footer">这里是底部</footer>
  </div>
</body>

</html>

以上是关于前端面试 CSS— CSS 如何实现“圣杯”布局?的主要内容,如果未能解决你的问题,请参考以下文章

前端基础面试题之语义化+块级元素和内联元素+css布局盒模型

面试前端面试常考手写题 - JavaScript - CSS

前端面试 CSS— CSS 如何实现“品”字布局?

前端面试 CSS— CSS如何实现双飞翼布局?

前端vue实现圣杯布局flex布局浮动布局

前端vue实现圣杯布局flex布局浮动布局