简体   繁体   English

列表项上的全高背景色是否需要填充或溢出?

[英]Full height background-color on list item requires padding or overflow?

I have a solution for my problem, but it doesn't seem right. 我有解决问题的方法,但这似乎不正确。 I want to know what's going on. 我想知道发生了什么事。

Nested list item's background colour doesn't extend to the bottom even though there's no margin on it (see the gap below the blue background in the screen shots). 嵌套列表项的背景颜色不会扩展到底部,即使没有空白(请参见屏幕截图中蓝色背景下方的间隙)。 The paragraph inside does have a margin. 里面的段落确实有一个空白。 But I've tried to reproduce this outside of my app (which uses Bootstrap) and I can't. 但是我尝试在我的应用程序(使用Bootstrap)之外重现此内容,但我做不到。 In Firebug I tried turning off all CSS except that which was necessary to show the problem (ie, the background-color and border -- see 2nd image), but it makes no difference. 在Firebug中,我尝试关闭了所有CSS,除了显示问题所必需的CSS(即背景颜色和边框-参见第二张图片),但这没有什么区别。 Seen in Chrome and Firefox. 在Chrome和Firefox中可见。

The fix is either adding bottom padding or overflow-y:auto; 解决方法是添加底部填充或overflow-y:auto; to the inner list item. 到内部列表项。 But why? 但为什么? Has anyone encountered something like this? 有没有人遇到过这样的事情?

I can't post all the code here, but the HTML at least is something like this: 我无法在此处发布所有代码,但是HTML至少是这样的:

<ul class="nav navbar-nav navbar-right">
    <li class="dropdown open">
         <ul class="dropdown-menu notification-list">   
                    <li class="notification-new">
                        <div class="text-muted">01/13/2015</div>
                        <p>Check out the new features coming to the 2015.1 release <a href="javascript:void(0)">here</a>!</p>
                    </li>
                    <li class="notification-new">
                        <div class="text-muted">12/24/2014<button class="close" type="button"><span class="sr-only">Close</span></button></div>
                        <p>Upcoming server maintenance scheduled for 11:00pm PST.</p>
                    </li>

蓝色背景下的多余间隙

几乎所有CSS都已关闭

[Update] Here is a simplified, non-Bootstrap version. [更新]这是简化的非Bootstrap版本。 Why no gaps in this one? 为什么这没有差距?

  ul { width: 300px; border: 1px solid red; float: left; } li.sub { border: 1px solid green; background-color: yellow; padding: 8px 8px 0; } p { margin:10px; /* no gaps with or without this */ } 
  <ul> <li><p>item 1</p> <ul> <li class="sub"> <div> something </div> <p> stuff </p> </li> <li class="sub"> <div> something </div> <p> stuff </p> </li> </ul> </li> <li>thing <ul> <li class="sub"> nuther </li> </ul> </li> </ul> 

The spacing is occurring because of the margins in the p elements inside the li s, specifically the bottom margin. 之所以会出现间距,是因为li s内的p元素的边距,特别是底部边距。

This behavior is defined as 'collapsing margins'. 此行为被定义为“崩溃边距”。 More info here: http://www.w3.org/TR/CSS2/box.html#collapsing-margins 此处的更多信息: http : //www.w3.org/TR/CSS2/box.html#collapsing-margins

You can see this by setting them to 0. 您可以通过将其设置为0来查看。

.notification-new p{
  margin:0;
}

Live example: http://www.bootply.com/wlfIl3RziC 实时示例: http//www.bootply.com/wlfIl3RziC

Full code below: 完整代码如下:

 .notification-new { background-color:red; } .notification-new p{ margin:0; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <ul class="nav navbar-nav navbar-right"> <li class="dropdown open"> <ul class="dropdown-menu notification-list"> <li class="notification-new"> <div class="text-muted">01/13/2015</div> <p>Check out the new features coming to the 2015.1 release <a href="javascript:void(0)">here</a>!</p> </li> <li class="notification-new"> <div class="text-muted">12/24/2014<button class="close" type="button"><span class="sr-only">Close</span></button></div> <p>Upcoming server maintenance scheduled for 11:00pm PST.</p> </li> </ul> </li> </ul> 

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

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