DIV+CSS实现居中


总结一下常用的居中方式,主要记录常用的上下左右居中方式和个别上下居中方式及兼容性

对于固定宽高元素

固定宽高 + 设置负margin值(相较于不定宽的transform: translate(-50%))
*需要固定定位 fixed 或者 absolute

<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>
	<div class="box">
		<div class="content"></div>
	</div>

</body>
<style type="text/css">
	.box {
		width: 500px;
		height: 300px;
		position: fixed;
		left: 50%;
		top: 50%;
		margin-left: -250px;
		margin-top: -150px;
		border: 1px solid red;
	}
	.content {
		width: 100px;
		height: 100px;
		position: absolute;
		left: 50%;
		top: 50%;
		margin-left: -50px;
		margin-top: -50px;
		border: 1px solid blue;
	}
</style>
</html>

效果图:在这里插入图片描述

固定宽高 + 绝对定位
*尝试去掉 left + right || top + bottom试试
*固定定位 absolute fixed ,如果没有固定定位,有效果吗

<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>
	<div class="box">
		<div class="content">我是需要居中的盒子中的内容</div>
	</div>

</body>
<style type="text/css">
	.box {
		width: 500px;
		height: 300px;
		position: fixed;
		right: 0;
		left: 0;
		margin: auto;
		border: 1px solid red;
	}
	.content {
		width: 200px;
		height: 100px;
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		margin: auto;
		border: 1px solid blue;
	}
</style>
</html>

效果图:在这里插入图片描述

对于不定宽高元素

transform: translate(-50%, -50%) 居中元素可以不用设定宽高
*需要固定定位 fixed 或者 absolute
Internet Explorer 10、Firefox、Opera 支持 transform 属性。
Internet Explorer 9 支持替代的 -ms-transform 属性(仅适用于 2D 转换)。
Safari 和 Chrome 支持替代的 -webkit-transform 属性(3D 和 2D 转换)。
Opera 只支持 2D 转换。

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>
	<div class="box">
		<div class="content"></div>
	</div>

</body>
<style type="text/css">
	.box {
		width: 500px;
		height: 300px;
		position: fixed;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		border: 1px solid red;
	}
	.content {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		border: 1px solid blue;
	}
</style>
</html>

效果图:
在这里插入图片描述

display: flex;
justify-content: center;
align-items: center;
*在父元素上设置以上布局即可

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>
	<div class="box">
		我也居中了
		<div class="content">我是需要居中的内容</div>
	</div>

</body>
<style type="text/css">
	.box {
		width: 500px;
		height: 300px;
		display: flex;
		justify-content: center;
		align-items: center;
		border: 1px solid red;
	}
	.content {
		border: 1px solid blue;
	}
</style>
</html>

效果图:在这里插入图片描述

table-cell + vertical-align
*vertical-align 作用于内联元素以及 display: table-cell 的元素,浮动和绝对定位会让元素块状化,所以在浮动和绝对定位中使用 vertical-align 是不起作用的

<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>
	<div class="box">
		我也居中了
		<div class="content">我是需要居中的内容</div>
	</div>

</body>
<style type="text/css">
	.box {
		width: 500px;
		height: 300px;
		display: table-cell;
		vertical-align: middle;
		text-align: center;
		border: 1px solid red;
	}
	.content {
		border: 1px solid blue;
	}
</style>
</html>

效果图:在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值