We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<div class="parent"> <div class="child"></div> </div>
第一种
div.parent { position: relative; } div.child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
第二种
div.parent { position: relative; } div.child { width: 50px; height: 10px; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; }
第三种:子元素定宽高
div.parent { position: relative; } div.child { width: 50px; height: 10px; position: absolute; top: 50%; left: 50%; margin-left: -25px; margin-top: -5px; }
div.parent { display: flex; justify-content: center; align-items: center; }
div.parent{ display:flex; } div.child{ margin:auto; }
div.parent { display: grid; } div.child { justify-self: center; align-self: center; }
第一种:
div.parent { font-size: 0; text-align: center; height: 500px; line-height: 500px; } div.child{ width: 100px; height: 100px; display: inline-block; }
第二种:
div.parent { font-size: 0; text-align: center; &::before { content: ""; display: inline-block; width: 0; height: 100%; vertical-align: middle; } } div.child{ display: inline-block; vertical-align: middle; }
div.parent { display: table-cell; text-align: center; vertical-align: middle; } div.child { display: table-cell }
The text was updated successfully, but these errors were encountered:
sihai00
No branches or pull requests
让一个 div 水平垂直居中
绝对定位
第一种
第二种
第三种:子元素定宽高
弹性布局
第一种
第二种
网格布局
基线对齐
第一种:
第二种:
表格对齐
The text was updated successfully, but these errors were encountered: