Home Web Front-end HTML Tutorial 第 29 章 CSS3 弹性伸缩布局[上] - 水之原

第 29 章 CSS3 弹性伸缩布局[上] - 水之原

May 20, 2016 pm 01:49 PM

学习要点:

1.布局简介

2.旧版本

 

主讲教师:李炎恢

 

本章主要探讨 HTML5 中 CSS3 提供的用来实现未来响应式弹性伸缩布局方案,这里做一个初步的了解。

 

一.布局简介

  CSS3 提供一种崭新的布局方式:Flexbox 布局,即弹性伸缩布局模型(Flexible Box)。用来提供一个更加有效的方式实现响应式布局。但是用于这个布局方式还处于 W3C 的草案阶段,并且它还分为旧版本、新版本以及混合过渡版本三种不同的编码方式。在发展中,可能还有各种改动,浏览器的兼容性还存在问题。所以,本节课作为初步了解即可。

首先,我们来看下旧版本的浏览器兼容情况:

Flexbox 旧版本兼容情况

属性

IE

Firefox

Chrome

Opera

Safari

带前缀

4 ~ 25

4 ~ 31

15 ~ 18

5.17+

不带前缀

以上的数据,我们摘自 CSS3 手册上的。当然,不同的教材和文章的会略有不同。但误差率也就一到两个版本,影响不大。

首先,第一步:先创建一段内容,分成三个区域。

//HTML 部分 

<span style="color: #0000ff;"><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">
        第一段内容...
    </span><span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">
        第二段内容...
    </span><span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">
        第三段内容...
    </span><span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span>
Copy after login

//CSS 部分 

<span style="color: #800000;">p </span>{<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 150px</span>;<span style="color: #ff0000;">
    border</span>:<span style="color: #0000ff;"> 1px solid gray</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> 5px</span>;<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;"> 5px</span>;
}<span style="color: #800000;">

div </span>{<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;"> -moz-box</span>;<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;"> -webkit-box</span>;<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;"> box</span>;
}
Copy after login

通过以上设置,在除了 IE 浏览器外,布局实现了水平分布。那么下面,我们就重点研究一下这些属性的含义。

 

二.旧版本

如果属性和属性值为:display:box,那么就是 2009 年 7 月份设定的工作草案,属于旧版本。它面向的是一些早期浏览器的弹性布局方案。

首先,我们要将几个需要布局模块的父元素设置一下容器属性 display。

属性值

说明

box

将容器盒模型作为块级弹性伸缩盒显示(旧版本)

inline-box

将容器盒模型作为内联级弹性伸缩盒显示(旧版本)

我们知道块级它是占用整行的,类似

元素;而内联级不占用整行,类似元素。但是我们设置了整个盒子,他们都不占用,保持一致。

//设置弹性,以火狐为例

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;"> -moz-box</span>;
}
Copy after login

1.box-orient 属性

box-orient 主要实现盒子内部元素的流动方向。

//设置垂直流动

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    -webkit-box-orient</span>:<span style="color: #0000ff;"> vertical</span>;
}
Copy after login

属性值

说明

horizontal

伸缩项目从左到右水平排列

vertical

伸缩项目从上到下垂直排列

inline-axis

伸缩项目沿着内联轴排列显示

block-axis

伸缩项目沿着块轴排练显示

2.box-direction

box-direction 属性主要是设置伸缩容器中的流动顺序。

//设置逆序

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    -moz-box-direction</span>:<span style="color: #0000ff;"> reverse</span>;
}<span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; background-color: #ffffff;"> </span>
Copy after login

属性值

说明

normal 

默认值,正常顺序

reverse 

逆序

3.box-pack

box-pack 属性用于伸缩项目的分布方式。

//分布方式已结束位置靠齐

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    -moz-box-pack</span>:<span style="color: #0000ff;"> end</span>;
}<span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; background-color: #ffffff;"> </span>
Copy after login

属性值

说明

start

伸缩项目以起始点靠齐

end

伸缩项目以结束点靠齐

center

伸缩项目以中心点靠齐

justify

伸缩项目平局分布,-webkit-支持,-moz-不支持

4.box-align

box-align 属性用来处理伸缩容器的额外空间。//居中对齐,清理上下额外空间

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    -moz-box-align</span>:<span style="color: #0000ff;"> center</span>;
}
Copy after login

属性值

说明

start

伸缩项目以顶部为基准,清理下部额外空间

end

伸缩项目以底部为基准,清理上部额外空间

center

伸缩项目以中部为基准,平均清理上下部额外空间

baseline

伸缩项目以基线为基准,清理额外的空间

stretch

伸缩项目填充整个容器,默认

5.box-flex

box-flex 属性可以使用浮点数分配伸缩项目的比例//设置每个伸缩项目占用的比例

<span style="color: #800000;">p:nth-child(1) </span>{<span style="color: #ff0000;">
    -moz-box-flex</span>:<span style="color: #0000ff;"> 1</span>;
}<span style="color: #800000;">

p:nth-child(2) </span>{<span style="color: #ff0000;">
    -moz-box-flex</span>:<span style="color: #0000ff;"> 2.5</span>;
}<span style="color: #800000;">

p:nth-child(3) </span>{<span style="color: #ff0000;">
    -moz-box-flex</span>:<span style="color: #0000ff;"> 1</span>;
}
Copy after login

6.box-ordinal-group

box-ordinal-group 属性可以设置伸缩项目的显示位置。

//将第一个位置的元素,跳转到第三个位置

<span style="color: #800000;">p:nth-child(1) </span>{<span style="color: #ff0000;">
    -moz-box-ordinal-group</span>:<span style="color: #0000ff;"> 3</span>;
}
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
4 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 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.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

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

See all articles