CSS多列布局方案整理_html/css_WEB-ITnose
来自: https://segmentfault.com/a/1190000004500652
了解 CSS 中属性的值及其特性, 透彻分析问题和需求才可以选择和设计最适合的布局解决方案。
多列布局在网页中非常常见(例如两列布局),多列布局可以是两列定宽,一列自适应, 或者多列不定宽一列自适应还有等分布局等。
定宽-自适应
有如下布局
<div class="parent"> <div class="left"> <p>left</p> </div> <div class="right"> <p>right</p> <p>right</p> </div></div>
float+margin
.left { float: left; width: 100px; } .right { margin-left: 100px /*间距可再加入 margin-left */ }
优点:容易理解
缺点:IE6中会有3像素的BUG, 解决方法可以在.left 加入 margin-left:-3px 。
因为left是浮动元素,right是没有浮动的,如果right内容中有清楚浮动就会产生bug。
改进:float + margin + (fix)
<div class="parent"> <div class="left"> <p>left</p> </div> /*外层在包裹一个容器*/ <div class="right-fix"> <div class="right"> <p>right</p> <p>right</p> </div> </div></div><style> .left { float: left; width: 100px; } .right-fix { float: right; width: 100%; margin-left: -100px; } .right { margin-left: 100px /*间距可再加入 margin-left */ }</style>
兼容性很好,适用于多版本浏览器(包括 IE6)但是多了层right-fix的结构。但left不可选择,被right-fix盖住。需要设置left的 position: relative; 来提高层级。
float+overflow
.left{ float: left; width: 100px; margin-right: 20px; } .right{ overflow: hidden; //触发BFC }
overflow:hidden 使得div产生了BFC 模式(Block Formatting Context)块级格式化文本,根据BFC的布局规则之一,div那个产生的BFC区域不会与左边浮动的元素所重叠。具体什么是BFC可以看看这篇 文章
table
.parent { display: table; width: 100%; table-layout: fixed; } .left { display: table-cell; width: 100px; } .right { display: table-cell; /*宽度为剩余宽度*/ }
table 的显示特性为每列的单元格宽度合一定等与表格宽度。 table-layout: fixed; 可加速渲染,也是设定布局优先。
table-cell 中不可以设置 margin 但是可以通过 padding 来设置间距。
flex
.parent{ display: flex ; } .left { width: 100px; margin-right: 20px; } .right{ /*等价于flex:1 1 0; 增长因子和收缩因子都为1, 基础宽度为0,那么剩余宽度都分配给right*/ flex: 1 ; }
flex-item 默认为内容宽度。
缺点: CSS3 兼容性。根据内容判断,性能会有问题,做小范围布局,不适合大范围布局。
不定宽-自适应
float + overflow
.left{ float: left; width: 200px; margin-right: 20px; } .right{ overflow: hidden; //触发BFC }
table
.parent{ display:table; width: 100%; } .left, .right { display: table-cell; } .left{ width:0.1%; //写的够小就行,不写1px的原因是IE8会有问题 padding-right:20px; } .left p{ width : 200px;} //用内部元素撑开,宽度可以不定
flex
.parent{display:flex;}.left{margin-right:20px;}.right{flex:1;}
三列不定宽 + 自适应 与两列的做法一样
等分布局
每一列的宽度和间距均相等。有如下HTML结构
<div class="parent"> <div class="column"><p>1</p></div> <div class="column"><p>2</p></div> <div class="column"><p>3</p></div> <div class="column"><p>4</p></div></div>
float
.parent{ margin-left :-20px; //为父元素增加20px宽度 } .column{ float : left; width: 25%; padding-left : 20px; box-sizing : border-box; }
缺点:结构和样式稍微有点耦合性
table
.parent-fix{ margin-left :-20px; //为父元素增加20px宽度 } .parent{ display : table; width : 100%; table-layout : fixed; } .column{ float : left; width: 25%; padding-left : 20px; box-sizing : border-box; }
fix
.parent{ display : flex; } .column{ flex: 1; } .column+.column{ margin-left: 20px; } //选择2,3,4
等高需求
table
上面自适应的table布局就具有登高的布局
flex
如上面自适应的例子,flex 天然等高
float
.parent{ overflow : hidden; } .left, .right{ padding-bottom : 9999px; margin-bottom : -9999px; }
优点是兼容性比较好缺点是伪等高 不是正真意义上的登高

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.
