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
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

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 Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.