资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

CSS两列布局和三列布局的用法

这篇文章主要介绍了CSS两列布局和三列布局的用法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的芙蓉网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

两列布局

左列定宽,右列自适应

CSS两列布局和三列布局的用法

float + margin 布局

html 代码


  左列定宽
  右列自适应

css 代码:

#left {
  float: left;
  width: 200px;
  height: 400px;
  background-color: lightblue;
}
#right {
  margin-left: 200px; /* 大于或等于左列的宽度 */
  height: 400px;
  background-color: lightgreen;
}

float + overflow 布局

html 代码


  左列定宽
  右列自适应

css 代码

#left {
  float: left;
  width: 200px;
  height: 400px;
  background-color: lightblue;
}
#right {
  overflow: hidden;
  height: 400px;
  background-color: lightgreen;
}

绝对定位布局

html 代码:


  
    左列定宽
    右列自适应
  

css 代码:

#parent {
  position: relative;
}
#left {
  position: absolute;
  top: 0;
  left: 0;
  width: 200px;
  height: 400px;
  background-color: lightblue;
}
#right {
  position: absolute;
  top: 0;
  left: 200px;
  right: 0;
  height: 400px;
  background-color: lightgreen;
}

table 布局

html 代码:


  
    左列定宽
    右列自适应
  

css 代码:

#parent {
  width: 100%;
  height: 400px;
  display: table;
}
#left,
#right {
  display: table-cell;
}
#left {
  width: 200px;
  background-color: lightblue;
}
#right {
  background-color: lightgreen;
}

flex 布局

html 代码:


  
    左列定宽
    右列自适应
  

css 代码:

#parent {
  width: 100%;
  height: 400px;
  display: flex;
}
#left {
  width: 200px;
  background-color: lightblue;
}
#right {
  flex: 1;
  background-color: lightgreen;
}

grid 网格布局

html 代码:


  
    左列定宽
    右列自适应
  

css 代码:

#parent {
  width: 100%;
  height: 400px;
  display: grid;
  grid-template-columns: 200px auto;
}
#left {
  background-color: lightblue;
}
#right {
  background-color: lightgreen;
}

左列不定宽,右列自适应

左列盒子宽度随着内容增加或减少发生变化,右列盒子自适应

float + overflow 布局

html 代码:


  左列不定宽
  右列自适应

css 代码:

#left {
  float: left;
  height: 400px;
  background-color: lightblue;
}
#right {
  overflow: hidden;
  height: 400px;
  background-color: lightgreen;
}

flex 布局

html 代码:


  
    左列不定宽
    右列自适应
  

css 代码:

#parent {
  display: flex;
  height: 400px;
}
#left {
  background-color: lightblue;
}
#right {
  flex: 1;
  background-color: lightgreen;
}

grid 布局

html 代码:


  
    左列不定宽
    右列自适应
  

css 代码:

#parent {
  display: grid;
  grid-template-columns: auto 1fr;
  height: 400px;
}
#left {
  background-color: lightblue;
}
#right {
  background-color: lightgreen;
}

三列布局

两列定宽,一列自适应

CSS两列布局和三列布局的用法

float + margin 布局

html 代码:


  
    左列定宽
    中间列定宽
    右列自适应
  

css 代码:

#parent {
  height: 400px;
}
#left {
  float: left;
  width: 100px;
  height: 400px;
  background-color: lightblue;
}
#center {
  float: left;
  width: 200px;
  height: 400px;
  background-color: lightgrey;
}
#right {
  margin-left: 300px; /* 左列的宽度 + 中间列的宽度 */
  height: 400px;
  background-color: lightgreen;
}

float + overflow 布局

html 代码:


  
    左列定宽
    中间列定宽
    右列自适应
  

css 代码:

#parent {
  height: 400px;
}
#left {
  float: left;
  width: 100px;
  height: 400px;
  background-color: lightblue;
}
#center {
  float: left;
  width: 200px;
  height: 400px;
  background-color: lightgrey;
}
#right {
  overflow: hidden;
  height: 400px;
  background-color: lightgreen;
}

table 布局

html 代码:


  
    左列定宽
    中间列定宽
    右列自适应
  

css 代码:

#parent {
  display: table;
  width: 100%;
  height: 400px;
}
#left {
  display: table-cell;
  width: 100px;
  background-color: lightblue;
}
#center {
  display: table-cell;
  width: 200px;
  background-color: lightgrey;
}
#right {
  display: table-cell;
  background-color: lightgreen;
}

flex 布局

html 代码:


  
    左列定宽
    中间列定宽
    右列自适应
  

css 代码:

#parent {
  display: flex;
  width: 100%;
  height: 400px;
}
#left {
  width: 100px;
  background-color: lightblue;
}
#center {
  width: 200px;
  background-color: lightgrey;
}
#right {
  flex: 1;
  background-color: lightgreen;
}

grid 布局

html 代码


  
    左列定宽
    中间列定宽
    右列自适应
  

css 代码

#parent {
  display: grid;
  grid-template-columns: 100px 200px auto;
  width: 100%;
  height: 400px;
}
#left {
  background-color: lightblue;
}
#center {
  background-color: lightgrey;
}
#right {
  background-color: lightgreen;
}

左右定宽,中间自适应

CSS两列布局和三列布局的用法

圣杯布局和双飞翼布局目的都是希望先加载的是中间的部分,然后再开始加载 left 和 right 两部分相对来说不是很重要的东西。

圣杯布局

圣杯布局:为了让中间的内容不被遮挡,将中间 div(或最外层父 div)设置 padding-left 和 padding-right (值等于 left 和 right 的宽度),将左右两个 div 用相对布局 position: relative 并分别配合 left 和 right 属性,以便左右两栏 div 移动后不遮挡中间 div。

html 代码:


  
    中间列
    左列
    右列
  

css 代码:

#parent {
  height: 400px;
  padding: 0 200px;
  overflow: hidden;
}
#left,
#right {
  float: left;
  width: 200px;
  height: 100%;
  position: relative;
  background-color: lightblue;
}
#left {
  margin-left: -100%; /* 使 #left 上去一行 */
  left: -200px;
}
#right {
  right: -200px;
  margin-left: -200px; /* 使 #right 上去一行 */
}
#center {
  float: left;
  width: 100%;
  height: 100%;
  background-color: lightgrey;
}

双飞翼布局

双飞翼布局,为了中间 div 内容不被遮挡,直接在中间 div 内部创建子 div 用于放置内容,在该子 div 里用 margin-left 和 margin-right 为左右两栏 div 留出位置。

html 代码:


  
    
      中间列
    
    左列
    右列
  

css 代码:

#left,
#right {
  float: left;
  width: 200px;
  height: 400px;
  background-color: lightblue;
}
#left {
  margin-left: -100%; /* 使 #left 上去一行 */
}
#right {
  margin-left: -200px; /* 使 #right 上去一行 */
}
#center {
  float: left;
  width: 100%;
  height: 400px;
  background-color: lightgrey;
}
#center-inside {
  height: 100%;
  margin: 0 200px;
}

flex 实现

html 代码:


  
    中间列
    左列
    右列
  

css 代码:

#parent {
  display: flex;
}
#left,
#right {
  flex: 0 0 200px;
  height: 400px;
  background-color: lightblue;
}
#left {
  order: -1; /* 让 #left 居于左侧 */
}
#center {
  flex: 1;
  height: 400px;
  background-color: lightgrey;
}

感谢你能够认真阅读完这篇文章,希望小编分享的“CSS两列布局和三列布局的用法”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


网站名称:CSS两列布局和三列布局的用法
文章地址:http://cdkjz.cn/article/gogppg.html
返回首页 了解更多建站资讯
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220