第 28 章 CSS3 多列布局_html/css_WEB-ITnose
学习要点:
1.早期多列问题
2.属性及版本
3.属性解释
主讲教师:李炎恢
本章主要探讨 HTML5 中 CSS3 提供的多列布局,通过多列布局我们方便的创建流体的多列布局。
一.早期多列问题
我们有时想布局成报纸、杂志那样的多列方式(至少两列,一般三列以上),但早期 CSS 提供的布局方式都有着极大的限制。如果是固体布局,那么使用浮动或定位布局都可以完成。但对于流体的多列,比如三列以上,那只能使用浮动布局进行,而它又有极大的限制。因为它是区块性的,对于动态的内容无法伸缩自适应,基本无能力。
//五段内容,分为三列
<div> <p> 我是第一段内容....省略的部分复制大量文本 </p> <p> 我是第二段内容....省略的部分复制大量文本 </p> <p> 我是第三段内容....省略的部分复制大量文本 </p> <p> 我是第四段内容....省略的部分复制大量文本 </p> <p> 我是第五段内容....省略的部分复制大量文本 </p></div>
//带标题的五段内容,分为三列
<div> <h4 id="第一段标题">第一段标题</h4> <p> 我是第一段内容....省略的部分复制大量文本 </p> <h4 id="第二段标题">第二段标题</h4> <p> 我是第二段内容....省略的部分复制大量文本 </p> <h4 id="第三段标题">第三段标题</h4> <p> 我是第三段内容....省略的部分复制大量文本 </p> <h4 id="第四段标题">第四段标题</h4> <p> 我是第四段内容....省略的部分复制大量文本 </p> <h4 id="第五段标题">第五段标题</h4> <p> 我是第五段内容....省略的部分复制大量文本 </p></div>
上门两组内容,如果想用浮动和定位实现流体三列,基本不可能。并且实际应用中,需求可能多变,要更改成四列或者五列呢?所以,CSS3 提供了多列布局属性 columns 来实现这个动态变换的功能。
二.属性及版本
CSS3 提供了 columns 多列布局属性等 7 个属性集合,具体如下:
属性 | 说明 |
columns | 集成 column-width 和 column-count 两个属性 |
column-width | 定义每列列宽度 |
column-count | 定义分分列列数 |
column-gap | 定义列间距 |
column-rule | 定义列边框 |
column-span | 定义多列布局中子元素跨列效果,firefox 不支持 |
column-fill | 控制每列的列高效果,主流浏览器不支持 |
由于 column 属性集尚未纳入标准,大多数浏览器必须使用厂商前缀才能访问,好在主流的浏览器都可以很好的支持了。下面是主流浏览器的支持和前缀情况。
| Opera | Firefox | Chrome | Safari | IE |
支持需带前缀 | 11.5 ~ 29 | 2 ~ 40 | 4 ~ 45 | 3.1 ~ 8 | 无 |
支持不带前缀 | 无 | 无 | 无 | 无 | 10.0+ |
通过上面的表格,我们可以了解到,要想让最新的浏览器全部实现效果,都必须使用前缀。
//完整形式
-webkit-columns: 150px 4;-moz-columns: 150px 4;columns: border-box;
三.属性解释
为了方便演示,我们在 Firefox 火狐浏览器测试,只用-moz-前缀演示。
1.columns
columns 是一个复合属性,包含 columns-width 和 columns-count 这两种简写。意为同时设置分列列数和分列宽度。//分成四列,每列宽度自适应
-moz-columns: auto 4;
2.column-width
column-width 属性,用于设置每列的宽度。
//设置列宽
-moz-column-width: 200px;
这里设置了 200px,有点最小宽度的意思。当浏览器缩放到小于 200 大小时,将变成 1 列显示。而如果是 auto,则一直保持四列。
属性 | 说明 |
auto | 默认值,自适应。 |
长度值 | 设置列宽。 |
3.column-count
column-count 属性,用于设置多少列。
//设置列数
-moz-column-count: 4;
属性值 | 说明 |
auto | 默认值,表示就 1 列。 |
数值 | 设置列数。 |
4.column-gap
column-gap 属性,用于设置列间距
//设置列间距
-moz-column-gap: 100px;
属性值 | 说明 |
auto | 默认值,表示就 1 列。 |
数值 | 设置列数。 |
5.column-rule
column-rule 属性,设置每列中间的分割线//设置列边线
-moz-column-rule: 2px dashed gray;
属性 | 说明 |
column-rule | <宽度> <样式> <颜色>的边框简写形式。 |
column-rule-width | 单独设置边框宽度。 |
column-rule-style | 单独设置边框的样式。 |
column-rule-color | 单独设置边框的颜色。 |
列边线不会影响到布局,它会根据布局的缩放自我调整是否显示。如果我们把页面缩放到只能显示一列时,它自动消失了。
6.column-span
column-span 属性,设置跨列大标题。
//跨列标题,由于火狐尚未支持,固使用 webkit
-webkit-column-span: all;
属性 | 说明 |
none | 默认值,表示不跨列。 |
all | 表示跨列。 |

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

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.
