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

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

Jun 21, 2016 am 08:56 AM

了解 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; 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

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

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

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

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.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

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

See all articles