css-flex布局

本文深入讲解Flex布局的核心概念,包括主轴与交叉轴控制、排列方向、空间占比调整及元素缩放,同时介绍如何实现元素的对齐、折行与嵌套容器的兼容处理。

div竖排 display:flex

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<div class="flex">
			<div class="flex1">Flex1</div>
			<div class="flex2">Flex2</div>
			<div class="flex3">Flex3</div>
		</div>

		<style type="text/css">
			.flex {
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

			.flex1 {
				background-color: limegreen;
			}

			.flex2 {
				background-color: greenyellow;
			}

			.flex3 {
				background-color: khaki;
			}
		</style>
	</head>
	<body>
	</body>
</html>

对齐方式: x轴对齐

  1. 所有内容居中对齐: justify-content:center

在这里插入图片描述

.flex {
				justify-content: center;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}
  1. 所有内容居右: justify-content: flex-end

在这里插入图片描述

.flex {
				justify-content: flex-end;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}
  1. 两端分散对齐: space-between

在这里插入图片描述

.flex {
				justify-content: space-between;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}
  1. 平分空间:space-evenly

在这里插入图片描述

.flex {
				justify-content: space-evenly;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}
  1. 平分空间: space-around (优先两边)
.flex {
				justify-content: space-around;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

对齐方式:y轴对齐

  1. 默认stretch 与justify-content:flex-start 或者不添加一致

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<div class="flex">
			<div class="flex1">Flex1</div>
			<div class="flex2">Flex2</div>
			<div class="flex3">Flex3</div>
		</div>

		<style type="text/css">
			.flex {
				align-items: stretch;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

			.flex1 {
				background-color: limegreen;
			}

			.flex2 {
				background-color: greenyellow;
			}

			.flex3 {
				background-color: khaki;
			}
		</style>
	</head>
	<body>
	</body>
</html>

  1. 左上角对齐,不占满: align-items: flex-start;

在这里插入图片描述

.flex {
				align-items: flex-start;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}
  1. 靠左居中: align-items: center;

在这里插入图片描述

  1. 靠左居下:align-items: flex-end;

在这里插入图片描述

  1. 基线对齐:解决鼠标经过后变大但是左右位置不变大:align-items: baseline;

在这里插入图片描述

.flex2 {
				font-size: 24px;
				background-color: greenyellow;
			}
  1. 比如使用 align-items: flex-start;

在这里插入图片描述

  1. 使用 align-items: flex-end;

在这里插入图片描述

子元素交叉对齐: align-self

  1. 前面元素考上对齐,最后一个靠下对齐:

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<div class="flex">
			<div class="flex1">Flex1</div>
			<div class="flex2">Flex2</div>
			<div class="flex3">Flex3</div>
		</div>

		<style type="text/css">
			.flex {
				align-items: flex-start;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

			.flex1 {
				background-color: limegreen;
			}

			.flex2 {
				/* font-size: 24px; */
				background-color: greenyellow;
			}

			.flex3 {
				background-color: khaki;
				align-self: flex-end;
			}
		</style>
	</head>
	<body>
	</body>
</html>

  1. 前面元素按照左对齐,最后一个右对齐:

在这里插入图片描述
去掉align-self, 使用margin

.flex3 {
				background-color: khaki;
				/* align-self: flex-end; */
				margin-left: auto;
			}

排列方式:

  1. 主轴和交叉轴一起使用,然后添加:flex-direction: column;

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<div class="flex">
			<div class="flex1">Flex1</div>
			<div class="flex2">Flex2</div>
			<div class="flex3">Flex3</div>
		</div>

		<style type="text/css">
			.flex {
				justify-content: space-between;
				align-items: flex-start;
				flex-direction: column;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

			.flex1 {
				background-color: limegreen;
			}

			.flex2 {
				background-color: greenyellow;
			}

			.flex3 {
				background-color: khaki;
			}
		</style>
	</head>
	<body>
	</body>
</html>

  1. 水平居中对齐

在这里插入图片描述

.flex {
			    justify-content: space-between;   // 去掉后则居中吸顶居中
				align-items: center;
				flex-direction: column;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}
  1. 垂直居中对齐:

在这里插入图片描述

	.flex {
				justify-content: center;
				align-items: center; // 注释后将变为以下图片
				flex-direction: column;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

在这里插入图片描述

  1. 方向靠右:

在这里插入图片描述

.flex {
				/* justify-content: center; */
				/* align-items: center; */
				flex-direction: row-reverse;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

开启两个注释后结果:

在这里插入图片描述

  1. 倒叙排列:

在这里插入图片描述

.flex {
				justify-content: center;
				align-items: center;
				flex-direction: column-reverse;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

空间占比: 左右1,中间2

在这里插入图片描述

	.flex .flex3{
				flex:1
			}
			.flex2{
				flex:2
			}

flex basis: 主轴尺寸

  1. 每个div宽度一致:

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<div class="flex">
			<div class="flex1">Flex1</div>
			<div class="flex2">Flex2</div>
			<div class="flex3">Flex3</div>
		</div>

		<style type="text/css">
			
			.flex >* {
				flex-basis: 200px;
			}
			
			.flex {
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

			.flex1 {
				background-color: limegreen;
			}

			.flex2 {
				background-color: greenyellow;
			}

			.flex3 {
				background-color: khaki;
			}
		</style>
	</head>
	<body>
	</body>
</html>

这个时候添加width不在管用,优先级高于width,默认是auto,这个时候我们可以使用min-width来设置

.flex > * {
  flex-basis: 200px;
  min-width: 250px;
}

在这里插入图片描述

高度依旧如此:

在这里插入图片描述

            .flex >* {
				flex-basis: 30px;

			}
			
			.flex {
				flex-direction: column;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

缩放:flex-grow

设置两边自动大小,中间auto:

在这里插入图片描述

<style type="text/css">
			
			.flex {
				flex-basis: auto;
				display: flex;
				height: 100px;
				background-color: aliceblue;
			}

			.flex1 {
				background-color: limegreen;
				/* flex-grow: 1; */
			}

			.flex2 {
				flex-grow: 1;
				background-color: greenyellow;
			}

			.flex3 {
				/* flex-grow: 1; */
				background-color: khaki;
			}
		</style>

将1和3的注释起开后:三者各占据1/3

在这里插入图片描述

收缩限制:flex-shrink

flex2随着flex-basis的变大,他的收缩也会变大,因为是flex-shrink = 2

在这里插入图片描述


	.flex1 ,.flex3 {
				flex-basis: 600px;
				flex-shrink: 1;
			}
			.flex2 {
				flex-basis: 600px;
				flex-shrink: 2;
			}
// 添加上面一个演示代码即可

flex属性:

  • 默认值:0 1 auto, 不增长但收缩,收缩比例:1,flex-basis为auto: 取用户自定义宽度或内容宽度。

  • flex值如下:

  • flex: 1 等同:flex:1 1 0,即自动缩放比例1,flex-basis为0.

  • auto 等同 flex: 1 1 auto

  • 指定两个数字: 第一个为flex-grow, 第二个若是数字则为: flex-shrink, 如果是宽度则为flex-basis.

  • 指定三个值分别为:flex-grow, flex-shrink ,flex-basis

折行

如果元素有固定宽度,且超过容器宽度,若不可以收缩可以使用flex-wrap让其折行,使得每个元素都不超过容器的高度,但无法使用grid的单独控制行、列占比,亦不能自由定位元素到特定的位置

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<div class="flex">
			<div class="flex1">Flex1</div>
			<div class="flex2">Flex2</div>
			<div class="flex3">Flex3</div>
			<div class="flex4">Flex4</div>
			<div class="flex5">Flex5</div>

		</div>

		<style type="text/css">
			.flex {
				height: 100px;
				display: flex;
				flex-wrap: wrap;
			}
			.flex>* {
				flex-shrink: 0;
				flex-basis: 300px;

			}
			.flex1 {
				background-color: limegreen;
			}

			.flex2 {
				background-color: greenyellow;
			}

			.flex3 {
				background-color: khaki;
			}

			.flex4 {
				background-color: blueviolet;
			}

			.flex5 {
				background-color: cyan;
			}
		</style>
	</head>
	<body>
	</body>
</html>

这里我们依旧可以添加align-content 与justify-content 来控制主轴与交叉轴位置(两者属性基本一致)

比如我们设置中间空格:

在这里插入图片描述

其他属性align-content依旧 可以使用

.flex {
				align-content: space-between;
				justify-content: ;
				height: 100px;
				display: flex;
				flex-wrap: wrap;
			}

嵌套flex容器兼容问题

在以上代码基础上flex4中增加p标签出现以下问题

<div class="flex4">Flex4
				<p>这是个很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长文本</p>
</div>

在这里插入图片描述

解决办法:
原因是min-widht:auto, 在flex4中添加min-widht:0

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<div class="flex">
			<div class="flex1">Flex1</div>
			<div class="flex2">Flex2</div>
			<div class="flex3">Flex3</div>
			<div class="flex4">Flex4
				<p>这是个很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长文本</p>
			</div>
			<div class="flex5">Flex5</div>

		</div>

		<style type="text/css">
			.flex {
				align-content: space-between;
				justify-content: ;
				height: 100px;
				display: flex;
				flex-wrap: wrap;
			}
			.flex>* {
				flex: 1;
			}
			.flex1 {
				background-color: limegreen;
			}

			.flex2 {
				background-color: greenyellow;
			}

			.flex3 {
				background-color: khaki;
			}

			.flex4 {
				display: flex;
				flex: 1;
				min-width: 0;
				background-color: blueviolet;
			}
			.flex4 > p{
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.flex5 {
				background-color: cyan;
			}
		</style>
	</head>
	<body>
	</body>
</html>

总结

  • 开启flex布局使用display: flex属性
  • flex有主轴和交叉轴,分别使用justify-content和align-items控制
  • 支持按行和列进行排列,使用flex-direction,另支持row-reverse和column-reverse反向排列
  • flex属性:flex-grow、flex-shrink、flex-basis调整空间占比和缩放
  • flex-wrap设置折行
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值