学习要点:
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 | 表示跨列。 |

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

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

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

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

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

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

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


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

SublimeText3 Chinese version
Chinese version, very easy to use

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
