【CSS】实现水平垂直居中的几种方法

本文详细介绍了如何通过text-align、width:fit-content、定位、transform、margin以及flex和grid布局,实现行内元素和块级元素的水平垂直居中。掌握这些技巧,轻松应对网页布局挑战。

关于实现 水平垂直居中 的一些方法


行内元素



在这里插入图片描述

<body>
	<style>
		.box {
      		width: 300px;
      		height: 200px;
      		background: #F1F1F1;
    	}
    	.box1 {
      		background: #ccc;
    	}
	</style>

	<div class="box">
		<span class="box1">content</span>
	</div>
</body>


1. text-align

.box {
	text-align:center;		/* 父元素设置 text-align 实现水平居中*/
}
.box1 {
	line-height: 200px;		/* 设置行高与父元素宽度相同,只能针对单行文本垂直居中 */
}

2. width: fit-content

.box {
	width:fit-content;		/* 这种方式实际上是使父元素的宽度适应子元素 */
	margin: auto;			/* 使父元素相对窗口居中 */
}
.box1 {
	line-height: 200px;
}



块级元素



在这里插入图片描述

<body>
	<style>
		.box {
      		width: 800px;
      		height: 500px;
      		background: #F1F1F1;
    	}
    	.box1{
      		width: 300px;
      		height: 200px;
      		background: #ccc;
    	}
	</style>

	<div class="box">
		<div class="box1"></div>
	</div>
</body>



1. 定位

.box {
	position: relative;
}
.box1 {
	position: absolute;
	left: 50%;
	top: 50%;
	margin-left: -150px;
	margin-top: -100px;
}

left: 50%; top: 50%; 将子元素的左上角定位到父元素中心点上,再使用margin-left: -150px; margin-top: -100px;将子元素偏移自身宽高的一半

在这里插入图片描述


2. 定位 + transform

.box {
	position: relative;
}
.box1 {
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
}

使用 transform: translate(-50%, -50%); 偏移子元素,不需要写具体数值。


3. 定位 + margin

.box {
	position: relative;
}
.box1 {
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	margin: auto;
}

absolute 这种脱离正常文档流的定位方式,会把元素宽高设置成内容的宽高。
设置left: 0; right: 0;使子元素宽度占满父元素
设置top: 0; bottom: 0;使子元素高度占满父元素
然后设置 margin: auto; 就可以实现水平垂直居中


4. flex

.box {
	display: flex;
	justify-content: center;
	align-items: center;
}

flex布局: 容器默认存在两根轴,横轴和纵轴
justify-content: center; 控制水平居中
align-items: center; 控制垂直居中


5. table-cell

.box {
	display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.box1 {
	display: inline-block;
}

table-cell布局:使元素表现的类似一个表格中的单元格td
text-align: center;水平居中
vertical-align: middle; 垂直居中
子元素改为 inline-block 元素


6. grid

.box {
	display: grid;
}
.box1 {
	align-self: center;
	justify-self: center;
}

grid布局:一种网格布局
justify-self: center; 水平居中
align-self: center;垂直居中

### CSS 实现元素水平垂直居中方法汇总 以下是几种常见的实现元素水平垂直居中方法: #### 方法一:使用 `display: table-cell` 和 `vertical-align: middle` 通过将父容器的 `display` 属性设置为 `table-cell`,并配合 `vertical-align: middle`,可以轻松实现子元素的垂直居中。同时,为了达到水平居中,可以在父容器上设置 `text-align: center`[^1]。 ```html <div style="border: 1px solid black; width: 300px; height: 300px; display: table-cell; text-align: center; vertical-align: middle;"> <div style="background-color: red; display: inline-block;">水平垂直居中</div> </div> ``` --- #### 方法二:使用绝对定位和 `transform` 这种方法利用了 `position: absolute` 的特性以及 `translate()` 函数来调整位置。通过设置 `top: 50%; left: 50%` 将元素中心点移动到父容器的中心,再用 `transform: translate(-50%, -50%)` 抵消自身的宽度和高度偏移[^4]。 ```css .parent { position: relative; width: 300px; height: 300px; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` --- #### 方法三:使用 Flexbox 布局 Flexbox 是现代布局中最常用的方法之一。只需将父容器的 `display` 设置为 `flex`,并通过 `align-items: center` 和 `justify-content: center` 来分别控制垂直水平方向上的居中[^2]。 ```css .parent { display: flex; align-items: center; justify-content: center; width: 300px; height: 300px; } ``` --- #### 方法四:使用 Grid 布局 Grid 布局提供了更强大的二维布局能力。可以通过设置 `place-items: center` 或者单独指定 `align-items` 和 `justify-items` 来完成居中操作[^2]。 ```css .parent { display: grid; place-items: center; width: 300px; height: 300px; } ``` --- #### 方法五:行内块元素与基线对齐 对于某些特定类型的行内块元素(如 `<input>`、`<img>` 等),可以直接通过设置其 `vertical-align` 属性为 `middle` 来实现与其他文本内容的水平对齐[^3]。 ```html <span>这是一个例子:</span><input type="text" style="vertical-align: middle;" /> ``` --- 以上列举了几种主流的 CSS 水平垂直居中方法,每种方法都有各自的适用场景和技术特点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值