纯CSS设置元素垂直水平居中的几种方法

本文介绍了五种实现网页元素居中的方法,包括使用transform、margin、table-cell、flex布局等技术,适用于不同场景下的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分为定位的方法,和不定位的方法。

使用定位的方法:
  1. 定位+transform:
    父元素相对定位,子元素绝对定位,设置子元素的 top:50%; left:50%;transform:translate: (-50%,-50%),代码对应类名f1,box1。
  2. 定位+margin:
    父元素相对定位,子元素绝对定位,设置子元素的 top:50%; left:50%;margin-left:负的自身宽度的一半,margin-top:负的自身高度的一半。代码对应类名f1,box1。代码都写在了这一部分里,有标注。
  3. 定位+margin:
    父元素相对定位,子元素绝对定位。设置子元素的left,right,bottom,top都为0,margin:auto;代码对应类名f3,box3。
不用定位的方法:
  1. table-cell方法,赋予父元素单元格的属性:
    父元素display:table-cell; vertical-align: middle;设置子元素的margin:0 auto;代码对应类名 f2,box2。
  2. flex弹性盒子布局:
    设置父元素display: flex;
    justify-content: center;
    align-items: center;
    代码对应类名 f4,box4。
html代码:

外层div与内层div,span标签区分第几种,序号。

<body>
	<span>1</span>
	<div class="f1">
		<div class="box1"></div>
	</div>
	<span>2</span>
	<div class="f2">
		<div class="box2"></div>
	</div>
	<span>3</span>
	<div class="f3">
		<div class="box3"></div>
	</div>
	<span>4</span>
	<div class="f4">
		<div class="box4"></div>
	</div>
</body>
style内容:
.f1 {
			width: 300px;
			height: 200px;
			border: 1px solid red;
			position: relative;
		}

		.box1 {
			position: absolute;
			width: 100px;
			height: 50px;
			background-color: pink;
			/*父元素相对定位,
			子元素绝对定位,
			设置子元素的 top:50%; left:50%;
			transform:translate: (-50%,-50%)
			*/
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			/* 方法2:父元素相对定位,子元素绝对定位
			设置子元素:
			top:50%; 
			left:50%;
			margin-left: 负的宽度一半 
			margin-top: 负的高度一半。
			这两种类似,我写在了一个类里面。
			 */			
			/* margin-left: -50px;
			margin-top: -25px; */
		}


		.f2 {
			width: 300px;
			height: 200px;
			background: rgb(241, 241, 43);
			border: 1px solid #555555;
			/* 方法3:父元素display:table-cell;vertical-align:middle;
			子元素:margin:0 auto;*/
			display: table-cell; /*转换为table单元格显示*/
			/*垂直居中*/
			vertical-align: middle;			
		}

		.box2 {
			width: 100px;
			height: 100px;
			background: pink;
			margin: 0 auto;
			/*水平居中*/
		}

		.f3 {
			width: 300px;
			height: 200px;
			border: 1px solid red;
			background: lightyellow;
			/* 方法4:
			父元素相对定位,子元素绝对定位,
			设置子元素left,right,bottom,top都为0,margin:auto;*/
			position: relative;
		}

		.box3 {
			position: absolute;
			width: 100px;
			height: 100px;
			background: pink;

			left: 0;
			top: 0;
			bottom: 0;
			right: 0;
			margin: auto;
		}

		.f4 {
			width: 300px;
			height: 200px;
			border: 1px solid red;
			background: lightyellow;
			/*方法5:flex弹性盒子布局
			设置父元素display: flex;
			justify-content: center;
			align-items: center;*/
			display: flex;
			/*主轴方向居中,即水平居中*/
			justify-content: center;	
			/*侧轴方向居中,即水平居中*/		
			align-items: center;
		}

		.box4 {
			width: 100px;
			height: 100px;
			background: pink;
		}

效果截图
文章整理完了,欢迎测试代码或下方评论哦~

### 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
发出的红包

打赏作者

清颖~

您的鼓励让我们一起进步,加油!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值