非常教程

CSS参考手册

灵活的框布局 | Flexible Box Layout

justify-content

CSSjustify-content属性定义了浏览器如何分配顺着父容器主轴的弹性元素之间及其周围的空间。

/* Positional alignment */
justify-content: center;     /* Pack items around the center */
justify-content: start;      /* Pack items from the start */
justify-content: end;        /* Pack items from the end */
justify-content: flex-start; /* Pack flex items from the start */
justify-content: flex-end;   /* Pack flex items from the end */
justify-content: left;       /* Pack items from the left */
justify-content: right;      /* Pack items from the right */

/* Baseline alignment */
justify-content: baseline;
justify-content: first baseline;
justify-content: last baseline;

/* Distributed alignment */
justify-content: space-between; /* Distribute items evenly
                                   The first item is flush with the start,
                                   the last is flush with the end */
justify-content: space-around;  /* Distribute items evenly
                                   Items have a half-size space
                                   on either end */
justify-content: space-evenly;  /* Distribute items evenly
                                   Items have equal space around them */
justify-content: stretch;       /* Distribute items evenly
                                   Stretch 'auto'-sized items to fit
                                   the container */

/* Overflow alignment */
justify-content: safe center;
justify-content: unsafe center;

/* Global values */
justify-content: inherit;
justify-content: initial;
justify-content: unset;

当 length 属性和自动外边距属性(margin: auto)生效之后,对齐已经完成了。也就是说,如果存在至少一个弹性元素,而且这个元素的flex-grow属性不等于 0,那么对齐方式不会生效,就像没有多余空间的情况。 

初始值

flex-start

适用于

柔性容器

遗传

没有

媒体

视觉

计算值

作为指定

动画类型

离散的

规范的顺序

形式语法定义的独特的非模糊顺序

语法

start从行首开始排列。每行第一个元素与行首对齐,同时所有后续的元素与前一个对齐。

flex-start从行首开始排列。每行第一个弹性元素与行首对齐,同时所有后续的弹性元素与前一个对齐。

flex-end从行尾开始排列。每行最后一个弹性元素与行尾对齐,其他元素将与后一个对齐。

这仅适用于柔性布局项目。对于不是弹性容器的子项的项目,这个值被视为像startflex-end根据柔性容器的主端侧,物品朝向对齐容器的边缘彼此齐平地包装。

这仅适用于柔性布局项目。对于不是弹性容器的子项的项目,这个值被视为像end

center这些物品沿着主轴朝向对齐容器的中心彼此齐平地包装。

left物品朝向对齐容器的左边缘彼此齐平。如果属性的轴不与内联轴平行,则该值的行为如同start

right物品在适当的轴线上朝向对齐容器的右边缘彼此齐平。如果属性的轴不与内联轴平行,则该值的行为如同start

baseline

first baseline

last baseline指定参与首次或最后一次基线对齐:将框的第一个或最后一个基准集的对齐基线与基准线共享组中所有框的共享第一个或最后一个基准线集中的相应基线对齐。

回退定位first baselinestart,一只为last baselineend

space-between物品沿主轴均匀分布在对齐容器内。每一对相邻物品之间的间隔是相同的。第一个项目与主起始边缘齐平,最后一个项目与主端边缘齐平。

space-around物品沿主轴均匀分布在对齐容器内。每一对相邻物品之间的间隔是相同的。第一个和最后一个项目之前的空白空间等于每一对相邻项目之间的空间的一半。

space-evenly物品沿主轴均匀分布在对齐容器内。每一对相邻的项目,主起始边缘和第一项目,以及主要边缘和最后项目之间的间隔都完全相同。

stretch如果物品的组合尺寸小于对齐容器的auto尺寸,则任何尺寸的物品的尺寸均等地增加(不成比例),同时仍然考虑由max-height/ max-width(或等同的功能)施加的约束,使得组合的尺寸沿主轴正好填充对齐容器。

safe如果项目的大小溢出对齐容器,则对齐项目就像对齐模式一样对齐start

unsafe无论物品和对齐容器的相对大小如何,给定的对齐值都是可以接受的。

形式语法

flex-start | flex-end | center | space-between | space-around | space-evenly

CSS内容

#container {
  display: flex;
  justify-content: space-between; /* Can be changed in the live sample */
}

#container > div {
  width: 100px;
  height: 100px;
  background: linear-gradient(-45deg, #788cff, #b4c8ff);
}

结果

规范

Specification

Status

Comment

CSS Box Alignment Module Level 3The definition of 'justify-content' in that specification.

Working Draft

Adds the first | last ? baseline, self-start, self-end, start, end, left, right, unsafe | safe values.

