简体   繁体   English

在另一个 div 内垂直对齐 div 没有 flex

[英]Vertical align div inside another div without flex

How to vertically align div inside another div using property vertical-align:middle .如何使用属性vertical-align:middle在另一个 div 内垂直对齐 div。

Here is my code.这是我的代码。

 .hello { height: 100px; width: 100px; background-color: black; vertical-align: middle; display: inline-block; color: white; } .parent { height: 400px; width: 400px; border: 1px solid red; display: table-cell; }
 <div class ="parent "> <div class="hello"> hello </div> </div>

I referred and found giving parent table-cell property and child inline-block works but still not.我提到并发现给父表单元格属性和子行内块工作,但仍然没有。

Html html

Here you go.给你。

Code Snippet:代码片段:

 .hello { height: 100px; width: 100px; background-color: black; vertical-align: middle; display: inline-block; color: white; } .parent { height: 400px; width: 400px; border: 1px solid red; display: table-cell; vertical-align: middle; text-align: center; }
 <div class="parent "> <div class="hello"> hello </div> </div>

vertical-align works only for display: table-cell, in some browsers you should wrap parent with display: table Vertical-align 仅适用于 display: table-cell,在某些浏览器中你应该用 display: table 包裹 parent

.hello {
  height: 100px;
  width: 100px;
}

.parent {
  height: 400px;
  width: 400px;
  display: table-cell;
  vertical-align: middle;
}

<div class ="parent ">
  <div class="hello">
    hello
  </div>
</div> 

Use vertical-align: middle on .parent and make .hello - display: block with margin: 0 auto , like:.parent上使用vertical-align: middle并制作.hello - display: block with margin: 0 auto ,例如:

.hello {
  display: block;
  margin: 0 auto;
}

.parent {
  display: table-cell;
  vertical-align: middle;
}

Have a look at the snippet below:看看下面的片段:

 .hello{ height:100px; width:100px; background-color:black; display: block; margin: 0 auto; color: white; } .parent{ height:400px; width:400px; border:1px solid red; display: table-cell; vertical-align: middle; }
 <div class ="parent "> <div class="hello"> hello </div> </div>

Hope this helps!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM