意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

CSS中用table可以实现哪些布局,代码是什么

来源:恒创科技 编辑:恒创科技编辑部
2024-01-11 13:15:59
这篇文章给大家分享的是“CSS中用table可以实现哪些布局,代码是什么”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。


CSS中用table可以实现哪些布局,代码是什么

本文介绍了CSS 利用table实现五种常用布局的方法示例,分享给大家,具体如下:

布局一:

效果:

代码:

html:

<div class="header">header</div>
<div class="main">main</div>
<div class="footer">footer</div>

注意:div中要有内容,不然显示不出来

css:

body{
  margin:0;
  padding:0;
  width:100%;
  min-height:100vh;
  display:table;
  text-align:center;
}
.header,.main,.footer{
  display:table-row;
}
.header{
  height:50px;
  background:tomato;
}
.main{
  background:skyblue;
}
.footer{
  height:50px;
  background:#9d70ff;
}

布局二:

效果:

代码:

html:

<div class="header">header</div>
<div class="main">
  <div class="left">left</div>
  <div class="right">right</div>
</div>
<div class="footer">footer</div>

css:

body{
  margin:0;
  padding:0;
  width:100%;
  min-height:100vh;
  display:table;
  text-align:center;
}
.header,.main,.footer{
  display:table-row;
}
.header{
  height:50px;
  background:tomato;
}
.main{
  width:100%;
  display:table;
  height:calc(100vh - 100px);
}
.main .left{
  width:300px;
  display:table-cell;
  background:#fcea96;
}
.main .right{
  display:table-cell;
  background:skyblue;
}
.footer{
  height:50px;
  background:#9d70ff;
}

注意:.main的height属性中的100px是header和footer的高度之和

布局三:

效果:

代码:

html:

<div class="left">left</div>
<div class="right">
  <div class="header">header</div>
  <div class="main">main</div>
  <div class="footer">footer</div>
</div>

css:

body{
  margin:0;
  padding:0;
  min-height:100vh;
  display:table;
  text-align:center;
}
.left{
  display:table-cell;
  width:200px;
  background:tomato;
}
.right{
  display:table;
  width:calc(100vw - 200px);
  height:100vh;
}
.header,.main,.footer{
  display:table-row;
}
.header{
  height:50px;
  background:skyblue;
}
.main{
  background:#fcea96;
}
.footer{
  height:50px;
  background:#9d70ff;
}

布局四(双栏布局,例子为左边固定,右边自适应):

效果:

代码:

html:

<div class="left">left</div>
<div class="right">right</div>

css:

body{
  margin:0;
  padding:0;
  width:100%;
  height:100vh;
  display:table;
  text-align:center;
}
.left,.right{
  display:table-cell;
}
.left{
  width:300px;
  background:tomato;
}
.right{
  background:skyblue;
}

布局五(三栏布局,例子为左边固定,右边固定,中间自适应):

效果:

代码:

html:

<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>

css:

body{
  margin:0;
  padding:0;
  width:100%;
  height:100vh;
  display:table;
  text-align:center;
}
.left,.middle,.right{
  display:table-cell;
}
.left{
  width:300px;
  background:tomato;
}
.middle{
  background:#ffe69e;
}
.right{
  width:200px;
  background:skyblue;
}

以上就是关于“CSS中用table可以实现哪些布局,代码是什么”的介绍了,感谢各位的阅读,希望这篇文章能帮助大家解决问题。如果想要了解更多知识,欢迎关注群英网络,小编每天都会为大家更新不同的知识。
上一篇: 如何用CSS3制作元素依次显示的效果,方法是什么 下一篇: 手机怎么远程登录云服务器?