使用flex布局和line-height实现字体的水平居中显示

在前端开发中除了实现div居中之外,还经常需要让字体水平垂直居中,div实现水平垂直居中可以参考使用flex布局和translate实现div水平和垂直居中,文字的水平和垂直居中代码实现如下

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
		.contain1{
			width: 100%;
			height: 100%;
		}
		.contain1 > div{
			position: absolute;
			width: 50%;
			height: 50%;
		}
		
		.div2{
			right: 0;
			background: rgba(149,64,36,0.6);
			/*开启flex布局*/
			display: flex;
			/*垂直居中*/
			align-items: center;
			/*水平居中*/
			justify-content: center;
		}
		.div3{
			top: 50%;
			background: cyan;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.div33{
			
			width: 200px;
			background: rgba(36,49,64,0.6);
			/*height+line-height控制字体垂直居中 
			 * text-align控制字体 水平居中*/
			height: 200px;
			line-height: 200px;
			text-align: center;
		}
		</style>
	</head>
	<body>
		<div id="contain1" class="contain1">
			<div id="div2" class="div2">
				我是居中字体
			</div>
			<div id="div3" class="div3">
				<div id="div33" class="div33">我是居中字体</div>
			</div>
		</div>
	</body>
</html>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值