search
HomeWeb Front-endHTML Tutorial第 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

表示跨列。

Statement
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
HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 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 as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 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.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 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: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 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 OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 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: 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.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 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.

See all articles

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

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

MantisBT

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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),