Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
css如何实现三栏布局_创想鸟

css如何实现三栏布局

css如何实现三栏布局

实现方法:

一、float浮动

.layout.float .left-right-center{ height: 100px; } .layout.float .left{ float: left; width: 300px; background-color: red; } .layout.float .right{ float: right; width: 300px; background-color: blue; } .layout.float .center{ background-color: yellow; }
我是中间的自适应元素--浮动

原理:左右两个div由于浮动脱离了文档流,center就会上移,造成三栏布局的效果(前提是高度相同)

优点:兼容性高

缺点:需要清除浮动来防止影响其他元素

立即学习“前端免费学习笔记(深入)”;

如果高度不固定,中间的内容会被撑开,左右两边不会一起撑开

(推荐教程:CSS教程)

二、绝对定位

.layout.absolute .left-center-right div{ position: absolute; height: 100px; } .layout.absolute .left{ left: 0; width: 300px; background-color: red; } .layout.absolute .center{ left: 300px; right: 300px; background-color: yellow; } .layout.absolute .right{ right: 0; width: 300px; background-color: blue; }
我是中间的自适应元素--绝对定位

原理:利用绝对定位以及宽度,将左右两边的div固定住,中间div的宽度就会有自适应的效果

优点:快捷

缺点:如果父元素脱离了文档流,子元素一定会脱离文档流,运用的场景不多

如果中间元素的高度增加,两边元素的高度不会增加,所以只有中间的div会撑开

三、flex布局

.layout.flex .left-center-right{ display: flex; height: 100px; } .layout.flex .left{ width: 300px; background-color: red; } .layout.flex .center{ flex: 1; background-color: yellow; } .layout.flex .right{ width: 300px; background-color: blue; }
我是中间的自适应元素--flex布局

原理:将父元素设置为flex布局,然后中间元素设置flex为1,达到自适应的效果

优点:在实际开发中常用

缺点:IE8及以下的浏览器不支持

如果高度不固定,中间内容的高度撑开后,两边也会随之撑开

四、table布局

   
.layout.table .left-center-right{ display: table; height: 100px; width: 100%; } .layout.table .left{ display: table-cell; width: 300px; background-color: red; } .layout.table .center{ display: table-cell; background-color: yellow; } .layout.table .right{ display: table-cell; width: 300px; background-color: blue; }
我是中间的自适应元素--table

原理:将父元素设置为table布局,然后每个子元素都是teble-cell,给左右两个格子设置固定的宽度,中间的格子就可以达到自适应的效果

优点:兼容性好,可做flex布局在ie8以下的代替

缺点:局限性

如果高度不固定,中间被撑开时,左右两边也会被撑开,和flex类似

五、网格布局

.layout.grid .left-center-right{ display: grid; width: 100%; grid-template-rows: 100px;/*每一格都是100px高*/ grid-template-columns: 300px auto 300px;/*左右两格都是300px,中间是自适应*/ } .layout.grid .left{ background-color: red; } .layout.grid .center{ background-color: yellow; } .layout.grid .right{ background-color: blue; }
我是中间的自适应元素--grid布局

原理:将父元素设置为网格布局,然后规定每格的高度以及每格的宽度,只用分别给每格单独设置颜色即可

优点:技术比较新,方便

缺点:兼容性不是很好

如果高度不固定,中间元素添加文本,也不会撑开

相关视频教程推荐:css视频教程

以上就是css如何实现三栏布局的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1615026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
什么是margin重叠
上一篇 2025年12月24日 04:42:07
css实现文本图标对齐的方法
下一篇 2025年12月24日 04:42:19

相关推荐

发表回复

登录后才能评论
关注微信