简体   繁体   English

当兄弟 div 的高度由于内容而更大时,为什么 div 不将文本与包含 div 的顶部对齐?

[英]Why doesn't div align text to the top of containing div when sibling div's height is greater due to content?

I have two divs nested inside another div.我有两个 div 嵌套在另一个 div 中。 Both child divs are of the same width, but one div contains more lines then the other div and as a result, one div does not align it's content text to the top of the div.两个子 div 的宽度相同,但一个 div 比另一个 div 包含更多行,因此,一个 div 没有将其内容文本对齐到 div 的顶部。 I have tried using vertical-align: top and that hasn't worked.我尝试过使用vertical-align: top并没有奏效。

 .footer { width: 600px; vertical-align: top; }.footer >.agency, .footer >.vendor { width: 297px; display: inline-block; vertical-align: top; }.footer >.agency >.details, .footer >.vendor >.details { font-size: 14px; line-height: 20px; }
 <div class="footer"> <div class="agency"> <div class="title"><b>Agency</b></div> <div class="details"> <div class="name">Name</div> <div class="address1">address</div> <div class="address2"></div> <div class="city-state-zip">City, ST 12345</div> </div> </div> <div class="vendor"> <div class="title"><b>Vendor</b></div> <div class="details"> <div class="name">Anonymous</div> </div> </div> </div>

 .footer { width: 300px; vertical-align: bottom; border:1px solid; }.footer >.agency, .footer >.vendor { width: 297px; display: inline-block; vertical-align: top; }.footer >.agency >.details, .footer >.vendor >.details { font-size: 14px; line-height: 30px; }
 <div class="footer"> <div class="agency"> <div class="title"><b>Agency</b></div> <div class="details"> <div class="name">Name</div> <div class="address1">address</div> <div class="address2"></div> <div class="city-state-zip">City, ST 12345</div> </div> </div> <div class="vendor"> <div class="title"><b>Vendor</b></div> <div class="details"> <div class="name">Anonymous</div> </div> </div> </div>

Since you are using display: inline-block , it aligns to the baseline and not to the middle, or top. 由于您正在使用display: inline-block ,因此它与baseline对齐,而不与中间或顶部对齐。 So you need to use vertical-align: top to align the top of all the inline-block elements on the same line: 因此,您需要使用vertical-align: top将同一行中所有inline-block元素的顶部对齐:

.footer > .agency,
.footer > .vendor {
  width: 297px;
  vertical-align: top;
  display: inline-block;
}
.footer {
  vertical-align: top;
}

You should give the vertical-align: top for the inline-block 's parent too. 您还应该为inline-block的父级提供vertical-align: top

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

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