javascript - css 控制 div 始终水平垂直居中,这个div大小还不同
本文介绍了javascript - css 控制 div 始终水平垂直居中,这个div大小还不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
css 控制加了一个统一类名的div,想让他水平垂直居中显示,但是这个div大小不一样,css就不能写固定,其他同学有什么好的思路没
解决方案
flex布局吧
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#main
{
width:220px;
height:300px;
border:1px solid black;
display:flex;
justify-content:center;
align-items:center;
}
#main div
{
}
</style>
</head>
<body>
<div id="main">
<div style="background-color:coral;">红色</div>
</div>
</body>
</html>
这篇关于javascript - css 控制 div 始终水平垂直居中,这个div大小还不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文