css常用布局

居中布局

以下居中布局均以不定宽为前提,定宽情况包含其中

1、水平居中

程序员花4小时总结:CSS如何去布局及运用方案

a) inline-block + text-align

程序员花4小时总结:CSS如何去布局及运用方案

tips:此方案兼容性较好,可兼容至IE8,对于IE567并不支持inline-block,需要使用css hack进行兼容

b) table + margin

程序员花4小时总结:CSS如何去布局及运用方案

tips:此方案兼容至IE8,可以使用<table/>代替css写法,兼容性良好

c) absolute + transform

程序员花4小时总结:CSS如何去布局及运用方案

tips:此方案兼容至IE9,因为transform兼容性限制,如果.child为定宽元素,可以使用以下写法,兼容性极佳

程序员花4小时总结:CSS如何去布局及运用方案

d) flex + justify-content

程序员花4小时总结:CSS如何去布局及运用方案

tips:flex是一个强大的css,生而为布局,它可以轻松的满足各种居中、对其、平分的布局要求,但由于现浏览器兼容性问题,此方案很少被使用,但是值得期待浏览器兼容性良好但那一天!

2、垂直

程序员花4小时总结:CSS如何去布局及运用方案

a) table-cell + vertial-align

.parentdisplay: table-cell; vertical-align: middle;}

tips:可替换成<table />布局,兼容性良好

b) absolute + transform

程序员花4小时总结:CSS如何去布局及运用方案

tips:存在css3兼容问题,定宽兼容性良好

c) flex + align-items

.parentdisplay: flex; align-items: center;}

tips:高版本浏览器兼容,低版本不适用

3、水平垂直

程序员花4小时总结:CSS如何去布局及运用方案

a) inline-block + table-cell + text-align + vertical-align

程序员花4小时总结:CSS如何去布局及运用方案

tips:兼容至IE8

b) absolute + transform

程序员花4小时总结:CSS如何去布局及运用方案

tips:兼容性稍差,兼容IE10以上

c) flex

.parentdisplay: flex; justify-content: center; align-items: center;}

tips:兼容差

多列布局

1、一列定宽,一列自适应

程序员花4小时总结:CSS如何去布局及运用方案

a) float + margin

程序员花4小时总结:CSS如何去布局及运用方案

tips:此方案对于定宽布局比较好,不定宽布局推荐方法b

b) float + overflow

程序员花4小时总结:CSS如何去布局及运用方案

tips:个人常用写法,此方案不管是多列定宽或是不定宽,都可以完美实现,同时可以实现登高布局

c) table

程序员花4小时总结:CSS如何去布局及运用方案

d) flex

程序员花4小时总结:CSS如何去布局及运用方案

2、多列定宽,一列自适应

程序员花4小时总结:CSS如何去布局及运用方案

a) float + overflow

程序员花4小时总结:CSS如何去布局及运用方案

b) table

程序员花4小时总结:CSS如何去布局及运用方案

c) flex

程序员花4小时总结:CSS如何去布局及运用方案

3、一列不定宽,一列自适应

程序员花4小时总结:CSS如何去布局及运用方案

a) float + overflow

程序员花4小时总结:CSS如何去布局及运用方案

b) table

程序员花4小时总结:CSS如何去布局及运用方案

c) flex

程序员花4小时总结:CSS如何去布局及运用方案

4、多列不定宽,一列自适应

程序员花4小时总结:CSS如何去布局及运用方案

a) float + overflow

.left,.centerfloat: left; margin-right20px;}.rightoverflow: hidden;}.left p,.center pwidth100px;}

5、等分

程序员花4小时总结:CSS如何去布局及运用方案

a) float + margin

.parentmargin-left: -20px;}.columnfloat: left; width25%padding-left20pxbox-sizing: border-box;}

b) table + margin

.parent-fixmargin-left: -20px;}.parentdisplay: table; width:100%table-layout: fixed;}.columndisplay: table-cell; padding-left20px;}

c) flex

.parentdisplay: flex;}.columnflex1;}.column+.columnmargin-left:20px;}

6、等高

程序员花4小时总结:CSS如何去布局及运用方案

a) float + overflow

.parentoverflow: hidden;}.left,.rightpadding-bottom9999pxmargin-bottom: -9999px;}.leftfloat: left; width100px;}.rightoverflow: hidden;}

b) table

.parentdisplay: table; width100%;}.leftdisplay:table-cell; width100pxmargin-right20px;}.rightdisplay:table-cell;}

c) flex

.parentdisplay:flex; width100%;}.leftwidth100px;}.rightflex:1;}

并排等分,单排对齐靠左布局

效果图

程序员花4小时总结:CSS如何去布局及运用方案

flex

.main { display: flex; flex-flow: row wrap; justify-content: space-between;}.item { display: inline-block;}.emptyheight0visibility: hidden;}

 

posted @   卡卡西村长  阅读(351)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示