[英]How to set div in center vertically and horizontally where height is not known?
[英]How to vertically and horizontally center a div of unknown height
我尝试使用flexbox方法,表格方法和其他一些方法垂直居中一个未知高度的div,但我的div没有正确居中。 我希望居中div的宽度为窗口宽度的50%或最小宽度为200px。
.content { background-color: violet; min-width: 200px; width: 50%; margin-left: auto; margin-right: auto; box-shadow: 0px 1px 7px 1px rgba(0, 0, 0, 1); } .outer-container { display: table; width: 100%; background-color: violet; } .container { display: table-cell; text-align: center; vertical-align: middle; }
<body> <div class="outer-container"> <div class="container"> <div class="content"> <div class="title-class"> Hello there </div> </div> </div> </div> </body>
使用flexbox,这里是您需要的所有代码:
HTML
<div class="container">
<div class="content">
<div class="title-class">Hello there</div>
</div>
</div>
CSS
html, body { height: 100%; }
.container {
display: flex;
justify-content: center;
align-items: center;
background-color: violet;
height: 100%;
}
.content {
background-color: violet;
width: 50%;
text-align: center;
box-shadow: 0px 1px 7px 1px rgba(0, 0, 0, 1);
}
作为替代方案,这是表方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.