Table of Contents
应用于 flex container 的属性
应用于 flex item 的属性
Home Web Front-end HTML Tutorial Flexbox 完全指南_html/css_WEB-ITnose

Flexbox 完全指南_html/css_WEB-ITnose

Jun 24, 2016 am 11:17 AM

Flexbox 完全指南

我不是这篇文章的原创作者,我只是好文章的搬运工。原文地址 A Complete Guide to Flexbox

应用于 flex container 的属性

display
该属性定义一个 flex container,根据不同取值定义为 inline 或 block 的 flex container。应用了该属性的元素为它的所有子元素创建了一个 flex context。

.container {  display: flex; /* or inline-flex */}
Copy after login

注意,CSS3 的多列布局对 flex 容器没有任何影响。

flex-direction

该属性建立主轴,规定了 flex container 中的 flex item 的排布方向。Flexbox 是一种单向布局概念,可以认为 flex item 都优先沿着水平行或竖直列布局。

.container {  flex-direction: row | row-reverse | column | column-reverse;}  
Copy after login
  • row (默认值): 在 ltr 上下文中为由左到右;在 rtl 上下文中为由右到左。
  • row-reverse : 在 ltr 上下文中为由右到左;在 rtl 上下文中为由左到右。
  • column : 与 row 类似,只不过是由上到下。
  • column-reverse : 与 row-reverse 类似,只不过是由下到上。
  • flex-wrap

    默认情况下,所有的 flex item 都将尽量保持在一个 line (行或者列,下同)之内。可以通过这个属性让 flex item 在需要的情况下换行或者换列。这里,新行或新列从哪里开始由 flex-direction 决定。

    .container{  flex-wrap: nowrap | wrap | wrap-reverse;}
    Copy after login
  • nowrap (默认值): 单 line。在 ltr 上下文中为由左到右;在 rtl 上下文中为由右到左。
  • wrap : 多 line。在 ltr 上下文中为由左到右;在 rtl 上下文中为由右到左。
  • wrap-reverse : 多 line。在 ltr 上下文中为由右到左;在 rtl 上下文中为由左到右。
  • flex-flow
    该属性为 flex-direction 和 flex-wrap 的简写属性,同时定义了 flex container 的主轴和交叉轴。默认值为 row nowrap。

    flex-flow: <‘flex-direction’> || <‘flex-wrap’>
    Copy after login

    justify-content

    该属性定义了沿着主轴的对齐方式。它被用来处理当所有 flex item 都已经放置完毕后的剩余空白空间。

    .container {  justify-content: flex-start | flex-end | center | space-between | space-around;}
    Copy after login
  • flex-start (默认值): 所有的 flex item 都向 line 的开始位置对齐。
  • flex-end : 所有的 flex item 都向 line 结束位置对齐。
  • center : 所有的 flex item 都沿着 line 的中间位置对齐。
  • space-between : 所有的 flex item 都均匀的沿着 line 分布,第一个 item 排在 line 的开始位置,最后一个 item 排在 line 的结束位置。
  • space-around : 所有的 flex item 都均匀的沿着 line 分布,每个 item 周围的空白空间相等。注意,看上去每个 item 周围的空白空间不一定完全相等,因为所有的 item 的两边都拥有大小完全相同的空白空间。
  • align-items

    该属性定义了在当前 line 中 flex item 沿着交叉轴布局的默认行为。可以认为它是针对交叉轴的 justify-content。

    .container {  align-items: flex-start | flex-end | center | baseline | stretch;}
    Copy after login
  • flex-start : 沿着交叉轴方向的 margin 边缘将会沿着交叉轴的开始位置对齐。
  • flex-end : 沿着交叉轴方向的 margin 边缘将会沿着交叉轴的结束位置对齐。
  • center : 所有的 flex item 将沿着交叉轴的中央对齐。
  • baseline : 所有的 flex item 将会沿着他们的 baseline 对齐。
  • stretch (默认值): 拉伸以填满容器(依然受 min-width/max-width 限制)。
  • align-content

    该属性用来在交叉轴还有空白空间的情况下控制 flex container 内的所有 line 的对齐方式。与 justify-content 控制 flex item 如何沿着主轴方向对齐的方式类似。
    注意: 单 line 情况下,该属性不生效。

    .container {  align-content: flex-start | flex-end | center | space-between | space-around | stretch;}
    Copy after login
  • flex-start : 所有的 line 都向 container 的开始位置对齐。
  • flex-end : 所有的 line 都向 container 的结束位置对齐。
  • center : 所有的 line 都沿着 container 的中央对齐。
  • space-between : 所有的 line 均匀分布,第一个 line 在 container 的开始位置,最后一个 line 在 container 的结束位置。
  • space-around : 所有的 line 均匀分布,每个 line 周围的空白空间相等。
  • stretch (默认值): 所有的 line 拉伸以填满剩余空间。
  • 应用于 flex item 的属性

    order

    在默认情况下,所有的 flex items 按照源代码中定义的顺序布局。但是, order 属性控制了 flex item 在 flex container 中显示的优先级。

    .item {  order: <integer>;}
    Copy after login

    flex-grow

    该属性赋予 flex item 生长(grow)的能力。它接受一个无单位的值,并将其作为比例值,表示在 flex container 中,这个 flex item 能够占有多大比例的可用空间。
    如果所有的 flex item 的 flex-grow 属性都设置为1,那么 container 的剩余空间将被均匀的分配给所有 flex item。如果当中有一个 item 的 flex-grow 属性设置为2,那么 这个 item 将占有2倍于其他 item 占有的可用空间。

    .item {  flex-grow: <number>; /* default 0 */}
    Copy after login

    负值是非法取值。

    flex-shrink
    该属性赋予 flex item 在必要的情况下收缩的能力。

    .item {  flex-shrink: <number>; /* default 1 */}
    Copy after login

    负值是非法取值。

    flex-basis
    该属性用于在分配剩余空间之前定义 flex item 的默认尺寸大小。它的取值可以为一个绝对长度值(比如 20%,5rem,等)或者是一个关键字。auto 关键字表示根据 flex item 自身的 width 和 height 属性取值确定。 content 关键字表示根据 flex item 的 content 来确定,但是目前并没有得到很好的支持。

    .item {  flex-basis: <length> | auto; /* default auto */}
    Copy after login

    如果该属性取值为0,那么在该 flex item 的 content 周围的额外空间将不被计算在内。如果取值为 auto,那么 flex item 的大小将根据 flex-grow 的取值来确定。可以参考这张图。

    flex
    该属性为 flex-grow, flex-shrink 和 flex-basis 的混合简写方式。 其中第二个和第三个参数(flex-shrink 和 flex-basis)为可选参数。该属性的默认值为 0 1 auto。

    .item {  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]}
    Copy after login

    align-self

    该属性可以为每个 flex item 指定对齐方式。我们可以通过该属性修改 flex item 默认的或者由 align-items 指定的对齐方式。它的取值说明参考 align-items 的取值说明。

    .item {  align-self: auto | flex-start | flex-end | center | baseline | stretch;} 
    Copy after login

    注意,float,clear 和 vertical-align 对 flex item 无任何影响。

    更多实例请参考原文例子。

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

    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 &lt;datalist&gt; 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 &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

    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 &lt;time&gt; 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 &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

    See all articles