Table of Contents
定宽-自适应
float+margin
改进:float + margin + (fix)
float + overflow
table
flex
不定宽-自适应
等分布局
float
fix
等高需求
Home Web Front-end HTML Tutorial CSS多列布局方案整理_html/css_WEB-ITnose

CSS多列布局方案整理_html/css_WEB-ITnose

Jun 21, 2016 am 08:56 AM

来自: 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>
Copy after login

float+margin

  .left {    float: left;    width: 100px;  }  .right {    margin-left: 100px    /*间距可再加入 margin-left */  }
Copy after login

优点:容易理解

缺点: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>
Copy after login

兼容性很好,适用于多版本浏览器(包括 IE6)但是多了层right-fix的结构。但left不可选择,被right-fix盖住。需要设置left的 position: relative; 来提高层级。

float+overflow

 .left{        float: left;       width: 100px;       margin-right: 20px;  }  .right{ overflow: hidden; //触发BFC }
Copy after login

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;    /*宽度为剩余宽度*/  }
Copy after login

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 ;     } 
Copy after login

flex-item 默认为内容宽度。

缺点: CSS3 兼容性。根据内容判断,性能会有问题,做小范围布局,不适合大范围布局。

不定宽-自适应

float + overflow

  .left{        float: left;       width: 200px;       margin-right: 20px;  }  .right{ overflow: hidden; //触发BFC }
Copy after login

table

 .parent{       display:table;       width: 100%;  } .left, .right {        display: table-cell;  }  .left{        width:0.1%;  //写的够小就行,不写1px的原因是IE8会有问题       padding-right:20px;    }  .left p{ width : 200px;}  //用内部元素撑开,宽度可以不定
Copy after login

flex

.parent{display:flex;}.left{margin-right:20px;}.right{flex:1;}
Copy after login

三列不定宽 + 自适应 与两列的做法一样

等分布局

每一列的宽度和间距均相等。有如下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>
Copy after login

float

 .parent{    margin-left :-20px; //为父元素增加20px宽度    } .column{       float : left;       width: 25%;       padding-left : 20px;       box-sizing : border-box;      }
Copy after login

缺点:结构和样式稍微有点耦合性

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;  }
Copy after login

fix

 .parent{       display : flex;  }       .column{      flex: 1;  }  .column+.column{  margin-left: 20px;  } //选择2,3,4
Copy after login

等高需求

table

上面自适应的table布局就具有登高的布局

flex

如上面自适应的例子,flex 天然等高

float

 .parent{  overflow : hidden; } .left, .right{       padding-bottom : 9999px;       margin-bottom : -9999px;  }
Copy after login

优点是兼容性比较好缺点是伪等高 不是正真意义上的登高

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

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 of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

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: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

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.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

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: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

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 vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

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.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

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

See all articles