search
HomeWeb Front-endHTML Tutorialflex布局的使用,纪念第一次开发手机网站_html/css_WEB-ITnose

一直专注于PC网站的开发,不曾接触手机网站,于今日机缘巧合也是公司业务需要,并在之前学习过flex的布局,于是一并实践。碰到的问题还是很多的,主要是谈谈flex布局。

 

flex布局是css3里的内容,一种新的布局方式,也称之为 弹性布局,主要是为了取代 inline-bolck  和float 为总布局。当然,这两种布局还是有各自的优势的,毕竟存在就有他的理由。

 

历史进程是  box-->flexbox --> flex

 

由于是在谷歌调试,所以很理所当然的以为手机上的浏览器都是支持html5+css3的于是就有一段版本

 1 display: flex; 2 justify-content: space-between; 3 align-items:center; 

等等

在http://www.responsinator.com/ 等测试网站表现得挺满意的

 

拿到真机上测,无论苹果还是安卓布局全乱了。后来才知道在苹果上运行需要加前缀,因为浏览器都是-webkit-内核的,主要是因为还没实现标准化,都是私有属性,于是有了下面的写法

display: -webkit-flex; display: flex;-webkit-justify-content:space-between; justify-content: space-between; -webkit-align-items:center;align-items:center;

嗯,苹果上表现得相当不错,我用的4,除了屏幕小了点其他还是可以接受的。

拿到安卓上,呵呵,基本上公司的安卓机全军覆没,布局没有一点改变。测试机器一般都是近些时候的,最久的话也是1-2年前的机器。后来才知道原来是根本不是识别flex布局,只能用box(也就是很早之前的语法)来解决,就得到了如下的写法

 1 .display_flex(){ 2     display: -webkit-box; 3     display: -webkit-flex; 4     display: flex; 5 } 6 .justify(){ 7      -webkit-justify-content: space-between; 8     -webkit-box-pack:justify; 9     justify-content: space-between;   10 }11 .justify(@ju)12 {13     -webkit-justify-content: @ju;14     -webkit-box-pack:@ju;15     justify-content: @ju;   16 }17 18 .align(@align)19 {20     -webkit-box-align:@align;21     -webkit-align-items: @align;22     align-items: @align;    23 }24 .flex-flow-column(){25     -webkit-box-orient:block-axis;26     -webkit-flex-flow: column nowrap;  27     flex-flow: column nowrap;  28 }29 .flex(@f){30     -webkit-box-flex:@f;31     -webkit-flex: @f;   32     flex: @f;33 }

注:这是less的写法(我学的不是很好)

不过中间有些遗憾新的flex里有一种叫做“超出自动换行”的功能,box里也有类似的属性,但是所有的浏览器都不支持,所以只能选择其他的布局(如前面提到的float)来代替。

 

总结:

  flex的布局基本都是考虑内核,PC上不是很推荐,因为国内大都要兼容IE7-8,可以考虑在移动端使用(毕竟只有安卓和苹果)。总而言之这种布局还是挺不错的,有学习的必要,像他们说的html5+css3必是未来的主流方向。

 

如有更好的解决方案望请不吝赐教,谢谢。

 

引用的资料:

http://www.w3cplus.com/css3/using-flexbox.html  (旧的box)

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html (新的flex)

 

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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

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,

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.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

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

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 <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

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

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

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

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

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

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

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools