简体   繁体   English

Div没有采用全高度,因为它的子锚用填充物

[英]Div doesn't take full height as its child anchors with padding

I want to know why an anchor tag with top and bottom padding would not expand its parent div to its full height. 我想知道为什么带有顶部和底部填充的锚标记不会将其父div扩展到其完整高度。 eg here in this fiddle 例如这里的小提琴

<div class="container">
  <a href="#" class="button">Sign Up</a>
  <a href="#" class="button">Login</a>
</div>

https://jsfiddle.net/exleedo/8qr4srLa/ https://jsfiddle.net/exleedo/8qr4srLa/

The parent has grey background and the two buttons are inside this div, but still the div doesn't take the same height as the links. 父级具有灰色背景,两个按钮位于此div内,但div仍然与链接的高度不同。

Because of collapsing margins . 由于利润率下降

You can fix this by adding display:inline-block to your links: 您可以通过向链接添加display:inline-block来解决此问题:

 .container { background: #CCC; } .button { padding: 10px 15px; border-radius: 5px; background: #262626; color: #FFF; margin-left: 10px; display: inline-block; } 
 <div style="height:100px"> <!-- Spacer --> </div> <div class="container"> <a href="#" class="button">Sign Up</a> <a href="#" class="button">Login</a> </div> 

I do not recommend any inline style (height for that matter) just do this: 我不推荐任何内联样式(高度),只需这样做:

.container {
background: #CCC;
display: flex;
}

display: flex will have the auto inner wrap similar to box-sizing display:flex将具有类似于box-sizing的自动内部包装

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

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