CSS3 Flex实现元素的水平居中和垂直居中

编程入门 行业动态 更新时间:2024-10-14 08:27:42

2019独角兽企业重金招聘Python工程师标准>>>

一.水平居中

1.单个元素水平居中

.box {
	display: flex;
	justify-content: center;   /*水平居中*/
}

2.多个h1元素水平居中

<!--html代码-->

<div class="box">
	<h1>flex弹性布局justify-content属性实现元素水平居中</h1>
	<h1>flex弹性布局justify-content属性实现元素水平居中</h1>
	<h1>flex弹性布局justify-content属性实现元素水平居中</h1>
</div>

css

.box {
	display: flex;
	justify-content: center;
	width: 100%;
	background: #0099cc
}

h1 {
	font-size: 1rem;
	padding: 1rem;
	border: 1px dashed #FFF;
	color: #FFF;
	font-weight: normal;
}

效果图

 

二.垂直居中

1.单个h1标签垂直居中

.box {
	display: flex;
	align-items:center;   /*垂直居中*/
}

2.多个h1标签并排垂直居中

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<style>
	.box {
		display: flex;
		width: 980px;
		height: 30rem;
		align-items: center;
		background: #6161d1
	}

	h1 {
		font-size: 1rem;
		padding: 1rem;
		border: 1px dashed#FFF;
		color: #FFF
	}

</style>

<body>
	<div class="box">
		<h1>flex弹性布局justify-content属性实现元素垂直居中</h1>
		<h1>flex弹性布局justify-content属性实现元素垂直居中</h1>
		<h1>flex弹性布局justify-content属性实现元素垂直居中</h1>
	</div>
</body>

 

 

 

总结:

常用代码:

display: flex;
align-items: center;      /*垂直居中*/
justify-content:center;   /*水平居中*/

 

转载于:https://my.oschina/u/3549294/blog/1604484

更多推荐

CSS3 Flex实现元素的水平居中和垂直居中

本文发布于:2023-06-13 12:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   水平   Flex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!