多列布局方案整理_html/css_WEB-ITnose
了解 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

AI Hentai Generator
Generate AI Hentai for free.

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



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.
