【前端】CSS水平居中的6种方法

文章目录

后文:【前端】CSS垂直居中的7种方法_karshey的博客-CSDN博客

左右两边间隔相等的居中

flex

  • display: flex;
  • justify-content: center;
html 复制代码
<div class='parent'>
	<div class="son">
	
	</div>
</div>
css 复制代码
.parent {
	display: flex;
	justify-content: center;
}
.son {
	background: pink;
	width: 100px;
	height: 100px;
}

绝对定位+margin:auto

  • 居中子元素
  • 子绝父相,子元素margin:auto

原理:

top + margin-top + border-top-width + padding-top + height + padding-bottom + border-bottom-width + margin-bottom + bottom = height

上述式子中,top和bottom为0,margin等于auto,此时浏览器为了满足这个等式会把上下的距离均匀分给margin,即达到我们想要的居中效果。横向也是一样的道理。

css 复制代码
.father {
	position: relative;
	width: 500px;
	height: 500px;
	background-color: blue;
}

.center {
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	margin: auto;
	width: 100px;
	height: 100px;
	background-color: red;
}
html 复制代码
<div class='father'>
	<div class="center">

	</div>
</div>

绝对定位+margin:负值

原理:对当前元素的position设置为absolute,并且相对于父元素定位。先设置left:50%;top:50%,使当前元素的左上角处于父元素的中心位置。之后再应用负margin特性使其中心位于父元素的中心。因此需要知道子元素的大小。挪动子元素大小的一半。

css 复制代码
.father {
	position: relative;
	width: 500px;
	height: 500px;
	background-color: blue;
}

.center {
	width: 200px;
	height: 100px;
	background-color: red;
	position: absolute;
	left: 50%;
	margin-left: -100px;
}
html 复制代码
<div class='father'>
	<div class="center">

	</div>
</div>

若想要垂直也居中,则需要center中添加:

css 复制代码
top: 50%;
margin-top: -50px;

利用负margin实现元素居中

定位+transform

  • 父元素:相对定位
  • 子元素:相对/绝对 定位 都可以
  • 子元素left:50%,左边界到父元素的中间
  • 子元素transform: translateX(-50%);,向左移动自己的一半,使得自己的中心对准父元素的中心
  • 不需要知道子元素宽度
css 复制代码
.father {
	position: relative;
	height: 500px;
	width: 500px;
	background-color: blue;
}

.center {
	position: relative;
	left: 50%;
	width: 200px;
	height: 200px;
	background-color: red;
	transform: translateX(-50%);
}
html 复制代码
<div class='father'>
	<div class="center">

	</div>
</div>

text-align: center;

指定元素文本的水平居中。是行内元素

  • text-align: center;
css 复制代码
.parent {
    text-align: center;
}
html 复制代码
<div class='parent'>
	<span>123</span>
</div>

margin: 0 auto;

  • margin: 0 auto;
  • 定宽 ,不定宽的话宽为width:100%
  • 块级元素
html 复制代码
<div class='box'>是块级元素居中,块级元素内的不居中,想让这行文字也居中要用text-align</div>
css 复制代码
.box {
	background: skyblue;
	width: 200px;
	height: 200px;
	margin: 0 auto;
}

思维导图

css 水平居中(8种方法)、垂直居中(8种方法) - 掘金 (juejin.cn)

绝对定位+margin auto垂直居中引发的思考 - 掘金 (juejin.cn)

使用transform使定位元素居中_transform居中_神膘护体小月半的博客-CSDN博客

相关推荐
天若有情67326 分钟前
前端 vs 后端:入行软件行业,我该如何选择?哪个更“简单”?
前端·后端·软件开发·职业·就业·选择
小画家~33 分钟前
第二十八:golang Time.time 时间格式返回定义结构体
java·前端·golang
Cobyte1 小时前
17. Vue3 业务组件库按需加载的实现原理
前端·javascript·vue.js
粥里有勺糖1 小时前
视野修炼-技术周刊第127期 | Valdi
前端·javascript·github
前端世界2 小时前
从零搭建 ASP.NET 单文件 Web 项目:一个能真用的 BookShop 管理页实战
服务器·前端·asp.net
码上成长2 小时前
Vue Router 3 升级 4:写法、坑点、兼容一次讲透
前端·javascript·vue.js
BBB努力学习程序设计2 小时前
响应式页面设计与实现:让网站适配所有设备的艺术
前端·html
IT从业者张某某2 小时前
less 工具 OpenHarmony PC适配实践
前端·microsoft·less
行走的陀螺仪3 小时前
vue3-封装权限按钮组件和自定义指令
前端·vue3·js·自定义指令·权限按钮
isyuah3 小时前
vite-plugin-openapi-ts CLI 使用指南
前端·vite