三栏布局的三个典型方法(圣杯、双飞翼、flex)

聊聊三栏布局—-左右定宽,中间自适应。

效果图:

《三栏布局的三个典型方法(圣杯、双飞翼、flex)》

圣杯布局

<!DOCTYPE html>
<html>
<head lang="en">
<title>圣杯</title>
<style>
.container{
    padding:0 200px 0 180px;
    height:100px;
}
.left{
    float:left;
    width:180px;
    height:100px;
    margin-left:-100%;
    background:red;
    position:relative;
    left:-180px;
}
.main{
    float:left;
    width:100%;
    height:100px;
    background:blue;
}

.right{
    float:left;
    width:200px;
    height:100px;
    margin-left:-200px;
    background:green;
    position:relative;
    right:-200px;
}

</style>
</head>
<body>
<div class="container">
  <div class="main">middle</div>
  <div class="left">left</div>
  <div class="right">right</div>
</div>
</body>
</html>


双飞翼布局

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>双飞翼</title>
    <style>
.main{
    float:left;
    width:100%; 
    height:100px;
    background:blue;
}
.left{
    float:left;
    width:180px;
    height:100px;
    margin-left:-100%;
    background:red;
}
.right{
    float:left;
    width:200px;
    height:100px;
    margin-left:-200px;
    background:green;
}
.inline{
margin:0 200px 0 180px;
}
</style>
</head>
<body>
  <div class="main">
    <div class="inline">middle</div> 
  </div>
  <div class="left">left</div>
  <div class="right">right</div>
</body>
</html>

注意:一定要在要在main中再包裹一个<div>并设置它的margin:0 180px 0 200px。

Flex布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex</title>
    <style>
.flex {
    display: flex;
    flex-flow: row;
}
.left{
    width: 180px;
    height: 100px;    
    background-color: red;
}
.main{
    flex: 1; 
    height: 100px;
    background-color: blue;
}
.right {
    width: 200px;
    height: 100px;
    background-color: green;
}
    </style>
</head>
<body>
<div class="flex">
    <div class="left">left</div>
    <div class="main">middle</div>
    <div class="right">right</div>
</div>
</body>
</html>

最重要的还是要理解浮动和负margin技术以及width:100%。

    原文作者:mingo
    原文地址: https://segmentfault.com/a/1190000015943175
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