CSS —— 5种方式实现三栏布局

5种方式实现三栏布局

1.使用float

<style type="text/css">
  .useFloat{
    margin-top: 10px;
  }
  .useFloat>div{
    height: 100px;
    line-height: 100px;
  }
  .left1{
    width: 300px;
    float: left;
    background-color: #7FFFD4;
  }
  .right1{
    width: 300px;
    float: right;
    background-color: #FA8072;
  }
  .center1{
    background-color: #FAEBD7;
    margin-left: 300px;
    margin-right: 300px;
  }
</style>
<div class="useFloat">
    <div class="left1">使用float的left</div>   
    <div class="right1">使用float的right</div>
    <div class="center1">使用float的center</div>
</div>

2.使用position

<style type="text/css">
.usePosition{
    position: relative;
    margin-top: 10px;
    height: 100px;
}
.usePosition>div{
    height: 100px;
    line-height: 100px;
}
.left2{
    width: 300px;
    position: absolute;
    left: 0;
    background-color: #7FFFD4;
}
.right2{
    width: 300px;
    position: absolute;
    right: 0;
    background-color: #FA8072;
}
.center2{
    position: absolute;
    left: 300px;
    right: 300px;
    background-color: #FAEBD7; 
}
</style>
<div class="usePosition"> 
    <div class="left2">使用position的left</div>
    <div class="right2">使用position的right</div>
    <div class="center2">使用position的center</div>
</div>

3.使用flex

<style type="text/css">
.useFlex{
    margin-top: 10px;
    display: flex;
}
.useFlex>div{
    height: 100px;
    line-height: 100px;
}
.left3{
    width: 300px;
    background-color: #7FFFD4;
}
.right3{
    width: 300px;
    background-color: #FA8072;
}
.center3{
    flex: 1;
    background-color: #FAEBD7;
}
</style>
<div class="useFlex">
    <div class="left3">使用flex的left</div>
    <div class="center3">使用flex的center</div>
    <div class="right3">使用flex的right</div>          
</div>

4.使用table

<style type="text/css">
.useTable{
    display: table;
    width: 100%;
    margin-top: 10px;
    height: 100px;
    line-height: 100px;
}
.useTable>div{
    height: 100px;
}
.left4,.center4,.right4{
    display: table-cell;
}
.left4{
    width: 300px;
    background-color: #7FFFD4;
}
.center4{
    background-color: #FAEBD7;
}
.right4{
    width: 300px;
    background-color: #FA8072;
}
</style>
<div class="useTable">
    <div class="left4">使用table的left</div>
    <div class="center4">使用table的center</div>
    <div class="right4">使用table的right</div>
</div>

5.使用grid

<style type="text/css">
.useGrid{
    display: grid;
    grid-template-rows: 100px;
    grid-template-columns: 300px auto 300px;
    margin-top: 10px;
    line-height: 100px;
}
.left5{
    background-color: #7FFFD4;
}
.center5{
    background-color: #FAEBD7;
}
.right5{
    background-color: #FA8072;
}
</style>
<div class="useGrid">
    <div class="left5">使用grid的left</div>
    <div class="center5">使用grid的center</div>
    <div class="right5">使用grid的right</div>
</div>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 经过前期的准备工作,现在就可以正确的开始布局了。这里也将是页面布局开始的地方。 常规的文档流布局 这种是最基础的布...
    loster阅读 3,532评论 0 0
  • 转载自:https://segmentfault.com/a/1190000013565024 前端布局非常重要的...
    天字一等阅读 3,165评论 0 5
  • 居中布局 水平居中 1)使用inline-block+text-align(1)原理、用法 原理:先将子框由块级元...
    littlesiqi阅读 1,828评论 0 0
  • 抗战老兵,朝鲜战争老兵,对印自卫反击战老兵,对越自卫反击战老兵。这些在那种战火纷飞的艰苦环境下生存下来的人,正在漫...
    一杯酒未消愁阅读 3,770评论 20 7
  • 妈妈又错了 妈妈又错了,总是这样的忏悔!你因为桃子姐姐洗澡去了,而痛哭不已,并跑到厕所门上不停的敲门喊着:桃子姐姐...
    妮小妞的嬉闹生活阅读 1,868评论 0 0

友情链接更多精彩内容