CSS Flexbox(弹性盒子)


目录

    显示目录


1

2

3

4

5

6

7

8

自己尝试一下→

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: nowrap;
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}
</style>
</head>
<body>
<h1>Flexible Boxes</h1>

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>
  <div>5</div>
  <div>6</div>  
  <div>7</div>
  <div>8</div>
</div>

<p>Try to resize the browser window.</p>
<p>A container with "flex-wrap: nowrap;" will never wrap its items.</p>
<p><strong>Note:</strong> Flexbox is not supported in Internet Explorer 10 or earlier versions.</p>

</body>
</html>

CSS Flexbox 布局模块

在Flexbox布局模块之前,有四种布局模式:

Block

类似块,用于网页中的部分

Inline

内联,用于文本

Table

Table,用于二维表格数据

Positioned

已定位,用于元素的明确位置

灵活的盒子布局模块,可以更轻松地设计灵活的响应式布局结构,而无需使用浮动或定位。


浏览器支持

所有现代浏览器都支持 flexbox 属性。

29.0 11.0 22.0 10 48

弹性盒元素

要开始使用 Flexbox 模型,您需要首先定义一个 Flex 容器。

1

2

3

上面的元素代表一个包含三个 Flex 项目的 Flex 容器(蓝色区域)。

例子

一个包含三个 Flex 项目的 Flex 容器:

<div 
  class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

自己尝试一下→

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  margin: 10px;
  padding: 20px;
  font-size: 30px;
}
</style>
</head>
<body>

<h1>Create a Flex Container</h1>

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
</div>

<p>A Flexible Layout must have a parent element with the <em>display</em> property set to <em>flex</em>.</p>

<p>Direct child elements(s) of the flexible container automatically becomes flexible items.</p>

</body>
</html>


您将在接下来的章节中了解有关 Flex 容器和 Flex 项目的更多信息。