两栏布局和三栏布局

两栏布局

水平方向:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    .left{
      width: 300px;
      height:100px;
      background: red;
      float: left;
    }
    .right{
      width: 980px;
      height: 100px;
      background: blue;
      margin-left: 300px;
    }
  </style>
</head>

<body class="body">
  <div class="container">
    <div class="left">左</div>
    <div class="right">右</div>
  </div>
</body>

</html>

思路:让左栏元素左浮动,右栏元素设置等于左栏元素宽度的margin-left值。这是为了不影响右栏文本的布局

三栏布局

水平方向

1.浮动方法

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    .left{
      width: 300px;
      height:100px;
      background: red;
      float: left;
    }
    .right{
      width: 200px;
      height: 100px;
      background: blue;
      float: right;
    }
    .middle{
      margin-left: 300px;
      margin-right: 200px;
    }
  </style>
</head>

<body class="body">
  <div class="container">
    <div class="left">左</div>
    <div class="right">右</div>
    <div class="middle">中</div>
  </div>
</body>

</html>

思路:左右两栏用浮动固定,中间栏的margin-left为左栏的宽度,margin-right为右栏宽度。注意html元素的顺序为左右中

2.绝对定位

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    body,html,.container{
      margin: 0;
      padding: 0;
      height: 100%;
    }
    .left,.right{
      top: 0;
      height: 100%;
      background: red;
      position: absolute;
    }
    .left{
      left: 0;
      width: 100px;
    }
    .right{
      right: 0;
      width: 100px;
    }
    .middle{
      height: 100%;
      background: blue;
      margin: 0 100px 0 100px;
    }
  </style>
</head>

<body class="body">
  <div class="container">
    <div class="left">左</div>
    <div class="right">右</div>
    <div class="middle">中</div>
  </div>
</body>

</html>

思路:左右栏元素用绝对定位固定,中间元素设置margin值空出左右栏元素占用的位置

3.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>
    .container{
      display: flex;
    }
    .left{
      width: 100px;
      height: 100px;
      background: red;
    }
    .right{
      width: 100px;
      height: 100px;
      background: red;
    }
    .center{
      flex-grow: 1;
      height: 100px;
      background: blue;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="left">左</div>
    <div class="center">中</div>
    <div class="right">右</div>
  </div>
</body>

</html>

思路:父元素display:flex,方向默认row,左右宽度固定,中间设置flex-grow:1分配到剩下的所有空间

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">
  <title>Document</title>
  <style>
    body{
      min-width: 700px;/*left+right+left*/
    }
    #header,#footer{
      width: 100%;
      background: #ccc;
    }
    #container{
      width: 100%;
      height: 200px;
      background: peru;
      padding-left: 200px;
      padding-right: 300px;
      box-sizing: border-box;
    }
    .column{
      float: left;
      height: 200px;
    }
    #center{
      width: 100%;
      background: red;
    }
    #left{
      width: 200px;
      background: green;
      margin-left: -100%;
      position: relative;
      right: 200px;
    }
    #right{
      width: 300px;
      background: blue;
      margin-right: -300px;
    }
    #footer{
      clear: both;
    }
  </style>
</head>

<body>
  <header id="header">header</header>
  <div id="container">
    <div id="center" class="column">center</div>
    <div id="left" class="column">left</div>
    <div id="right" class="column">right</div>
  </div>
  <footer id="footer">footer</footer>
</body>

</html>

5.双飞翼布局

不用position:relative,暂时还不理解

垂直方向

1.position:absolute方法

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    .container div{
      position: absolute;
    }
    .top{
      top:0;
      width: 100%;
      height: 100px;
      background: red;
    }
    .center{
      width: 100%;
      top: 100px;
      bottom: 100px;
      background: green;
    }
    .bottom{
      bottom:0;
      width: 100%;
      height: 100px;
      background: blue;
    }
  </style>
</head>

<body class="body">
  <div class="container">
    <div class="top">上</div>
    <div class="center">中</div>
    <div class="bottom">下</div>
  </div>
</body>

</html>

思路:上下栏用绝对定位固定,中间栏也用绝对定位空出上下栏占用的位置

2.flex布局方法

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    body,html,.container{
      margin: 0;
      padding: 0;
      height: 100%;
    }
    .container{
      display: flex;
      flex-direction: column;
    }
   .top{
     width: 100%;
     height: 100px;
     background: red;
   }
   .bottom{
     width: 100%;
     height: 100px;
     background: red;
   }
   .middle{
     width:100%;
     background: blue;
     flex-grow: 1;
   }
  </style>
</head>

<body class="body">
  <div class="container">
    <div class="top">上</div>
    <div class="middle">中</div>
    <div class="bottom">下</div>
  </div>
</body>

</html>

思路:设置父元素为弹性盒布局,方向为column,上下栏高度为固定值,中间栏占用所有剩下的空间。

注意:flex-grow属性设置元素能够分到多少父元素剩余的空间,中间栏设置为1,上下栏不设置默认为0,所以中间栏分到父元素剩下的所有空间

小结

三栏布局的总体思路就是两端固定,中间空出两端占用的空间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值