前端面试--圣杯布局

209 阅读3分钟

基础布局

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Title</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .con{
      width: 980px;
      height: 500px;
      margin: 0 auto;
      background-color: coral;
    }
    .header{
      width: 100%;
      height: 100px;
      background-color: blue;
    }
    .content{
      width: 100%;
      height: 500px;
      background-color: yellow;
    }
    .content .left{
      width: 20%;
      float: left;
      height: 100%;
      background-color: green;
    }
    .content .right{
      width: 20%;
      float: right;
      height: 100%;
      background-color: saddlebrown;
    }
    .footer{
      width: 100%;
      height: 100px;
      background-color: purple;
    }
  </style>
</head>
<body>
<div class="con">
  <div class="header">
    #我是头部信息
  </div>
  <div class="content">
<!--    #我是内容-->

    <div class='left'>#left</div>
    <div class='right'>#right</div>
    <div class='middle'>#middle</div>
<!--    继续排列一列布局的时候   什么意思?-->
    <div class="clear"></div>
  </div>
  <div class="footer">
    #我是尾部
  </div>
</div>


</body>
</html>
#middle

要写到最下面

继续排列一列布局的时候 清除浮动什么意思?

规定在content 这个div快中 清除溢出的内容

right 的浮动要设置为左浮动

所以要给content左右都设置padding 预留空间给right 和 left

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Title</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .con{
      width: 980px;
      height: 500px;
      margin: 0 auto;
      background-color: coral;
    }
    .header{
      width: 100%;
      height: 100px;
      background-color: blue;
    }
    .content{
      /*给了width内容会溢出*/
      /*width: 100%;*/
      height: 500px;
      background-color: yellow;
      padding-left: 150px;
      padding-right: 100px;
    }
    .content .left{
      width: 150px;
      margin-left: -100%;
      float: left;
      height: 100%;
      background-color: green;
      position: relative;
      left: -150px;
    }
    .content .right{
      width: 100px;
      margin-right: -100%;
      /*float: right;*/
      float:left ;
      margin-left:-100px;
      height: 100%;
      background-color: saddlebrown;
      position: relative;
      right: -100px;
    }
    .content .middle{
      width: 100%;
      height: 100%;
      float: left;
      background-color: #f00;
    }
    .footer{
      width: 100%;
      height: 100px;
      background-color: purple;
    }
    .clear{
      clear: both;
    }
  </style>
</head>
<body>
<div class="con">
  <div class="header">
    #我是头部信息
  </div>
  <div class="content">
    <!--    #我是内容-->
    <div class='middle'>#middle</div>
    <div class='left'>#left</div>
    <div class='right'>#right</div>
    <div class="clear"></div>

  </div>
  <div class="footer">
    #我是尾部
  </div>
</div>


</body>
</html>

完成!!!

flex圣杯布局

<!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">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    /* 将整个页面设置为弹性盒 */
    html,
    body {
      height: 100%;
      overflow: hidden;
      display: flex;
      /* 改变主轴的排列方式 */
      flex-direction: column;
      /* 将主轴上的排列规则改为两端分布 */
      justify-content: space-between;
      text-align: center;
      font-size: 25px;
    }

    /* 设置头部和尾部的颜色 */
    .footer,
    .header {
      height: 88px;
      background: #c33;
      text-align: center;
      line-height: 88px;
      font-size: 30px;
    }

    /* 设置中间内容区域样式 */
    .center {
      flex: 1;
      background: #ccc;
      display: flex;
    }

    /* 设置左边div和右边div */
    .center>.left,.center>.right {
      width: 200px;
      height: 100%;
      background: yellow;
    }
    /* 将 */
    .center>.content {
      flex: 1;
      background: pink;
    }

  </style>
</head>

<body>
<!-- 界面结构区域 -->
<div class="header">header</div>
<div class="center">
  <div class="left">left</div>
  <div class="content">content</div>
  <div class="right">right</div>
</div>
<div class="footer">footer</div>
</body>

</html>