CSS Flexible Box Layout ModuleThe definition of 'justify-content' in that specification.

Candidate Recommendation

Initial definition

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

21.0 -webkit 29.03

(Yes)-webkit (Yes)

18.0 (18.0)1 20.0 (20.0)2

11

12.10

9

space-evenly

No support

No support

52.0 (52.0)

No support

?

?

start, end

No support4

No support

(Yes)

No support

No support4

?

left, right

No support4

No support

52.0 (52.0)5

No support

No support4

?

baseline

57.0

No support

(Yes)

No support

44.0

?

first baseline, last baseline

No support

No support

52.0 (52.0)

No support

No support

?

stretch

57.0

No support

52.0 (52.0)

No support

44.0

?

Feature

Firefox Mobile (Gecko)

Android

Android Webview

Edge

IE Phone

Opera Mobile

Safari Mobile

Chrome for Android

Basic support

?

?

(Yes)3

(Yes)-webkit (Yes)

No support

12.10

?

(Yes)3

space-evenly

52.0 (52.0)

No support

No support

No support

No support

No support

No support

No support

start, end

No support4

No support

(Yes)

No support

No support4

?

left, right

No support4

No support

52.0 (52.0)5

No support

?4

?

baseline

57.0

No support

(Yes)

No support

44.0

?

first baseline, last baseline

No support

No support

52.0 (52.0)

No support

No support

?

stretch

57.0

No support

52.0 (52.0)

No support

44.0

?

CSS

层叠样式表( Cascading Style Sheets )是一种用来表现 HTML 或 XML 等文件样式的计算机语言。CSS 不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。

CSS目录

1.动画和转换 | Animations & Transitions
2.背景和边框 | Backgrounds & Borders
3.基本框模型 | Basic Box Model
4.基本用户界面 | Basic User Interface
5.框对齐 | Box Alignment
6. 级联和继承 | Cascading & Inheritance
7.颜色 | Color
8. 合成与混合 | Compositing & Blending
9.条件和规则 | Conditional Rules
10.计数器样式 | Counter Styles
11.设备适配 | Device Adaptation
12.扩展 | Extensions
13.滤镜效果 | Filter Effects
14.灵活的框布局 | Flexible Box Layout
15.字体 | Fonts
16.片段模块 | Fragmentation
17.全屏 API | Fullscreen API
18. 生成内容 | Generated Content
19.网格布局 | Grid Layout
20.图像值 | Image Values
21.初始线格局 | Inline Layout
22.列表和计数器 | Lists & Counters
23.逻辑属性 | Logical Properties
24.面具 | Masking
25.媒体查询 | Media Queries
26.杂项 | Miscellaneous
27.Miscellaneous Level 1
28.Miscellaneous Level 2
29.运动路径 | Motion Path
30. 多列布局 | Multi-column Layout
31.命名空间 | Namespaces
32.项目模型 | Object Model View
33.网页媒体 | Paged Media
34.定位布局 | Positioned Layout
35.伪元素 | Pseudo-
36.节奏大小 | Rhythmic Sizing
37. Ruby布局 | Ruby Layout
38.可缩放矢量图形 | Scalable Vector Graphics
39.滚动快照 | Scroll Snap
40.选择 | Selectors
41.形状 | Shapes
42.文本 | Text
43.文字装饰 | Text Decoration
44.变换 | Transforms
45.值和单位 | Values & Units
46.变量 | Variables
47.写入模型 | Writing Modes
48.CSS 教程
49.CSS 创建
50.CSS Id 和 Class选择器
51.CSS 简介
52.CSS 盒子模型
53.CSS Table(表格)
54.CSS 列表样式(ul)
55.CSS 链接(link)
56.CSS Fonts(字体)
57.CSS Text(文本)
58.CSS Backgrounds(背景)
59.CSS Display(显示) 与 Visibility(可见性)
60.CSS 尺寸 (Dimension)
61.CSS 分组和嵌套
62.CSS 轮廓(outline)属性
63.CSS Border(边框)
64.CSS 图像透明/不透明
65.CSS 导航栏
66.CSS 伪元素
67.CSS 伪类
68.CSS Float(浮动)
69.CSS Position(定位)
70.CSS 总结
71.CSS 属性选择器
72.CSS 媒体类型
73.CSS 图像拼合技术
74.CSS 实例
75.CSS 组合选择符
76.响应式 Web 设计 – 框架
77.响应式 Web 设计 – 视频(Video)
78.CSS 提示工具(Tooltip)
79.CSS 布局 Overflow
80.CSS 计数器
81.CSS 表单