前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >前端面试 【CSS】— CSS 如何实现“圣杯”布局?

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

作者头像
越陌度阡
发布2021-11-16 16:14:41
3980
发布2021-11-16 16:14:41
举报

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

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

1. Flex布局来实现

代码语言:javascript
复制
<!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)

代码语言:javascript
复制
<!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)

代码语言:javascript
复制
<!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. 绝对定位来实现

代码语言:javascript
复制
<!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布局来实现

代码语言:javascript
复制
<!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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-11-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Flex布局来实现
  • 2. Float布局来实现(全部float:left)
  • 3. Float布局来实现(左边 float: left, 右边 float: right)
  • 4. 绝对定位来实现
  • 5. Grid布局来实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档