css布局

120 阅读1分钟

1圣杯布局

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

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    header {
      height: 400px;
      width: 100%;
      background: #FFD34E;
    }

    footer {
      height: 100px;
      width: 100%;
      background: rgb(210, 209, 208);
      clear: both;
    }

    .main {
      float: left;
      width: 100%;
      height: 300px;
      background: rgb(80, 97, 192);
    }

    .left {
      width: 200px;
      height: 300px;
      background: rgb(131, 124, 104);
      float: left;
      margin-left: -100%;
      position: relative;
      left: -200px;
    }

    .right {
      width: 200px;
      height: 300px;
      background: rgb(131, 124, 104);
      float: left;
      margin-left: -200px;
      position: relative;
      left: 200px;
    }

    .content {
      padding: 0 200px;
    }
  </style>
</head>
<header class="header">头部</header>
<div class="content">
  <div class="main">主要内容</div>
  <div class="left">左侧栏</div>
  <div class="right">右侧栏</div>
</div>
<footer class="footer">底部</footer>
<script>
</script>
</body>

</html>

效果图:

实现思路

中间三列都是左侧浮动,left块的margin-left设置为-100%,right块的margin-left=-200px,这样三列就等高了,但是这个时候,main的宽度依然是100%,左右块在main上面吧,包含块设置一个padding:0 200px,然后把left,right都设置为相对定位,left:-200px和left=200px,就使左右两个块占据了Padding200px的位置,中间框在content里面,就实现了不管怎么改变页面大小,都是左右固定,中间自适应。

缺点: 页面过小的时候格式会乱(用min-width可以解决)

2 双翼飞布局

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

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style>
   header {
     height: 400px;
     width: 100%;
     background: #FFD34E;
   }

   footer {
     height: 100px;
     width: 100%;
     clear: both;
     background: rgb(210, 209, 208);
   }

   .main {
     float: left;
     width: 100%;
     height: 300px;
     background: rgb(80, 97, 192);
   }

   .main .center {
     height: 300px;
     margin: 0 200px;
     background: rgb(60, 66, 101);
   }

   .left {
     float: left;
     width: 200px;
     height: 300px;
     margin-left: -100%;
     background: rgb(131, 124, 104);
   }

   .right {
     float: left;
     width: 200px;
     height: 300px;
     margin-left: -200px;
     background: rgb(131, 124, 104);
   }
 </style>
</head>
<header class="header">头部</header>
<div class="content">
 <div class="main">
   <div class="center">主要内容</div>
 </div>
 <div class="left">左侧栏</div>
 <div class="right">右侧栏</div>
</div>
<footer class="footer">底部</footer>
<script>
</script>
</body>

</html>

实现思路

相比于圣杯布局,不同点是用一个div包裹main,left和right没有了相对定位,使main被一个左侧浮动的div包裹,这个div会挨着上一个浮动元素排列,显示在中间,没有了页面很小时布局混乱的现象。

缺点:增加了一个dom元素,渲染性能降低

flex布局

<!DOCTYPE html>
<html>
 <body>
   <header class="header">头部</header>
   <div class="content">
     <div class="left">左侧栏</div>   
     <div class="main">主要内容</div>
     <div class="right">右侧栏</div>
   </div>
   <footer class="footer">底部</footer>
 </body>
</html>
<style>
header {
 height: 400px;
 width: 100%;
 background: #FFD34E;
}

footer {
 height: 100px;
 width: 100%;
 clear: both;
 background: rgb(210, 209, 208);
}

.content {
 display: flex;
}

.main {
 width: 100%;
 height: 300px;
 background: rgb(80, 97, 192);
}

.left {
 width: 200px;
 height: 300px;
 background: rgb(131, 124, 104);
}

.right {
 width: 200px;
 height: 300px;
 background: rgb(131, 124, 104);
}
</style>

实现思路

注意左中右的顺序,centent包裹块设置为flex,main的宽度设置为100%,或者flex-grow :1。简单好理解