search
HomeWeb Front-endHTML Tutorial第 29 章 CSS3 弹性伸缩布局[上]_html/css_WEB-ITnose

学习要点:

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

<div>    <p>        第一段内容...    </p>    <p>        第二段内容...    </p>    <p>        第三段内容...    </p></div>

//CSS 部分 

p {    width: 150px;    border: 1px solid gray;    margin: 5px;    padding: 5px;}div {    display: -moz-box;    display: -webkit-box;    display: box;}

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

 

二.旧版本

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

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

属性值

说明

box

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

inline-box

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

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

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

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

div {    display: -moz-box;}

1.box-orient 属性

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

//设置垂直流动

div {    -webkit-box-orient: vertical;}

属性值

说明

horizontal

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

vertical

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

inline-axis

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

block-axis

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

2.box-direction

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

//设置逆序

div {    -moz-box-direction: reverse;} 

属性值

说明

normal 

默认值,正常顺序

reverse 

逆序

3.box-pack

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

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

div {    -moz-box-pack: end;} 

属性值

说明

start

伸缩项目以起始点靠齐

end

伸缩项目以结束点靠齐

center

伸缩项目以中心点靠齐

justify

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

4.box-align

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

div {    -moz-box-align: center;}

属性值

说明

start

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

end

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

center

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

baseline

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

stretch

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

5.box-flex

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

p:nth-child(1) {    -moz-box-flex: 1;}p:nth-child(2) {    -moz-box-flex: 2.5;}p:nth-child(3) {    -moz-box-flex: 1;}

6.box-ordinal-group

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

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

p:nth-child(1) {    -moz-box-ordinal-group: 3;}

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

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

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

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.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor